suzanne
Version:
3D Software Renderer
46 lines (45 loc) • 1.12 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Vector_1 = require("./Vector");
class Vec2 {
constructor(x = 0, y = 0) {
this.x = x;
this.y = y;
this.dims = 2;
}
add(v) {
return new Vec2(this.x + v.x, this.y + v.y);
}
sub(v) {
return new Vec2(this.x - v.x, this.y - v.y);
}
mul(v) {
return new Vec2(this.x * v.x, this.y * v.y);
}
times(k) {
return new Vec2(this.x * k, this.y * k);
}
div(v) {
return new Vec2(this.x / v.x, this.y / v.y);
}
dot(v) {
return this.x * v.x + this.y * v.y;
}
mag() {
return Math.sqrt(Math.pow(this.x, 2) + Math.pow(this.y, 2));
}
normalize() {
return this.times(1 / this.mag());
}
reflect(N) {
return N.times(2 * this.dot(N)).sub(this);
}
addTimes(v, k) {
return new Vec2(this.x + v.x * k, this.y + v.y * k);
}
equals(v, eps = Vector_1.EPS) {
return Math.abs(this.x - v.x) < eps &&
Math.abs(this.y - v.y) < eps;
}
}
exports.Vec2 = Vec2;