diff options
Diffstat (limited to 'src/V2.ts')
| -rw-r--r-- | src/V2.ts | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/V2.ts b/src/V2.ts new file mode 100644 index 0000000..5ef60b3 --- /dev/null +++ b/src/V2.ts @@ -0,0 +1,38 @@ +export default class V2 { + readonly x: number; + readonly y: number; + + constructor(x: number, y: number) { + this.x = x; + this.y = y; + } + + static get zero() { + return new V2(0, 0); + } + + toString() { + return `(${this.x}, ${this.y})`; + } + + *[Symbol.iterator]() { + yield this.x; + yield this.y; + } + + scale(factor: number) { + return new V2(this.x * factor, this.y * factor); + } + + get neg() { + return this.scale(-1); + } + + add(other: V2) { + return new V2(this.x + other.x, this.y + other.y); + } + + sub(other: V2) { + return this.add(other.neg); + } +} |
