summaryrefslogtreecommitdiff
path: root/src/V2.ts
blob: 5ef60b3ce597119720fe4d4ec31b6bdc6de74f58 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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);
  }
}