webgl/public/gl/math/Vec2.js
Tyler Murphy 30cf48cd70 cube
2023-01-18 22:43:02 -05:00

78 lines
No EOL
1.2 KiB
JavaScript

export class Vec2 {
constructor(x = 0, y = 0) {
this.x = x
this.y = y
}
set(x,y) {
this.x = x
this.y = y
return this
}
clone() {
return new Vec2(this.x, this.y)
}
add(v) {
this.x += v.x
this.y += v.y
return this
}
sub(v) {
this.x -= v.x
this.y -= v.y
return this
}
copy(v) {
this.x = v.x
this.y = v.y
return this
}
multV(v) {
this.x *= v.x
this.y *= v.y
return this
}
multS(s) {
this.x *= s
this.y *= s
return this
}
divV(v) {
this.x *= (1 / v.x)
this.y *= (1 / v.y)
return this
}
divS(s) {
this.x *= (1 / s)
this.y *= (1 / s)
return this
}
invert() {
this.x = this.x == 0 ? 0 : (1 / this.x)
this.y = this.y == 0 ? 0 : (1 / this.y)
return this
}
normalize() {
return this.divS(this.length() || 1)
}
dot(v) {
return this.x * v.x + this.y * v.y;
}
length() {
return Math.sqrt(this.x * this.x + this.y * this.y);
}
}