All files Rectangle.js

100% Statements 84/84
100% Branches 154/154
100% Functions 2/2
100% Lines 83/83
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 3381x                                 30x               2x               2x               2x               2x               29x               33x               29x               23x               3x                     1182x   197x 197x 197x               6x 6x                 7x                 1x 1x 1x 1x 1x 1x               6x 6x 6x 6x               3x 3x 3x 3x               2x 2x 2x 2x                 8x 8x               8x 8x               5x 5x               5x 5x               22x 22x 22x 22x               12x 12x 12x 12x                 20x 10x 10x 10x                 6x 3x 3x 3x 3x                 48x 24x 24x                     32x 8x 8x 8x 8x                 40x 20x 20x                     48x 12x 12x 12x 12x                 12x 6x 6x 6x                 11x                   3x  
import {
    Point
} from './Point.js';
 
/**
 * @author Michael Duve <mduve@designmail.net>
 * @file represents a rectangle with a point as position, width and height
 * @extends Point
 * @copyright Michael Duve 2016
 */
export class Rectangle extends Point {
 
    /**
     * get center-position of rectangle
     * @return {Point} center point
     */
    get center() {
        return new Point(this.x + (this.width / 2), this.y + (this.height / 2));
    }
 
    /**
     * get top-left-position of rectangle
     * @return {Point} top-left point
     */
    get topLeft() {
        return new Point(this.x, this.y);
    }
 
    /**
     * get top-right-position of rectangle
     * @return {Point} top-right point
     */
    get topRight() {
        return new Point(this.x + this.width, this.y);
    }
 
    /**
     * get bottom-left-position of rectangle
     * @return {Point} bottom-left point
     */
    get bottomLeft() {
        return new Point(this.x, this.y + this.height);
    }
 
    /**
     * get bottom-right-position of rectangle
     * @return {Point} bottom-right point
     */
    get bottomRight() {
        return new Point(this.x + this.width, this.y + this.height);
    }
 
    /**
     * Returns right position of Rectangle
     * @return {Number} right position
     */
    get right() {
        return this.x + this.width;
    }
 
    /**
     * Returns left position of Rectangle
     * @return {Number} left position
     */
    get left() {
        return this.x;
    }
 
    /**
     * Returns top position of Rectangle
     * @return {Number} top position
     */
    get top() {
        return this.y;
    }
 
    /**
     * Returns bottom position of Rectangle
     * @return {Number} bottom position
     */
    get bottom() {
        return this.y + this.height;
    }
 
    /**
     * clones a rectangle
     * @return {Rectangle} duplicated rectangle
     */
    get clone() {
        return Rectangle.createFromRectangle(this);
    }
 
    /**
     * @constructor
     * @param  {Number} x = 0 - x-position of specified rectangle
     * @param  {Number} y = 0 - y-position of specified rectangle
     * @param  {Number} width = 0 - width of specified rectangle
     * @param  {Number} height = 0 - height of specified rectangle
     * @return {Rectangle} instance of Rectangle for chaining
     */
    constructor(x = 0, y = 0, width = 0, height = 0) {
        super(x, y);
        this.width = width;
        this.height = height;
        return this;
    }
 
    /**
     * Checks whether Rectangle intersects with specified Rectangle
     * @param  {Rectangle} rect = new Rectangle() - the specified rectangle to check against
     * @return {Boolean} true if containment is entirely
     */
    intersects(rect = new Rectangle()) {
        return !(rect.left > this.right || rect.right < this.left || rect.top > this.bottom || rect.bottom < this.top);
    }
 
    /**
     * Checks whether Rectangle entirely contains the Rectangle or Point
     * @param  {Rectangle|Point} rectOrPoint - the specified point or rectangle to check against
     * @return {Boolean} true if containment is entirely
     */
    contains(rectOrPoint) {
        return (rectOrPoint instanceof Rectangle) ? this.containsRect(rectOrPoint) : (rectOrPoint instanceof Point) ? this.containsPoint(rectOrPoint) : false;
    }
 
    /**
     * extend a rectangle by specified rectangle dimensions
     * @param  {Rectangle} rect - specified rectangle
     * @return {Rectangle} instance of Rectangle for chaining
     */
    extend(rect) {
        const left = Math.min(this.left, rect.left);
        const right = Math.max(this.right, rect.right);
        const top = Math.min(this.top, rect.top);
        const bottom = Math.max(this.bottom, rect.bottom);
        this.size(left, top, right - left, bottom - top);
        return this;
    }
 
    /**
     * Sets the center of this Rectangle to specified point
     * @param  {Point} point = new Point() - specified point to set center of rectangle to
     * @return {Rectangle} instance of Rectangle for chaining
     */
    setCenter(point = new Point()) {
        const difference = point.substract(this.center);
        this.translate(difference.x, difference.y);
        return this;
    }
 
    /**
     * Sets the x-center of this Rectangle to specified x
     * @param  {Number} x = 0 - specified x coordinate to set x center of rectangle to
     * @return {Rectangle} instance of Rectangle for chaining
     */
    setCenterX(x = 0) {
        const difference = x - this.center.x;
        this.translate(difference, 0);
        return this;
    }
 
