All files AbstractVector.ts

91.15% Statements 103/113
87.5% Branches 7/8
94.44% Functions 34/36
90.91% Lines 100/110

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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350        1x                                                                       1x 114x                         1x 3x 3x 3x           1x 90x           1x 3x   3x           1x 90x           1x 3x   3x           9x 6x 3x   3x           1x 3x           1x 3x                 1x 6x 6x   6x           1x 3x 3x   3x           1x 24x           1x 3x 3x   3x           1x 3x           1x 3x 3x 3x           1x 3x           1x 3x 3x   3x           1x 3x           1x 12x 12x 12x           1x 3x           1x 9x           1x 3x           1x 3x           1x 24x 24x   24x           1x 3x           1x                   1x 3x           1x 3x           1x 3x 3x 3x           1x 3x 3x   3x           1x 3x   3x           1x 3x 3x   3x           1x                               42x 21x       21x 21x   21x           1x 3x   1x  
/**
 * These values are used by the `AbstractVector.round` method to increase
 * performance vs. using  Number.toFixed.
 */
const precision = [
  1,
  10,
  100,
  1000,
  10000,
  100000,
  1000000,
  10000000,
  100000000,
  1000000000,
  10000000000
]
 
export interface VectorConstructable<T> {
  new (x: number, y: number): T
}
 
/**
 * The class that all other vector representations are derived from.
 *
 * Contains the core implementation for all methods that will be exposed by
 * vector instances.
 *
 * Example of creating a custom implementation:
 *
 * ```ts
 * import { AbstractVector } from "./AbstractVector"
 *
 * export class MyCustomVector extends AbstractVector {
 *  constructor (x: number, y: number) {
 *    super(CustomVectorType)
 *  }
 * }
 * ```
 */
export abstract class AbstractVector {
  constructor(protected ctor: VectorConstructable<AbstractVector>) {}
 
  abstract set x(x: number)
  abstract get x(): number
 
  abstract set y(y: number)
  abstract get y(): number
 
  /**
   * Set both x and y axis value
   * @param x   New x val
   * @param y   New y val
   */
  setAxes(x: number, y: number) {
    this.x = x
    this.y = y
    return this
  }
 
  /**
   * Getter for x the axis value
   */
  getX() {
    return this.x
  }
 
  /**
   * Setter for x axis value
   */
  setX(x: number) {
    this.x = x
 
    return this
  }
 
  /**
   * Getter for y axis value
   */
  getY() {
    return this.y
  }
 
  /**
   * Setter for y axis.
   */
  setY(y: number) {
    this.y = y
 
    return this
  }
 
  /**
   * Return the vector as a formatted string, e.g "(0, 4)"
   */
  toString(round = false) {
    if (round) {
      return `(${Math.round(this.x)}, ${Math.round(this.y)})`
    }
    return `(${this.x}, ${this.y})`
  }
 
  /**
   * Return an Array containing the vector axes, e.g [0, 4]
   */
  toArray() {
    return [this.x, this.y]
  }
 
  /**
   * Return an Object containing the vector axes, e.g { x: 0, y: 4 }
   */
  toObject() {
    return {
      x: this.x,
      y: this.y
    }
  }
 
  /**
   * Add the provided vector to this one
   */
  add(vec: AbstractVector) {
    this.x += vec.x
    this.y += vec.y
 
    return this
  }
 
  /**
   * Subtract the provided vector from this one
   */
  subtract(vec: AbstractVector) {
    this.x -= vec.x
    this.y -= vec.y
 
    return this
  }
 
  /**
   * Check if the provided vector equal to this one
   */
  equals(vec: AbstractVector) {
    return vec.x === this.x && vec.y === this.y
  }
 
  /**
   * Multiply this vector by the provided vector
   */
  multiplyByVector(vec: AbstractVector) {
    this.x *= vec.x
    this.y *= vec.y
 
    return this
  }
 
  /**
   * Multiply this vector by the provided vector
   */
  mulV(vec: AbstractVector) {
    return this.multiplyByVector(vec)
  }
 
  /**
   * Divide this vector by the provided vector
   */
  divideByVector(vec: AbstractVector) {
    this.x /= vec.x
    this.y /= vec.y
    return this
  }
 
  /**
   * Divide this vector by the provided vector
   */
  divV(v: AbstractVector) {
    return this.divideByVector(v)
  }
 
  /**
   * Multiply this vector by the provided number
   */
  multiplyByScalar(n: number) {
    this.x *= n
    this.y *= n
 
    return this
  }
 
  /**
   * Multiply this vector by the provided number
   */
  mulS(n: number) {
    return this.multiplyByScalar(n)
  }
 
  /**
   * Divive this vector by the provided number
   */
  divideByScalar(n: number) {
    this.x /= n
    this.y /= n
    return this
  }
 
  /**
   * Divive this vector by the provided number
   */
  divS(n: number) {
    return this.divideByScalar(n)
  }
 
  /**
   * Normalise this vector
   */
  normalise() {
    return this.divideByScalar(this.magnitude())
  }
 
  /**
   * For American spelling. Same as unit/normalise function
   */
  normalize() {
    return this.normalise()
  }
 
  /**
   * The same as normalise and normalize
   */
  unit() {
    return this.normalise()
  }
 
  /**
   * Returns the magnitude (length) of this vector
   */
  magnitude() {
    const x = this.x
    const y = this.y
 
    return Math.sqrt(x * x + y * y)
  }
 
  /**
   * Returns the magnitude (length) of this vector
   */
  length() {
    return this.magnitude()
  }
 
  /**
   * Returns the squred length of this vector
   */
  lengthSq() {
    const x = this.x
    const y = this.y
 
    return x * x + y * y
  }
 
  /**
   * Returns the dot product of this vector by another
   */
  dot(vec: AbstractVector) {
    return vec.x * this.x + vec.y * this.y
  }
 
  /**
   * Returns the cross product of this vector by another.
   */
  cross(vec: AbstractVector) {
    return this.x * vec.y - this.y * vec.x
  }
 
  /**
   * Reverses this vector i.e multiplies it by -1
   */
  reverse() {
    this.x = -this.x
    this.y = -this.y
    return this
  }
 
  /**
   * Set the vector axes values to absolute values
   */
  abs() {
    this.x = Math.abs(this.x)
    this.y = Math.abs(this.y)
 
    return this
  }
 
  /**
   * Zeroes the vector i.e sets all axes to 0
   */
  zero() {
    this.x = this.y = 0
 
    return this
  }
 
  /**
   * Returns the distance between this vector and another
   */
  distance(v: AbstractVector) {
    var x = this.x - v.x
    var y = this.y - v.y
 
    return Math.sqrt(x * x + y * y)
  }
 
  /**
   * Rotates the vetor by provided radians
   */
  rotate(rads: number) {
    const cos = Math.cos(rads)
    const sin = Math.sin(rads)
 
    const ox = this.x
    const oy = this.y
 
    this.x = ox * cos - oy * sin
    this.y = ox * sin + oy * cos
 
    return this
  }
 
  /**
   * Rounds this vector to n decimal places
   */
  round(En = 2) {
    var p = precision[n]
 
    // This performs waaay better than toFixed and give Float32 the edge again.
    // http://www.dynamicguru.com/javascript/round-numbers-with-precision/
    this.x = ((0.5 + this.x * p) << 0) / p
    this.y = ((0.5 + this.y * p) << 0) / p
 
    return this
  }
 
  /**
   * Returns a copy of this vector
   */
  clone() {
    return new this.ctor(this.x, this.y)
  }
}