All files Line.js

100% Statements 15/15
87.5% Branches 7/8
100% Functions 2/2
100% Lines 15/15

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 737x                                                       3182x         2x         2x   2x 2x 5x 5x 5x 1x       3180x 3176x   4x                       1x             7x  
const Point = require('./Point');
 
/**
 * Create a line object represnting a set of two points in 2D space.
 *
 * Line objects can be constructed by passing in either 4 numbers (startX, startY, endX, endY) - or
 * two {@link Point} objects representing `start` and `end` respectively
 *
 * @class Line
 */
class Line {
    /**
     * Construct a Line using two {@link Point} objects
     * .
     * @param {Point} start An instance of {@link Point} containing X and Y co-ordinates
     * @param {Point} end   An instance of {@link Point} containing X and Y co-ordinates
     * @memberof Line
     */
    /**
     * Construct a Line using 4 {@link number}s
     *
     * @param {number} startX Starting position on the X axis
     * @param {number} startY Starting position on the Y axis
     * @param {number} endX   Ending position on the X axis
     * @param {number} endY   Ending position on the Y acis
     * @memberof Line
     */
    constructor (){
        if (arguments.length === 4) {
 
            /**
             * @type {Point}
            */
            this.start = {};
 
            /**
             * @type {Point}
            */
            this.end   = {};
 
            [this.start.x, this.start.y, this.end.x, this.end.y] = arguments;
            for(let argument_index in arguments) {
                Eif(arguments.hasOwnProperty(argument_index)) {
                    let argument = arguments[argument_index];
                    if(typeof argument !== 'number'){
                        throw TypeError('When passing 4 arguments, only numbers may be passed');
                    }
                }
            }
        } else if(arguments.length === 2) {
            [this.start, this.end] = arguments;
        } else {
            throw Error('Please pass either two Point objects, or 4 integers to the constructor');
        }
    }
 
    /**
     * Get the line length
     *
     * @returns {number}
     *
     * @memberof Line
     */
    getLength() {
        return Math.sqrt(
            Math.pow(this.start.x - this.end.x, 2) + Math.pow(this.start.y - this.end.y, 2)
        );
    }
}
 
/** @ignore */
module.exports = Line;