All files Point.js

100% Statements 37/37
100% Branches 55/55
100% Functions 2/2
100% Lines 36/36
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                        7x               7x               8x                 1137x 379x 379x 379x               14x 14x 14x 14x               3x 3x 3x 3x                 8x 4x 4x 4x                 18x 9x 9x 9x                 3x               5x 5x                 56x 28x 28x 28x                 48x 24x 24x 24x               1x                   7x  
/**
 * @author Michael Duve <mduve@designmail.net>
 * @file represents a point with x and y value
 * @copyright Michael Duve 2016
 */
export class Point {
 
    /**
     * length of a point
     * @return {Number} length of a point
     */
    get length() {
        return Math.sqrt(Math.pow(this.x, 2) + Math.pow(this.y, 2));
    }
 
    /**
     * gets a clone of this point
     * @return {Point} new instance equals this point
     */
    get clone() {
        return Point.createFromPoint(this);
    }
 
    /**
     * gets absolute Point
     * @return {Point} returns Point with absolute values
     */
    get abs() {
        return new Point(Math.abs(this.x), Math.abs(this.y));
    }
 
    /**
     * @constructor
     * @param  {Number} x = 0 - representation of x coordinate
     * @param  {Number} y = 0 - representation of y coordinate
     * @return {Point} instance of Point for chaining
     */
    constructor(x = 0, y = 0) {
        this.x = x;
        this.y = y;
        return this;
    }
 
    /**
     * substracts 2 points
     * @param  {Point} point = new Point() - the point to substract from this
     * @return {Point} instance of Point for chaining
     */
    substract(point = new Point()) {
        this.x -= point.x;
        this.y -= point.y;
        return this;
    }
 
    /**
     * adds 2 points
     * @param  {Point} point = new Point() - the point to add to this
     * @return {Point} instance of Point for chaining
     */
    add(point = new Point()) {
        this.x += point.x;
        this.y += point.y;
        return this;
    }
 
    /**
     * multiplicates a point with a given x and y
     * @param  {Number} x = 1 - factor to multiplicate x with
     * @param  {Number} y = x - factor to multiplicate y with
     * @return {Point} instance of Point for chaining
     */
    multiply(x = 1, y = x) {
        this.x *= x;
        this.y *= y;
        return this;
    }
 
    /**
     * divide a point with a given x and y
     * @param  {Number} x = 1 - factor to divide x with
     * @param  {Number} y = x - factor to divide y with
     * @return {Point} instance of Point for chaining
     */
    divide(x = 1, y = x) {
        this.x /= x;
        this.y /= y;
        return this;
    }
 
    /**
     * check if points are equal
     * @param  {Point} point - the point to check against this
     * @return {Boolean} is true, if x and y are the same
     */
    equals(point) {
        return (point instanceof Point) && this.x === point.x && this.y === point.y;
    }
 
    /**
     * Returns the distance from this Point to a specified Point
     * @param  {Point} point = new Point() - the specified point to be measured against this Point
     * @return {Point} the distance between this Point and specified point
     */
    distance(point = new Point()) {
        return this.clone.substract(point).length;
    }
 
    /**
     * translates a point by x and y
     * @param  {Number} x = 0 - value to move x
     * @param  {Number} y = x - value to move y
     * @return {Point} instance of Point for chaining
     */
    translate(x = 0, y = x) {
        this.x += x;
        this.y += y;
        return this;
    }
 
    /**
     * positions a point by x and y
     * @param  {Number} x = 0 - value to position x
     * @param  {Number} y = x - value to position y
     * @return {Point} instance of Point for chaining
     */
    position(x = 0, y = x) {
        this.x = x;
        this.y = y;
        return this;
    }
 
    /**
     * translates a Point to an array
     * @return {Array} Returns Point as Array(x, y)
     */
    toArray() {
        return [this.x, this.y];
    }
 
}
 
/**
 * Creates a Point from specified point
 * @param  {Point} point - specified point
 * @return {Point} the point specified
 */
Point.createFromPoint = (point) => new Point(point.x, point.y);