Advertisement
Guest User

Untitled

a guest
Jan 19th, 2022
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Vector2 {
  2.     constructor(x, y) {
  3.         this.x = (typeof x === "undefined")?0:x;
  4.         this.y = (typeof y === "undefined")?0:y;
  5.         this.srand = new Srand();
  6.         this.dom = null;
  7.     }
  8.     toVector3() {
  9.         return new Vector3(this.x, this.y, 0);
  10.     }
  11.     add(what) {
  12.         if(what instanceof Vector3 || what instanceof Vector2) {
  13.             return(new Vector2(this.x+what.x,this.y+what.y));
  14.         } else if (typeof what === typeof 0) {
  15.             return(new Vector2(this.x+what,this.y+what));
  16.         } else {
  17.             return this;
  18.         }
  19.     }
  20.     sub(what) {
  21.         if(what instanceof Vector3 || what instanceof Vector2) {
  22.             return(new Vector2(this.x-what.x,this.y-what.y));
  23.         } else if (typeof what === typeof 0) {
  24.             return(new Vector2(this.x-what,this.y-what));
  25.         } else {
  26.             return this;
  27.         }
  28.     }
  29.     mul(what) {
  30.         if(what instanceof Vector3 || what instanceof Vector2) {
  31.             return(new Vector2(this.x*what.x,this.y*what.y));
  32.         } else if (typeof what === typeof 0) {
  33.             return(new Vector2(this.x*what,this.y*what));
  34.         } else {
  35.             return this;
  36.         }
  37.     }
  38.     div(what) {
  39.         if(what instanceof Vector3 || what instanceof Vector2) {
  40.             return(new Vector2(this.x/what.x,this.y/what.y));
  41.         } else if (typeof what === typeof 0) {
  42.             return(new Vector2(this.x/what,this.y/what));
  43.         } else {
  44.             return this;
  45.         }
  46.     }
  47.     static sortX(array) {
  48.         array.sort((a,b)=>{return a.x>b.x});
  49.         return array;
  50.     }
  51.     static sortY(array) {
  52.         array.sort((a,b)=>{return a.y>b.y});
  53.         return array;
  54.     }
  55.     static get forward(){return new Vector2(1,0)}
  56.     static get backward(){return new Vector2(-1,0)}
  57.     static get down(){return new Vector2(0,1)}
  58.     static get up(){return new Vector2(0,-1)}
  59.     static get zero(){return new Vector2(0,0)}
  60.     static get rand(){
  61.         var rnd = new Srand();
  62.         return new Vector2(rnd.next(),rnd.next());
  63.     }
  64.     distance(to) {
  65.         to = to | new Vector2(0, 0);
  66.         var cal_1 = Math.pow(to.x - this.x, 2);
  67.         var cal_2 = Math.pow(to.y - this.y, 2);
  68.         // var cal_3 = Math.pow(to.z - this.z, 2);
  69.         var cal_4 = Math.abs(Math.sqrt(cal_1 + cal_2));
  70.         return(cal_4);
  71.     }
  72. }
  73. class Vector3 {
  74.     constructor(x, y, z) {
  75.         this.x = (typeof x === "undefined")?0:x;
  76.         this.y = (typeof y === "undefined")?0:y;
  77.         this.z = (typeof z === "undefined")?0:z;
  78.         this.dom = null;
  79.     }
  80.     toVector2() {
  81.         return new Vector2(this.x, this.y);
  82.     }
  83.     add(what) {
  84.         if(what instanceof Vector3) {
  85.             return(new Vector3(this.x+what.x,this.y+what.y,this.z+what.z));
  86.         } else if (what instanceof Vector2) {
  87.             return(new Vector3(this.x+what.x,this.y+what.y,this.z));
  88.         } else if (typeof what === typeof 0) {
  89.             return(new Vector3(this.x+what,this.y+what,this.z+what));
  90.         } else {
  91.             return this;
  92.         }
  93.     }
  94.     sub(what) {
  95.         if(what instanceof Vector3) {
  96.             return(new Vector3(this.x-what.x,this.y-what.y,this.z-what.z));
  97.         } else if (what instanceof Vector2) {
  98.             return(new Vector3(this.x-what.x,this.y-what.y,this.z));
  99.         } else if (typeof what === typeof 0) {
  100.             return(new Vector3(this.x-what,this.y-what,this.z-what));
  101.         } else {
  102.             return this;
  103.         }
  104.     }
  105.     mul(what) {
  106.         if(what instanceof Vector3) {
  107.             return(new Vector3(this.x*what.x,this.y*what.y,this.z*what.z));
  108.         } else if (what instanceof Vector2) {
  109.             return(new Vector3(this.x*what.x,this.y*what.y));
  110.         } else if (typeof what === typeof 0) {
  111.             return(new Vector3(this.x*what,this.y*what,this.z*what));
  112.         } else {
  113.             return this;
  114.         }
  115.     }
  116.     div(what) {
  117.         if(what instanceof Vector3) {
  118.             return(new Vector3(this.x/what.x,this.y/what.y,this.z/what.z));
  119.         } else if (what instanceof Vector2) {
  120.             return(new Vector3(this.x/what.x,this.y/what.y));
  121.         } else if (typeof what === typeof 0) {
  122.             return(new Vector3(this.x/what,this.y/what,this.z/what));
  123.         } else {
  124.             return this;
  125.         }
  126.     }
  127.     static sortX(array) {
  128.         array.sort((a,b)=>{return a.x>b.x});
  129.         return array;
  130.     }
  131.     static sortY(array) {
  132.         array.sort((a,b)=>{return a.y>b.y});
  133.         return array;
  134.     }
  135.     static sortZ(array) {
  136.         array.sort((a,b)=>{return a.z>b.z});
  137.         return array;
  138.     }
  139.     static get forward(){return new Vector3(1,0,0)}
  140.     static get backward(){return new Vector3(-1,0,0)}
  141.     static get down(){return new Vector3(0,1,0)}
  142.     static get up(){return new Vector3(0,-1,0)}
  143.     static get back(){return new Vector3(0,0,1)}
  144.     static get front(){return new Vector3(0,0,-1)}
  145.     static get zero(){return new Vector3(0,0,0)}
  146.     static set seed(seed) {
  147.         this.srand = new Srand(seed);
  148.     }
  149.     static get rand(){
  150.         var rnd = (typeof this.srand === "undefined")?new Srand():this.srand;
  151.         return new Vector3(rnd.next(),rnd.next(),rnd.next());
  152.     }
  153.     distance(to) {
  154.         to = (to instanceof Vector3)?to:new Vector3(0, 0);
  155.         var cal_1 = Math.pow(to.x - this.x, 2);
  156.         var cal_2 = Math.pow(to.y - this.y, 2);
  157.         var cal_3 = Math.pow(to.z - this.z, 2);
  158.         var cal_4 = Math.abs(Math.sqrt(cal_1 + cal_2 + cal_3));
  159.         return(cal_4);
  160.     }
  161. }
  162.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement