Skip to content

Instantly share code, notes, and snippets.

@manthrax
Last active May 20, 2020 01:20
Show Gist options
  • Save manthrax/15bb5a96f09ebadb951981582c405e16 to your computer and use it in GitHub Desktop.
Save manthrax/15bb5a96f09ebadb951981582c405e16 to your computer and use it in GitHub Desktop.
class vec3 {
set(x=0, y=0, z=0) {
this.x = x;
this.y = y;
this.z = z;
return this;
}
copy(v) {
return this.set(v.x, v.y, v.z)
}
constructor(x, y, z) {
this.set(x, y, z);
}
dot(v) {
return (v.x * this.x) + (v.y * this.y) + (v.z * this.z)
}
len2() {
return this.x * this.x + this.y * this.y + this.z * this.z
}
len() {
return Math.sqrt(this.len2())
}
add(v) {
this.x += v.x;
this.y += v.y;
this.z += v.z;
return this
}
sub(v) {
this.x -= v.x;
this.y -= v.y;
this.z -= v.z;
return this
}
scale(v) {
this.x *= v;
this.y *= v;
this.z *= v;
return this;
}
cross(v) {
return this.set(this.y * v.z - this.z * v.y, this.z * v.y - this.x * v.z, this.x * v.y - this.y * v.x)
}
nml() {
return this.scale(1 / this.len())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment