Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Vector2 {
- constructor(x, y) {
- this.x = (typeof x === "undefined")?0:x;
- this.y = (typeof y === "undefined")?0:y;
- this.srand = new Srand();
- this.dom = null;
- }
- toVector3() {
- return new Vector3(this.x, this.y, 0);
- }
- add(what) {
- if(what instanceof Vector3 || what instanceof Vector2) {
- return(new Vector2(this.x+what.x,this.y+what.y));
- } else if (typeof what === typeof 0) {
- return(new Vector2(this.x+what,this.y+what));
- } else {
- return this;
- }
- }
- sub(what) {
- if(what instanceof Vector3 || what instanceof Vector2) {
- return(new Vector2(this.x-what.x,this.y-what.y));
- } else if (typeof what === typeof 0) {
- return(new Vector2(this.x-what,this.y-what));
- } else {
- return this;
- }
- }
- mul(what) {
- if(what instanceof Vector3 || what instanceof Vector2) {
- return(new Vector2(this.x*what.x,this.y*what.y));
- } else if (typeof what === typeof 0) {
- return(new Vector2(this.x*what,this.y*what));
- } else {
- return this;
- }
- }
- div(what) {
- if(what instanceof Vector3 || what instanceof Vector2) {
- return(new Vector2(this.x/what.x,this.y/what.y));
- } else if (typeof what === typeof 0) {
- return(new Vector2(this.x/what,this.y/what));
- } else {
- return this;
- }
- }
- static sortX(array) {
- array.sort((a,b)=>{return a.x>b.x});
- return array;
- }
- static sortY(array) {
- array.sort((a,b)=>{return a.y>b.y});
- return array;
- }
- static get forward(){return new Vector2(1,0)}
- static get backward(){return new Vector2(-1,0)}
- static get down(){return new Vector2(0,1)}
- static get up(){return new Vector2(0,-1)}
- static get zero(){return new Vector2(0,0)}
- static get rand(){
- var rnd = new Srand();
- return new Vector2(rnd.next(),rnd.next());
- }
- distance(to) {
- to = to | new Vector2(0, 0);
- var cal_1 = Math.pow(to.x - this.x, 2);
- var cal_2 = Math.pow(to.y - this.y, 2);
- // var cal_3 = Math.pow(to.z - this.z, 2);
- var cal_4 = Math.abs(Math.sqrt(cal_1 + cal_2));
- return(cal_4);
- }
- }
- class Vector3 {
- constructor(x, y, z) {
- this.x = (typeof x === "undefined")?0:x;
- this.y = (typeof y === "undefined")?0:y;
- this.z = (typeof z === "undefined")?0:z;
- this.dom = null;
- }
- toVector2() {
- return new Vector2(this.x, this.y);
- }
- add(what) {
- if(what instanceof Vector3) {
- return(new Vector3(this.x+what.x,this.y+what.y,this.z+what.z));
- } else if (what instanceof Vector2) {
- return(new Vector3(this.x+what.x,this.y+what.y,this.z));
- } else if (typeof what === typeof 0) {
- return(new Vector3(this.x+what,this.y+what,this.z+what));
- } else {
- return this;
- }
- }
- sub(what) {
- if(what instanceof Vector3) {
- return(new Vector3(this.x-what.x,this.y-what.y,this.z-what.z));
- } else if (what instanceof Vector2) {
- return(new Vector3(this.x-what.x,this.y-what.y,this.z));
- } else if (typeof what === typeof 0) {
- return(new Vector3(this.x-what,this.y-what,this.z-what));
- } else {
- return this;
- }
- }
- mul(what) {
- if(what instanceof Vector3) {
- return(new Vector3(this.x*what.x,this.y*what.y,this.z*what.z));
- } else if (what instanceof Vector2) {
- return(new Vector3(this.x*what.x,this.y*what.y));
- } else if (typeof what === typeof 0) {
- return(new Vector3(this.x*what,this.y*what,this.z*what));
- } else {
- return this;
- }
- }
- div(what) {
- if(what instanceof Vector3) {
- return(new Vector3(this.x/what.x,this.y/what.y,this.z/what.z));
- } else if (what instanceof Vector2) {
- return(new Vector3(this.x/what.x,this.y/what.y));
- } else if (typeof what === typeof 0) {
- return(new Vector3(this.x/what,this.y/what,this.z/what));
- } else {
- return this;
- }
- }
- static sortX(array) {
- array.sort((a,b)=>{return a.x>b.x});
- return array;
- }
- static sortY(array) {
- array.sort((a,b)=>{return a.y>b.y});
- return array;
- }
- static sortZ(array) {
- array.sort((a,b)=>{return a.z>b.z});
- return array;
- }
- static get forward(){return new Vector3(1,0,0)}
- static get backward(){return new Vector3(-1,0,0)}
- static get down(){return new Vector3(0,1,0)}
- static get up(){return new Vector3(0,-1,0)}
- static get back(){return new Vector3(0,0,1)}
- static get front(){return new Vector3(0,0,-1)}
- static get zero(){return new Vector3(0,0,0)}
- static set seed(seed) {
- this.srand = new Srand(seed);
- }
- static get rand(){
- var rnd = (typeof this.srand === "undefined")?new Srand():this.srand;
- return new Vector3(rnd.next(),rnd.next(),rnd.next());
- }
- distance(to) {
- to = (to instanceof Vector3)?to:new Vector3(0, 0);
- var cal_1 = Math.pow(to.x - this.x, 2);
- var cal_2 = Math.pow(to.y - this.y, 2);
- var cal_3 = Math.pow(to.z - this.z, 2);
- var cal_4 = Math.abs(Math.sqrt(cal_1 + cal_2 + cal_3));
- return(cal_4);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement