All files Vec2.ts

61.53% Statements 8/13
100% Branches 2/2
50% Functions 5/10
61.53% Lines 8/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54              1x         1839379x 1839379x       11135x               59x       120407x               28x 28x                              
/**
 * @author Timo Lehnertz
 */
 
/**
 * Immutable Vec2 class
 */
export class Vec2 {
  public readonly x: number;
  public readonly y: number;
 
  public constructor(x: number, y: number) {
    this.x = x;
    this.y = y;
  }
 
  public equals(other: Vec2): boolean {
    return this.x === other.x && this.y === other.y;
  }
 
  public add(b: Vec2): Vec2 {
    return new Vec2(this.x + b.x, this.y + b.y);
  }
 
  public subtract(b: Vec2): Vec2 {
    return new Vec2(this.x - b.x, this.y - b.y);
  }
 
  public multiply(scalar: number): Vec2 {
    return new Vec2(this.x * scalar, this.y * scalar);
  }
 
  public distanceFrom(other: Vec2): number {
    return this.subtract(other).length;
  }
 
  public manhattanDistanceFrom(other: Vec2): number {
    const diff = this.subtract(other);
    return Math.abs(diff.x) + Math.abs(diff.y);
  }
 
  public get length(): number {
    return Math.sqrt(this.x * this.x + this.y * this.y);
  }
 
  public setX(x: number): Vec2 {
    return new Vec2(x, this.y);
  }
 
  public setY(y: number): Vec2 {
    return new Vec2(this.x, y);
  }
}