all files / engine/ XYPair.js

100% Statements 14/14
100% Branches 6/6
100% Functions 3/3
100% Lines 14/14
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         13×                                                 12×   11× 11×          
const TOLERANCE = 0.0001;
 
/**
 * A basic xy pair
 */
class XYPair {
  constructor(x, y) {
    this.set(x, y);
  }
 
  /**
   * Compare to another XYPair for equivalene within the
   * provided tolerance
   *
   * @param  {XYPair} pair      - the XYPair to compare to
   * @param  {[type]} tolerance - the tolerance to check within, optional
   *                              will default to TOLERANCE
   *
   * @return {boolean}           - whether or not the two points are equal
   */
  equals(pair, tolerance) {
    var dx = Math.abs(this.x - pair.x);
    var dy = Math.abs(this.y - pair.y);
 
    tolerance = tolerance || TOLERANCE;
 
    return dx < tolerance && dy < tolerance;
  }
 
  /**
   * Set with either an x and y value or an existing XYPair
   *
   * @param {number|XYPair} x - the x value, or the XYPair to copy
   * @param {number} y        - the y value, optional if using an XYPair for x value
   */
  set(x, y) {
    if (x.x) {
      this.x = x.x;
      this.y = x.y;
    } else {
      this.x = x;
      this.y = y;
    }
  }
}
 
export default XYPair;