This repository has been archived by the owner on Dec 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathg.min.js
1 lines (1 loc) · 17.4 KB
/
g.min.js
1
function calculateRadius(n){return Math.sqrt(n/Math.PI*2)}function updateObjects(n){forces=calculateForces(n);for(var t=0;t<n.length;t++){var r=multipleVectorOperation(forces[t],add),e=scalarOperation1(n[t].mass,r,div),o=(Date.now()-n[t].lastTimeUpdated)/SLOW_DOWN,i=initialiseArray(n[t].velocity.length,o),u=vectorOperation(i,e,mult);n[t].velocity=vectorOperation(n[t].velocity,u,add);var c=vectorOperation(i,n[t].velocity,mult);n[t].coord=vectorOperation(c,n[t].coord,add),n[t].lastTimeUpdated=Date.now()}return checkForCollisions(n),checkForOutSideCanvas(n),forces}function checkForCollisions(n){for(var t=0;t<n.length;t++)for(var r=0;r<n.length;r++)if(!(r<=t)&&detectCollision(n[t],n[r])){n[t].mass+=n[r].mass,n[t].radius=calculateRadius(n[t].mass),relativeVelocityJ=1*n[r].mass/n[t].mass,relativeVelocityI=1-relativeVelocityJ,n[t].velocity=scalarOperation(relativeVelocityI,n[t].velocity,mult);var e=scalarOperation(relativeVelocityJ,n[r].velocity,mult);n[t].velocity=vectorOperation(n[t].velocity,e,add),n.splice(r,1),r--}}function detectCollision(n,t){return getDistance(vectorOperation(n.coord,t.coord,sub))<n.radius+t.radius}function checkForOutSideCanvas(n){for(var t=0;t<n.length;t++)n[t].coord[0]>MAX_COORD[0]||n[t].coord[0]<0?n[t].velocity=[-n[t].velocity[0],n[t].velocity[1]]:(n[t].coord[1]>MAX_COORD[1]||n[t].coord[1]<0)&&(n[t].velocity=[n[t].velocity[0],-n[t].velocity[1]])}function calculateForces(n){for(var t=initialise2DArray(n.length),r=0;r<n.length;r++)for(var e=0;e<n.length;e++)if(r==e)t[r][e]=initialiseArray(n[r].force.length,0);else{if(void 0!==t[r][e])continue;t[e][r]=calculateForce(n[r],n[e]),t[r][e]=_.map(t[e][r],function(n){return-n})}return t}function calculateForce(n,t){var r=vectorOperation(n.coord,t.coord,sub),e=getDistance(r),o=0===e?initialiseArray(r.length,0):getNormalisedVector(r,e),i=0===e?0:G*n.mass*t.mass/(e*e);return _.map(o,function(n){return n*i})}function getDistance(n){for(var t=0,r=0;r<n.length;r++)t+=n[r]*n[r];return Math.sqrt(t)}function getNormalisedVector(n,t){return _.map(n,function(n){return 1*n/t})}function multipleVectorOperation(n,r){return _.reduce(n,function(n,t){return vectorOperation(n,t,r)})}function vectorOperation(n,t,r){for(var e=Math.min(n.length,t.length),o=[],i=0;i<e;i++)o.push(r(1*n[i],t[i]));return o}function scalarOperation(t,n,r){return _.map(n,function(n){return r(t,n)})}function scalarOperation1(t,n,r){return _.map(n,function(n){return r(n,t)})}function initialiseArray(n,t){return _.map(new Array(n),function(n){return t})}function initialise2DArray(n){for(var t=[],r=0;r<n;r++)t.push(new Array(n));return t}function sub(n,t){return n-t}function add(n,t){return n+t}function mult(n,t){return n*t}function div(n,t){return 0===t?0:n/t}function init(){var n=0<arguments.length&&void 0!==arguments[0]?arguments[0]:"canvas",t=1<arguments.length&&void 0!==arguments[1]?arguments[1]:[window.innerWidth-20,.8*window.innerHeight],r=2<arguments.length&&void 0!==arguments[2]?arguments[2]:100,e=3<arguments.length&&void 0!==arguments[3]?arguments[3]:.4,o=4<arguments.length&&void 0!==arguments[4]?arguments[4]:26,i=!(5<arguments.length&&void 0!==arguments[5])||arguments[5];canvas=document.getElementById(n),context=canvas.getContext("2d"),OBJECTS=r,G=e,SLOW_DOWN=o,MAX_DIST=getDistance([(MAX_COORD=t)[0]/6,MAX_COORD[1]/6]),setCanvasSize(canvas);var u=generateObjects(r);i&&(window.onresize=function(){cancelAnimationFrame(requestId),MAX_COORD=[window.innerWidth-20,.8*window.innerHeight],setCanvasSize(canvas),draw(generateObjects(OBJECTS))}),draw(u)}function setCanvasSize(n){n.setAttribute("width",MAX_COORD[0]),n.setAttribute("height",MAX_COORD[1])}function draw(n){if(n.length<20)return cancelAnimationFrame(requestId),void draw(generateObjects(OBJECTS));context.clearRect(0,0,MAX_COORD[0],MAX_COORD[1]);var t=updateObjects(n);checkForOutOfBounds(n),drawCircles(n),drawLines(n,t),requestId=requestAnimationFrame(function(){return draw(n)})}function drawCircles(n){n.forEach(function(n){context.beginPath(),context.arc(n.coord[0],n.coord[1],n.radius,0,2*Math.PI,!1),20<n.mass?context.fillStyle="rgb(255, 255, 255)":context.fillStyle="rgba(255, 255, 255,"+OPACITY+")",context.fill(),context.lineWidth=0})}function drawLines(n,t){getMagnitudeForce(n,t);for(var r=0;r<n.length;r++)for(var e=0;e<n.length;e++)if(!(e<=r||distanceTooGreat(n[r],n[e]))){if(t[r][e]>=MAX)context.strokeStyle="rgba(255, 255, 255,"+OPACITY+")";else{var o=t[r][e]/MAX*OPACITY;context.strokeStyle="rgba(255, 255, 255,"+o+")"}context.beginPath(),context.lineWidth=1,context.moveTo(n[r].coord[0],n[r].coord[1]),context.lineTo(n[e].coord[0],n[e].coord[1]),context.stroke()}}function checkForOutOfBounds(n){for(var t=0;t<n.length;t++)(n[t].coord[0]<-TOLERANCE||n[t].coord[0]>MAX_COORD[0]+TOLERANCE||n[t].coord[1]<-TOLERANCE||n[t].coord[1]>MAX_COORD[1]+TOLERANCE)&&(n.splice(t,1),t--)}function distanceTooGreat(n,t){var r=getDistance(vectorOperation(n.coord,t.coord,sub));return MAX_DIST<r}function getMagnitudeForce(n,t){for(var r=0;r<n.length;r++)for(var e=0;e<n.length;e++)e<r||(t[r][e]=getDistance(t[r][e]),t[e][r]=t[r][e])}function generateObjects(n){for(var t=[],r=0;r<n;r++){var e=1*getRandomInt(1,10);t.push({mass:e,radius:calculateRadius(e),velocity:[1*getRandomInt(0,1),1*getRandomInt(0,1)],force:[0,0],coord:[getRandomInt(0,MAX_COORD[0]),getRandomInt(0,MAX_COORD[1])],lastTimeUpdated:Date.now()})}return t}function getRandomInt(n,t){return Math.floor(Math.random()*(t-n+1))+n}(function(){function a(n,t){return n.push.apply(n,t),n}function t(t){return function(n){return null==n?U:n[t]}}function i(n){return n instanceof r?n:new r(n)}function r(n,t){this.__wrapped__=n,this.__actions__=[],this.__chain__=!!t}function e(n,t,r,e){return n===U||X(n,jn[r])&&!Cn.call(e,r)?t:n}function v(n,t,r){n[t]=r}function o(n,t,r){if("function"!=typeof n)throw new TypeError(K);return setTimeout(function(){n.apply(U,r)},t)}function u(n,t,r){for(var e=-1,o=n.length;++e<o;){var i=n[e],u=t(i);if(null!=u&&(c===U?u==u:r(u,c)))var c=u,a=i}return a}function c(n,e){var o=[];return Jn(n,function(n,t,r){e(n,t,r)&&o.push(n)}),o}function f(n,t,r,e,o){var i=-1,u=n.length;for(r||(r=R),o||(o=[]);++i<u;){var c=n[i];0<t&&r(c)?1<t?f(c,t-1,r,e,o):a(o,c):e||(o[o.length]=c)}return o}function n(n,t){return n&&zn(n,t,ut)}function l(t,n){return c(n,function(n){return k(t[n])})}function _(n){return function(n){return xn.call(n)}(n)}function s(n,t){return t<n}function p(n,t,r,e,o){return n===t||(null==n||null==t||!q(n)&&!B(t)?n!=n&&t!=t:function(t,r,n,e,o,i){var u=Zn(t),c=Zn(r),a=rn,f=rn;u||(a=(a=_(t))==tn?sn:a),c||(f=(f=_(r))==tn?sn:f);var l=a==sn,s=f==sn,p=a==f;i||(i=[]);var v=Yn(i,function(n){return n[0]==t}),h=Yn(i,function(n){return n[0]==r});if(v&&h)return v[1]==r;if(i.push([t,r]),i.push([r,t]),p&&!l){var d=u?function(n,t,r,e,o,i){var u=r&Q,c=n.length,a=t.length;if(c!=a&&!(u&&c<a))return!1;for(var f=-1,l=!0,s=r&Z?[]:U;++f<c;){var p=n[f],v=t[f];if(void 0!==U){void 0,l=!1;break}if(s){if(!A(t,function(n,t){if(!M(s,t)&&(p===n||o(p,n,r,e,i)))return s.push(t)})){l=!1;break}}else if(p!==v&&!o(p,v,r,e,i)){l=!1;break}}return l}(t,r,n,e,o,i):function(n,t,r){switch(r){case on:case un:case ln:return X(+n,+t);case cn:return n.name==t.name&&n.message==t.message;case vn:case hn:return n==t+""}return!1}(t,r,a);return i.pop(),d}if(!(n&Q)){var g=l&&Cn.call(t,"__wrapped__"),y=s&&Cn.call(r,"__wrapped__");if(g||y){var O=g?t.value():t,m=y?r.value():r;d=o(O,m,n,e,i);return i.pop(),d}}if(!p)return!1;d=function(n,t,r,e,o,i){var u=r&Q,c=ut(n),a=c.length,f=ut(t).length;if(a!=f&&!u)return!1;for(var l=a;l--;){var s=c[l];if(!(u?s in t:Cn.call(t,s)))return!1}for(var p=!0,v=u;++l<a;){s=c[l];var h=n[s],d=t[s];if(!(void 0===U?h===d||o(h,d,r,e,i):void 0)){p=!1;break}v||(v="constructor"==s)}if(p&&!v){var g=n.constructor,y=t.constructor;g!=y&&"constructor"in n&&"constructor"in t&&!("function"==typeof g&&g instanceof g&&"function"==typeof y&&y instanceof y)&&(p=!1)}return p}(t,r,n,e,o,i);return i.pop(),d}(n,t,r,e,p,o))}function h(n){return"function"==typeof n?n:null==n?J:("object"==typeof n?y:t)(n)}function d(n,t){return n<t}function g(n,e){var o=-1,i=N(n)?Array(n.length):[];return Jn(n,function(n,t,r){i[++o]=e(n,t,r)}),i}function y(e){var o=Tn(e);return function(n){var t=o.length;if(null==n)return!t;for(n=Object(n);t--;){var r=o[t];if(!(r in n&&p(e[r],n[r],Q|Z)))return!1}return!0}}function O(n,t){return Un(D(n,t,J),n+"")}function m(n,t,r){var e=-1,o=n.length;t<0&&(t=o<-t?0:o+t),(r=o<r?o:r)<0&&(r+=o),o=r<t?0:r-t>>>0,t>>>=0;for(var i=Array(o);++e<o;)i[e]=n[e+t];return i}function b(n){return m(n,0,n.length)}function A(n,e){var o;return Jn(n,function(n,t,r){return!(o=e(n,t,r))}),!!o}function w(n,t,r,e){var o,i,u,c,a=!r;r||(r={});for(var f=-1,l=t.length;++f<l;){var s=t[f],p=e?e(r[s],n[s],s,r,n):U;p===U&&(p=n[s]),a?v(r,s,p):(u=p,void 0,c=(o=r)[i=s],Cn.call(o,i)&&X(c,u)&&(u!==U||i in o)||v(o,i,u))}return r}function j(u){return O(function(n,t){var r=-1,e=t.length,o=1<e?t[e-1]:U;for(o=3<u.length&&"function"==typeof o?(e--,o):U,n=Object(n);++r<e;){var i=t[r];i&&u(n,i,r,o)}return n})}function C(c,n,a,f){if("function"!=typeof c)throw new TypeError(K);var l=1&n,s=function(e){return function(){var n=arguments,t=Sn(e.prototype),r=e.apply(t,n);return q(r)?r:t}}(c);return function n(){for(var t=-1,r=arguments.length,e=-1,o=f.length,i=Array(o+r),u=this&&this!==mn&&this instanceof n?s:c;++e<o;)i[e]=f[e];for(;r--;)i[e++]=arguments[++t];return u.apply(l?a:this,i)}}function R(n){return Zn(n)||Qn(n)}function x(n){var t=[];if(null!=n)for(var r in Object(n))t.push(r);return t}function D(i,u,c){return u=In(u===U?i.length-1:u,0),function(){for(var n=arguments,t=-1,r=In(n.length-u,0),e=Array(r);++t<r;)e[t]=n[u+t];t=-1;for(var o=Array(u+1);++t<u;)o[t]=n[t];return o[u]=c(e),i.apply(this,o)}}function E(n){return(null==n?0:n.length)?f(n,1):[]}function F(n){return n&&n.length?n[0]:U}function M(n,t,r){for(var e=null==n?0:n.length,o=((r="number"==typeof r?r<0?In(e+r,0):r:0)||0)-1,i=t==t;++o<e;){var u=n[o];if(i?u===t:u!=u)return o}return-1}function T(n,t){return Jn(n,h(t))}function I(n,t,r){return function(n,e,o,i,t){return t(n,function(n,t,r){o=i?(i=!1,n):e(o,n,t,r)}),o}(n,h(t),r,arguments.length<3,Jn)}function S(n,t){var r;if("function"!=typeof t)throw new TypeError(K);return n=nt(n),function(){return 0<--n&&(r=t.apply(this,arguments)),n<=1&&(t=U),r}}function X(n,t){return n===t||n!=n&&t!=t}function N(n){return null!=n&&function(n){return"number"==typeof n&&-1<n&&n%1==0&&n<=nn}(n.length)&&!k(n)}function k(n){if(!q(n))return!1;var t=_(n);return t==an||t==fn||t==en||t==pn}function q(n){var t=typeof n;return null!=n&&("object"==t||"function"==t)}function B(n){return null!=n&&"object"==typeof n}function L(n){return"number"==typeof n||B(n)&&_(n)==ln}function P(n){return"string"==typeof n||!Zn(n)&&B(n)&&_(n)==hn}function V(n){return"string"==typeof n?n:null==n?"":n+""}function W(n){return null==n?[]:function(t,n){return g(n,function(n){return t[n]})}(n,ut(n))}function J(n){return n}function z(e,t,n){var r=ut(t),o=l(t,r);null!=n||q(t)&&(o.length||!r.length)||(n=t,t=e,e=this,o=l(t,ut(t)));var i=!(q(n)&&"chain"in n&&!n.chain),u=k(e);return Jn(o,function(n){var r=t[n];e[n]=r,u&&(e.prototype[n]=function(){var n=this.__chain__;if(i||n){var t=e(this.__wrapped__);return(t.__actions__=b(this.__actions__)).push({func:r,args:arguments,thisArg:e}),t.__chain__=n,t}return r.apply(e,a([this.value()],arguments))})}),e}function G(){}var U,Y,$,H,K="Expected a function",Q=1,Z=2,nn=9007199254740991,tn="[object Arguments]",rn="[object Array]",en="[object AsyncFunction]",on="[object Boolean]",un="[object Date]",cn="[object Error]",an="[object Function]",fn="[object GeneratorFunction]",ln="[object Number]",sn="[object Object]",pn="[object Proxy]",vn="[object RegExp]",hn="[object String]",dn=/[&<>"']/g,gn=RegExp(dn.source),yn="object"==typeof global&&global&&global.Object===Object&&global,On="object"==typeof self&&self&&self.Object===Object&&self,mn=yn||On||Function("return this")(),_n="object"==typeof exports&&exports&&!exports.nodeType&&exports,bn=_n&&"object"==typeof module&&module&&!module.nodeType&&module,An=(H={"&":"&","<":"<",">":">",'"':""","'":"'"},function(n){return null==H?U:H[n]}),wn=Array.prototype,jn=Object.prototype,Cn=jn.hasOwnProperty,Rn=0,xn=jn.toString,Dn=mn._,En=Object.create,Fn=jn.propertyIsEnumerable,Mn=mn.isFinite,Tn=(Y=Object.keys,$=Object,function(n){return Y($(n))}),In=Math.max,Sn=function(n){if(!q(n))return{};if(En)return En(n);Xn.prototype=n;var t=new Xn;return Xn.prototype=U,t};function Xn(){}(r.prototype=Sn(i.prototype)).constructor=r;function Nn(n){return B(n)&&_(n)==un}function kn(n){return B(n)&&_(n)==vn}var qn,Bn,Ln,Pn,Vn,Wn,Jn=(Pn=n,function(n,t){if(null==n)return n;if(!N(n))return Pn(n,t);for(var r=n.length,e=Vn?r:-1,o=Object(n);(Vn?e--:++e<r)&&!1!==t(o[e],e,o););return n}),zn=function(n,t,r){for(var e=-1,o=Object(n),i=r(n),u=i.length;u--;){var c=i[Ln?u:++e];if(!1===t(o[c],c,o))break}return n},Gn=G,Un=J,Yn=(Bn=function(n,t,r){var e=null==n?0:n.length;if(!e)return-1;var o=null==r?0:nt(r);return o<0&&(o=In(e+o,0)),function(n,t,r,e){for(var o=n.length,i=r+(e?1:-1);e?i--:++i<o;)if(t(n[i],i,n))return i;return-1}(n,h(t),o)},function(n,t,r){var e=Object(n);if(!N(n)){var o=h(t);n=ut(n),t=function(n){return o(e[n],n,e)}}var i=Bn(n,t,r);return-1<i?e[o?n[i]:i]:U}),$n=O(function(n,t,r){return C(n,33,t,r)}),Hn=O(function(n,t){return o(n,1,t)}),Kn=O(function(n,t,r){return o(n,tt(t)||0,r)}),Qn=Gn(function(){return arguments}())?Gn:function(n){return B(n)&&Cn.call(n,"callee")&&!Fn.call(n,"callee")},Zn=Array.isArray,nt=Number,tt=Number,rt=j(function(n,t){w(t,Tn(t),n)}),et=j(function(n,t){w(t,x(t),n)}),ot=j(function(n,t,r,e){w(t,ct(t),n,e)}),it=O(function(n){return n.push(U,e),ot.apply(U,n)}),ut=Tn,ct=x,at=Un(D(qn=function(n,t){return null==n?{}:function(r,n){return r=Object(r),I(n,function(n,t){return t in r&&(n[t]=r[t]),n},{})}(n,t)},U,E),qn+""),ft=h;i.assignIn=et,i.before=S,i.bind=$n,i.chain=function(n){var t=i(n);return t.__chain__=!0,t},i.compact=function(n){return c(n,Boolean)},i.concat=function(){var n=arguments.length;if(!n)return[];for(var t=Array(n-1),r=arguments[0],e=n;e--;)t[e-1]=arguments[e];return a(Zn(r)?b(r):[r],f(t,1))},i.create=function(n,t){var r=Sn(n);return null==t?r:rt(r,t)},i.defaults=it,i.defer=Hn,i.delay=Kn,i.filter=function(n,t){return c(n,h(t))},i.flatten=E,i.flattenDeep=function(n){return(null==n?0:n.length)?f(n,1/0):[]},i.iteratee=ft,i.keys=ut,i.map=function(n,t){return g(n,h(t))},i.matches=function(n){return y(rt({},n))},i.mixin=z,i.negate=function(t){if("function"!=typeof t)throw new TypeError(K);return function(){var n=arguments;return!t.apply(this,n)}},i.once=function(n){return S(2,n)},i.pick=at,i.slice=function(n,t,r){var e=null==n?0:n.length;return t=null==t?0:+t,r=r===U?e:+r,e?m(n,t,r):[]},i.sortBy=function(n,e){var o=0;return e=h(e),g(g(n,function(n,t,r){return{value:n,index:o++,criteria:e(n,t,r)}}).sort(function(n,t){return function(n,t){if(n!==t){var r=n!==U,e=null===n,o=n==n,i=t!==U,u=null===t,c=t==t;if(!u&&t<n||e&&i&&c||!r&&c||!o)return 1;if(!e&&n<t||u&&r&&o||!i&&o||!c)return-1}return 0}(n.criteria,t.criteria)||n.index-t.index}),t("value"))},i.tap=function(n,t){return t(n),n},i.thru=function(n,t){return t(n)},i.toArray=function(n){return N(n)?n.length?b(n):[]:W(n)},i.values=W,i.extend=et,z(i,i),i.clone=function(n){return q(n)?Zn(n)?b(n):w(n,Tn(n)):n},i.escape=function(n){return(n=V(n))&&gn.test(n)?n.replace(dn,An):n},i.every=function(n,t,r){return function(n,e){var o=!0;return Jn(n,function(n,t,r){return o=!!e(n,t,r)}),o}(n,h(t=r?U:t))},i.find=Yn,i.forEach=T,i.has=function(n,t){return null!=n&&Cn.call(n,t)},i.head=F,i.identity=J,i.indexOf=M,i.isArguments=Qn,i.isArray=Zn,i.isBoolean=function(n){return!0===n||!1===n||B(n)&&_(n)==on},i.isDate=Nn,i.isEmpty=function(n){return N(n)&&(Zn(n)||P(n)||k(n.splice)||Qn(n))?!n.length:!Tn(n).length},i.isEqual=function(n,t){return p(n,t)},i.isFinite=function(n){return"number"==typeof n&&Mn(n)},i.isFunction=k,i.isNaN=function(n){return L(n)&&n!=+n},i.isNull=function(n){return null===n},i.isNumber=L,i.isObject=q,i.isRegExp=kn,i.isString=P,i.isUndefined=function(n){return n===U},i.last=function(n){var t=null==n?0:n.length;return t?n[t-1]:U},i.max=function(n){return n&&n.length?u(n,J,s):U},i.min=function(n){return n&&n.length?u(n,J,d):U},i.noConflict=function(){return mn._===this&&(mn._=Dn),this},i.noop=G,i.reduce=I,i.result=function(n,t,r){var e=null==n?U:n[t];return e===U&&(e=r),k(e)?e.call(n):e},i.size=function(n){return null==n?0:(n=N(n)?n:Tn(n)).length},i.some=function(n,t,r){return A(n,h(t=r?U:t))},i.uniqueId=function(n){var t=++Rn;return V(n)+t},i.each=T,i.first=F,z(i,(Wn={},n(i,function(n,t){Cn.call(i.prototype,t)||(Wn[t]=n)}),Wn),{chain:!1}),i.VERSION="4.17.2",Jn(["pop","join","replace","reverse","split","push","shift","sort","splice","unshift"],function(n){var r=(/^(?:replace|split)$/.test(n)?String.prototype:wn)[n],e=/^(?:push|sort|unshift)$/.test(n)?"tap":"thru",o=/^(?:pop|join|replace|shift)$/.test(n);i.prototype[n]=function(){var t=arguments;if(!o||this.__chain__)return this[e](function(n){return r.apply(Zn(n)?n:[],t)});var n=this.value();return r.apply(Zn(n)?n:[],t)}}),i.prototype.toJSON=i.prototype.valueOf=i.prototype.value=function(){return function(n,t){return I(t,function(n,t){return t.func.apply(t.thisArg,a([n],t.args))},n)}(this.__wrapped__,this.__actions__)},"function"==typeof define&&"object"==typeof define.amd&&define.amd?(mn._=i,define(function(){return i})):bn?((bn.exports=i)._=i,_n._=i):mn._=i}).call(this);var MAX_DIST,MAX_COORD,OBJECTS,G,SLOW_DOWN,canvas,context,requestId,OPACITY=.7,MAX=.01,TOLERANCE=10,requestAnimationFrame=window.requestAnimationFrame||window.mozRequestAnimationFrame||window.webkitRequestAnimationFrame||window.msRequestAnimationFrame,cancelAnimationFrame=window.cancelAnimationFrame||window.mozCancelAnimationFrame;