Created
October 20, 2022 22:53
-
-
Save alessandromrc/1575b6a14939342e943c11ce822372dd to your computer and use it in GitHub Desktop.
Simple implementation of Vector3 and Vector2 in Javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Vector3 = class { | |
x = 0; | |
y = 0; | |
z = 0; | |
constructor(x, y, z) { | |
this.x = x; | |
this.y = y; | |
this.z = z; | |
} | |
subtract(vector) { | |
this.x -= vector.x; | |
this.y -= vector.y; | |
this.z -= vector.z | |
} | |
add(vector) { | |
this.x += vector.x; | |
this.y += vector.y; | |
this.z += vector.z; | |
} | |
divide(vector) { | |
this.x /= vector.x; | |
this.y /= vector.y; | |
this.z /= vector.z; | |
} | |
multiply(vector) { | |
this.x *= vector.x; | |
this.y *= vector.y; | |
this.z *= vector.z; | |
} | |
} | |
const vector = new Vector3(12, 12, 12); | |
const vector2 = new Vector3(6, 6, 6); | |
vector.divide(vector2); | |
console.log(vector) | |
const Vector2 = class { | |
x = 0; | |
y = 0; | |
constructor(x, y, z) { | |
this.x = x; | |
this.y = y; | |
} | |
subtract(vector) { | |
this.x -= vector.x; | |
this.y -= vector.y; | |
} | |
add(vector) { | |
this.x += vector.x; | |
this.y += vector.y; | |
} | |
divide(vector) { | |
this.x /= vector.x; | |
this.y /= vector.y; | |
} | |
multiply(vector) { | |
this.x *= vector.x; | |
this.y *= vector.y; | |
} | |
} | |
const vec2d = new Vector2(12, 12); | |
const vec2d2 = new Vector2(6, 6); | |
vec2d.divide(vec2d2); | |
console.log(vec2d) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment