const CONST = require('../../../const');
/**
* the Rectangle object is an area defined by its position, as indicated by its top-left corner point (x, y) and by its width and its height.
*
* @class
*/
class Rectangle {
/**
* @constructor
* @param {number} x The X coordinate of the upper-left corner of the rectangle
* @param {number} y The Y coordinate of the upper-left corner of the rectangle
* @param {number} width The overall width of this rectangle
* @param {number} height The overall height of this rectangle
*/
constructor(x, y, width, height) {
/**
* @member {number}
* @default 0
*/
this.x = x || 0;
/**
* @member {number}
* @default 0
*/
this.y = y || 0;
/**
* @member {number}
* @default 0
*/
this.width = width || 0;
/**
* @member {number}
* @default 0
*/
this.height = height || 0;
/**
* The type of the object, mainly used to avoid `instanceof` checks
*
* @member {number}
*/
this.type = CONST.SHAPES.RECT;
}
/**
* Creates a clone of this Rectangle
*
* @return {Rectangle} a copy of the rectangle
*/
clone() {
return new Rectangle(this.x, this.y, this.width, this.height);
}
/**
* Checks whether the x and y coordinates given are contained within this Rectangle
*
* @param {number} x The X coordinate of the point to test
* @param {number} y The Y coordinate of the point to test
* @return {boolean} Whether the x/y coordinates are within this Rectangle
*/
contains(x, y) {
if (this.width <= 0 || this.height <= 0) {
return false;
}
if (x >= this.x && x < this.x + this.width) {
if (y >= this.y && y < this.y + this.height) {
return true;
}
}
return false;
}
}
/**
* A constant empty rectangle.
*
* @static
* @constant
*/
Rectangle.EMPTY = new Rectangle(0, 0, 0, 0);
module.exports = Rectangle;