32 lines
693 B
JavaScript
32 lines
693 B
JavaScript
export class Vector {
|
|
constructor(x, y) {
|
|
this.x = x;
|
|
this.y = y;
|
|
}
|
|
|
|
normalize() {
|
|
let length = this.getLength();
|
|
return new Vector(this.x / length, this.y / length);
|
|
}
|
|
|
|
getLength() {
|
|
return Math.sqrt(this.x * this.x + this.y * this.y);
|
|
}
|
|
|
|
getDistance(target) {
|
|
let v = new Vector(target.x - this.x, target.y - this.y);
|
|
return v.getLength();
|
|
}
|
|
|
|
dotProduct(vector) {
|
|
return this.x * vector.x + this.y * vector.y;
|
|
}
|
|
|
|
getAngle(vector) {
|
|
return this.dotProduct(vector) / (this.getLength() * vector.getLength())
|
|
}
|
|
|
|
copy() {
|
|
return new Vector(this.x, this.y);
|
|
}
|
|
} |