    /**
     * Sets the y-center of this Rectangle to specified y
     * @param  {Number} y = 0 - specified y coordinate to set y center of rectangle to
     * @return {Rectangle} instance of Rectangle for chaining
     */
    setCenterY(y = 0) {
        const difference = y - this.center.y;
        this.translate(0, difference);
        return this;
    }
 
 
    /**
     * Checks whether Rectangle entirely contains the Point
     * @param  {Point} point = new Point() - the specified point to check against
     * @return {Boolean} true if containment is entirely
     */
    containsPoint(point = new Point()) {
        return (point instanceof Point) ? point.x >= this.left && point.y >= this.top && point.x <= this.right && point.y <= this.bottom : false;
    }
 
    /**
     * Checks whether Rectangle entirely contains the Rectangle
     * @param  {Rectangle} rect = new Rectangle() - the specified rectangle to check against
     * @return {Boolean} true if containment is entirely
     */
    containsRect(rect = new Rectangle()) {
        return (rect instanceof Rectangle) ? rect.left >= this.left && rect.top >= this.top && rect.right <= this.right && rect.bottom <= this.bottom : false;
    }
 
    /**
     * distorts rectangle by factor
     * @param  {Number} factor = 1 - the specified factor of distortion
     * @return {Rectangle} a distorted Rectangle
     */
    getDistortedRect(factor = 1) {
        return new Rectangle(this.x, this.y, this.width, this.height).scaleX(factor);
    }
 
    /**
     * redistorts rectangle by factor
     * @param  {Number} factor = 1- the specified factor of distortion
     * @return {Rectangle} an undistorted Rectangle
     */
    getNormalRect(factor = 1) {
        return new Rectangle(this.x, this.y, this.width, this.height).scaleX(1 / factor);
    }
 
    /**
     * scale x and width of rectangle
     * @param  {Number} x = 1 - factor to be applied to scale
     * @return {Rectangle} instance of Rectangle for chaining
     */
    scaleX(x = 1) {
        this.x *= x;
        this.width *= x;
        return this;
    }
 
    /**
     * scale y and height of rectangle
     * @param  {Number} y = 1- factor to be applied to scale
     * @return {Rectangle} instance of Rectangle for chaining
     */
    scaleY(y = 1) {
        this.y *= y;
        this.height *= y;
        return this;
    }
 
    /**
     * scale x and y for width and height of rectangle
     * @param  {Number} x = 1 - factor to be applied to scale
     * @param  {Number} y = x - factor to be applied to scale
     * @return {Rectangle} instance of Rectangle for chaining
     */
    scale(x = 1, y = x) {
        this.scaleX(x);
        this.scaleY(y);
        return this;
    }
 
    /**
     * scale x and y for width and height of rectangle
     * @param  {Number} x = 1 - factor to be applied to scale
     * @param  {Number} y = x - factor to be applied to scale
     * @return {Rectangle} instance of Rectangle for chaining
     */
    scaleCenter(x = 1, y = x) {
        const oldCenter = this.clone.center;
        this.scale(x, y);
        this.setCenter(oldCenter);
        return this;
    }
 
    /**
     * moves a rectangle by specified coords
     * @param  {Number} x = 0 - specified x to be added to x position
     * @param  {Number} y = x - specified y to be added to y position
     * @return {Rectangle} instance of Rectangle for chaining
     */
    translate(x = 0, y = x) {
        super.translate(x, y);
        return this;
    }
 
    /**
     * transforms a rectangle by specified coords
     * @param  {Number} x = 0 - specified x to be added to x position
     * @param  {Number} y = x - specified y to be added to y position
     * @param  {Number} width = 0 - specified width to be added to this width
     * @param  {Number} height = 0 - specified height to be added to this height
     * @return {Rectangle} instance of Rectangle for chaining
     */
    transform(x = 0, y = x, width = 0, height = width) {
        this.translate(x, y);
        this.width += width;
        this.height += height;
        return this;
    }
 
    /**
     * changes the position a rectangle by specified coords
     * @param  {Number} x = 0 - the new x position
     * @param  {Number} y = 0 - he new y position
     * @return {Rectangle} instance of Rectangle for chaining
     */
    position(x = 0, y = x) {
        super.position(x, y);
        return this;
    }
 
    /**
     * changes the size of a rectangle by specified params
     * @param  {Number} x = 0- the new x position
     * @param  {Number} y = x - the new y position
     * @param  {Number} width = 0 - the new width
     * @param  {Number} height = 0 - the new width
     * @return {Rectangle} instance of Rectangle for chaining
     */
    size(x = 0, y = x, width = 0, height = width) {
        this.position(x, y);
        this.width = width;
        this.height = height;
        return this;
    }
 
    /**
     * changes the size of a rectangle by specified params
     * @param  {Number} width = 0 - the new width
     * @param  {Number} height = width - the new width
     * @return {Rectangle} instance of Rectangle for chaining
     */
    setSize(width = 0, height = width) {
        this.width = width;
        this.height = height;
        return this;
    }
 
    /**
     * check if rectangles are equal
     * @param  {Rectangle} rectangle - the specified rectangle to check against this
     * @return {Boolean} is true, if x, y, width and height are the same
     */
    equals(rectangle) {
        return (rectangle instanceof Rectangle) ? this.x === rectangle.x && this.y === rectangle.y && this.width === rectangle.width && this.height === rectangle.height : false;
    }
 
}
 
/**
 * Creates a Rectangle from specified Rectangle
 * @param  {Rectangle} rect - specified Rectangle
 * @return {Rectangle} a copy of specified rectangle
 */
Rectangle.createFromRectangle = (rect) => new Rectangle(rect.x, rect.y, rect.width, rect.height);