SHARE
TWEET

Untitled

a guest Dec 2nd, 2019 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const _ = undefined;
  2. const log = console.log;
  3. const toRad = deg => deg*Math.PI/180;
  4. const toDeg = deg => deg*180/Math.PI;
  5. const pythag = (x,y) => Math.sqrt(Math.pow(x,2)+Math.pow(y,2));
  6. const round = n => Math.round(n*1000)/1000;
  7. const vertex = (a,b) => (a==0) ? 0 : (-1*b)/(2*a);
  8. function quadform(a,b,c){
  9.     let ans = [];
  10.     if(a!=0){
  11.         let determinate = Math.sqrt(Math.pow(b,2)-4*a*c);
  12.         let ans1 = (-1*b-determinate)/(2*a);
  13.         let ans2 = (-1*b+determinate)/(2*a);
  14.         if(ans1||ans1==0) ans.push(ans1);
  15.         if(ans2||ans2==0) ans.push(ans2);
  16.     }else ans.push(-1*c/b);
  17.     return ans;
  18. }
  19. class vector{
  20.     constructor(ang,dist,x,y,...accelerators){
  21.         this.ang = ang||0;
  22.         this.dist = dist;
  23.         this.x=x;
  24.         this.y=y;
  25.         this.accels = accelerators;
  26.     }
  27.     addAccelerator(accel){
  28.         this.accels.push(accel);
  29.         return this;
  30.     }
  31.     multiply(c) {
  32.         return new vectorC(this.x*c,this.y*c,...this.accels);
  33.     }
  34.     divide(c){
  35.         return this.multiply(1/c);
  36.     }
  37.     negate() {
  38.         return this.multiply(-1);
  39.     }
  40.     toComponents() {
  41.         return `${round(this.x)}\\ivec${(this.y>0)?"+":""}${round(this.y)}\\jvec`;
  42.     }
  43.     logMagnitude(){
  44.         console.log(`Magnitude: ${round(this.dist)}`);
  45.         return this;
  46.     }
  47.     logAngle(){
  48.         let deg = toDeg(this.ang);
  49.         if(this.xc<0) deg+=180;
  50.         //if(deg>180) deg-=360;
  51.         console.log(`Angle: ${round(deg)} (Rads: ${round(this.ang)})`);
  52.         return this;
  53.     }
  54.     logX(){
  55.         console.log(`X Component: ${round(this.x)}`);
  56.         return this;
  57.     }
  58.     logY(){
  59.         console.log(`Y Component: ${round(this.y)}`);
  60.         return this;
  61.     }
  62.     logWeb(){
  63.         console.log(`WebForm: ${this.toComponents()}`);
  64.         return this;
  65.     }
  66.     log(){
  67.         return this.logMagnitude().logAngle().logX().logY().logWeb();
  68.     }
  69.     atTime(time){
  70.         let x = this.x*time;
  71.         let y = this.y*time;
  72.         for(let accel of this.accels){
  73.             x+=0.5*Math.pow(time,2)*accel.x;
  74.             y+=0.5*Math.pow(time,2)*accel.y;
  75.         }
  76.         return new vectorC(x,y);
  77.     }
  78.     accelSum(){
  79.         return sumVectors(...this.accels);
  80.     }
  81.     atPositionX(x){
  82.         let a = this.accelSum().x;
  83.         let b = this.x;
  84.         let c = -1*x;
  85.         return quadform((a/2)||0,b,c).map(ans=>(new timeData(ans, this.atTime(ans))));
  86.     }
  87.     atPositionY(y){
  88.         let a = this.accelSum().y;
  89.         let b = this.y;
  90.         let c = -1*y;
  91.         return quadform((a/2)||0,b,c).map(ans=>(new timeData(ans, this.atTime(ans))));
  92.     }
  93.     vertexX(){
  94.         let t = vertex((this.accelSum().x/2)||0, this.x);
  95.         return new timeData(t, this.atTime(t))
  96.     }
  97.     vertexY(){
  98.         let t = vertex((this.accelSum().y/2)||0, this.y);
  99.         return new timeData(t, this.atTime(t))
  100.     }
  101. }
  102. class vectorA extends vector{
  103.     constructor(ang,dist,...accels){
  104.         super(toRad(ang+((dist<0)?180:0)),Math.abs(dist),Math.cos(toRad(ang))*dist,Math.sin(toRad(ang))*dist,...accels);
  105.     }
  106. }
  107. class vectorC extends vector{
  108.     constructor(x,y,...accels){
  109.         super(Math.atan(y/x),pythag(x,y),x,y,...accels);
  110.     }
  111. }
  112. class timeData{
  113.     constructor(time, data){
  114.         this.time = time;
  115.         this.data = data;
  116.     }
  117.     log(){
  118.         console.log(`Time: ${round(this.time)}`);
  119.         return this.data;
  120.     }
  121. }
  122. function sumVectors(...vectors){
  123.     let cx = vectors.reduce((acc,v)=>acc+v.x,0);
  124.     let cy = vectors.reduce((acc,v)=>acc+v.y,0);
  125.     return new vectorC(cx,cy);
  126. }
  127.  
  128. const gravity = new vectorC(0,-9.8);
  129.  
  130. let object = new vectorA(54,28,gravity);
  131. object.atTime(2).logX().logY();
  132. object.atTime(4).logX().logY();
  133. object.atTime(6).logX().logY();
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top