Skip to content

Instantly share code, notes, and snippets.

@alessandromrc
Created October 20, 2022 22:53
Show Gist options
  • Save alessandromrc/1575b6a14939342e943c11ce822372dd to your computer and use it in GitHub Desktop.
Save alessandromrc/1575b6a14939342e943c11ce822372dd to your computer and use it in GitHub Desktop.
Simple implementation of Vector3 and Vector2 in Javascript
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