|
| 1 | +/** |
| 2 | + * @file A collection of utility classes. |
| 3 | + * @author Una Ada <una@anarchy.website> |
| 4 | + * @version 2021.06.02 |
| 5 | + */ |
| 6 | + |
| 7 | +/** A single point in cartesian coordinated. */ |
| 8 | +export class Point { |
| 9 | + /** |
| 10 | + * Create a single point. |
| 11 | + * @param {number} x - The point's x coordinate. |
| 12 | + * @param {number} y - The point's y coordinate. |
| 13 | + */ |
| 14 | + constructor(x, y) { |
| 15 | + /** @var {number} x - The point's x coordinate. */ |
| 16 | + this.x = x; |
| 17 | + /** @var {number} y - The point's y coordinate. */ |
| 18 | + this.y = y; |
| 19 | + } |
| 20 | +} |
| 21 | +/** A 2-dimensional vector in cartesian coordinated. */ |
| 22 | +export class Vector extends Point { |
| 23 | + /** |
| 24 | + * Create a 2-dimensional vector. |
| 25 | + * @param {number} x - The vector's x length. |
| 26 | + * @param {number} y - The vector's y length. |
| 27 | + */ |
| 28 | + constructor(x, y) { |
| 29 | + super(x, y); |
| 30 | + } |
| 31 | + |
| 32 | + /*----- Getters and setters ------------------------------------------------*/ |
| 33 | + /** @type {number} */ |
| 34 | + get magnitude() { |
| 35 | + return Math.sqrt(this.x ** 2 + this.y ** 2); |
| 36 | + } |
| 37 | + set magnitude(magnitude) { |
| 38 | + let direction = this.direction; |
| 39 | + this.x = magnitude * Math.cos(direction); |
| 40 | + this.y = magnitude * Math.sin(direction); |
| 41 | + } |
| 42 | + /** @type {number} */ |
| 43 | + get direction() { |
| 44 | + /** @TODO */ |
| 45 | + } |
| 46 | + set direction(direction) { |
| 47 | + let magnitude = this.magnitude; |
| 48 | + // Keep direction within [-pi, pi] |
| 49 | + if(Math.abs(direction) > Math.PI) direction %= Math.PI; |
| 50 | + this.x = magnitude * Math.cos(direction); |
| 51 | + this.y = magnitude * Math.sin(direction); |
| 52 | + } |
| 53 | +} |
0 commit comments