2017-06-30 14:47:51 +00:00
|
|
|
var Class = require('../../utils/Class');
|
2017-10-29 21:46:23 +00:00
|
|
|
var Contains = require('./Contains');
|
2017-10-26 15:39:15 +00:00
|
|
|
var GetPoint = require('./GetPoint');
|
|
|
|
var GetPoints = require('./GetPoints');
|
2017-09-25 17:10:01 +00:00
|
|
|
var Random = require('./Random');
|
2016-12-20 17:07:16 +00:00
|
|
|
|
|
|
|
// Encapsulates a 2D rectangle defined by its corner point in the top-left
|
|
|
|
// and its extends in x (width) and y (height)
|
|
|
|
|
2017-06-30 14:47:51 +00:00
|
|
|
var Rectangle = new Class({
|
2016-12-20 17:07:16 +00:00
|
|
|
|
2017-06-30 14:47:51 +00:00
|
|
|
initialize:
|
2016-12-20 17:07:16 +00:00
|
|
|
|
2017-06-30 14:47:51 +00:00
|
|
|
function Rectangle (x, y, width, height)
|
|
|
|
{
|
|
|
|
if (x === undefined) { x = 0; }
|
|
|
|
if (y === undefined) { y = 0; }
|
|
|
|
if (width === undefined) { width = 0; }
|
|
|
|
if (height === undefined) { height = 0; }
|
2016-12-20 17:07:16 +00:00
|
|
|
|
2017-06-30 14:47:51 +00:00
|
|
|
this.x = x;
|
2016-12-20 17:07:16 +00:00
|
|
|
|
2017-06-30 14:47:51 +00:00
|
|
|
this.y = y;
|
2016-12-20 17:07:16 +00:00
|
|
|
|
2017-06-30 14:47:51 +00:00
|
|
|
this.width = width;
|
2016-12-20 17:07:16 +00:00
|
|
|
|
2017-06-30 14:47:51 +00:00
|
|
|
this.height = height;
|
|
|
|
},
|
2016-12-20 17:07:16 +00:00
|
|
|
|
2017-10-29 21:46:23 +00:00
|
|
|
contains: function (x, y)
|
|
|
|
{
|
|
|
|
return Contains(this, x, y);
|
|
|
|
},
|
|
|
|
|
2017-10-26 15:39:15 +00:00
|
|
|
getPoint: function (position, output)
|
|
|
|
{
|
|
|
|
return GetPoint(this, position, output);
|
|
|
|
},
|
|
|
|
|
|
|
|
getPoints: function (quantity, stepRate, output)
|
|
|
|
{
|
|
|
|
return GetPoints(this, quantity, stepRate, output);
|
|
|
|
},
|
|
|
|
|
2017-09-25 17:10:01 +00:00
|
|
|
getRandomPoint: function (point)
|
|
|
|
{
|
|
|
|
return Random(this, point);
|
|
|
|
},
|
|
|
|
|
2016-12-28 23:41:02 +00:00
|
|
|
setTo: function (x, y, width, height)
|
2016-12-22 01:32:21 +00:00
|
|
|
{
|
|
|
|
this.x = x;
|
|
|
|
this.y = y;
|
|
|
|
this.width = width;
|
|
|
|
this.height = height;
|
2016-12-20 17:07:16 +00:00
|
|
|
|
2016-12-22 01:32:21 +00:00
|
|
|
return this;
|
|
|
|
},
|
2016-12-20 17:07:16 +00:00
|
|
|
|
2016-12-28 23:41:02 +00:00
|
|
|
setEmpty: function ()
|
|
|
|
{
|
|
|
|
return this.setTo(0, 0, 0, 0);
|
|
|
|
},
|
|
|
|
|
2016-12-22 01:32:21 +00:00
|
|
|
setPosition: function (x, y)
|
|
|
|
{
|
|
|
|
if (y === undefined) { y = x; }
|
2016-12-20 17:07:16 +00:00
|
|
|
|
2016-12-22 01:32:21 +00:00
|
|
|
this.x = x;
|
|
|
|
this.y = y;
|
2016-12-20 17:07:16 +00:00
|
|
|
|
2016-12-22 01:32:21 +00:00
|
|
|
return this;
|
|
|
|
},
|
2016-12-20 17:07:16 +00:00
|
|
|
|
2016-12-22 01:32:21 +00:00
|
|
|
setSize: function (width, height)
|
|
|
|
{
|
|
|
|
if (height === undefined) { height = width; }
|
2016-12-20 17:07:16 +00:00
|
|
|
|
2016-12-22 01:32:21 +00:00
|
|
|
this.width = width;
|
|
|
|
this.height = height;
|
2016-12-20 17:07:16 +00:00
|
|
|
|
2016-12-22 01:32:21 +00:00
|
|
|
return this;
|
2016-12-29 00:17:20 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
isEmpty: function ()
|
|
|
|
{
|
|
|
|
return (this.width <= 0 || this.height <= 0);
|
2017-04-04 00:04:05 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
// TOP
|
|
|
|
getLineA: function ()
|
|
|
|
{
|
|
|
|
return { x1: this.x, y1: this.y, x2: this.right, y2: this.y };
|
|
|
|
},
|
|
|
|
|
|
|
|
// RIGHT
|
|
|
|
getLineB: function ()
|
|
|
|
{
|
|
|
|
return { x1: this.right, y1: this.y, x2: this.right, y2: this.bottom };
|
|
|
|
},
|
|
|
|
|
|
|
|
// BOTTOM
|
|
|
|
getLineC: function ()
|
|
|
|
{
|
|
|
|
return { x1: this.right, y1: this.bottom, x2: this.x, y2: this.bottom };
|
|
|
|
},
|
|
|
|
|
|
|
|
// LEFT
|
|
|
|
getLineD: function ()
|
|
|
|
{
|
|
|
|
return { x1: this.x, y1: this.bottom, x2: this.x, y2: this.y };
|
2017-06-30 14:47:51 +00:00
|
|
|
},
|
2016-12-20 17:07:16 +00:00
|
|
|
|
2016-12-22 01:32:21 +00:00
|
|
|
left: {
|
2016-12-20 17:07:16 +00:00
|
|
|
|
2016-12-22 01:32:21 +00:00
|
|
|
get: function ()
|
|
|
|
{
|
|
|
|
return this.x;
|
2016-12-20 17:07:16 +00:00
|
|
|
},
|
|
|
|
|
2016-12-22 01:32:21 +00:00
|
|
|
set: function (value)
|
|
|
|
{
|
|
|
|
if (value >= this.right)
|
|
|
|
{
|
|
|
|
this.width = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.width = this.right - value;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.x = value;
|
|
|
|
}
|
2016-12-20 17:07:16 +00:00
|
|
|
|
2016-12-22 01:32:21 +00:00
|
|
|
},
|
2016-12-20 17:07:16 +00:00
|
|
|
|
2016-12-22 01:32:21 +00:00
|
|
|
right: {
|
2016-12-20 17:07:16 +00:00
|
|
|
|
2016-12-22 01:32:21 +00:00
|
|
|
get: function ()
|
|
|
|
{
|
2016-12-20 17:07:16 +00:00
|
|
|
return this.x + this.width;
|
|
|
|
},
|
|
|
|
|
2016-12-22 01:32:21 +00:00
|
|
|
set: function (value)
|
|
|
|
{
|
|
|
|
if (value <= this.x)
|
|
|
|
{
|
|
|
|
this.width = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.width = value - this.x;
|
|
|
|
}
|
|
|
|
}
|
2016-12-20 17:07:16 +00:00
|
|
|
|
2016-12-22 01:32:21 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
top: {
|
|
|
|
|
|
|
|
get: function ()
|
|
|
|
{
|
|
|
|
return this.y;
|
2016-12-20 17:07:16 +00:00
|
|
|
},
|
|
|
|
|
2016-12-22 01:32:21 +00:00
|
|
|
set: function (value)
|
|
|
|
{
|
|
|
|
if (value >= this.bottom)
|
|
|
|
{
|
|
|
|
this.height = 0;
|
|
|
|
this.y = value;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.height = (this.bottom - value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
bottom: {
|
|
|
|
|
|
|
|
get: function ()
|
|
|
|
{
|
2016-12-20 17:07:16 +00:00
|
|
|
return this.y + this.height;
|
2016-12-22 01:32:21 +00:00
|
|
|
},
|
2016-12-20 17:07:16 +00:00
|
|
|
|
2016-12-22 01:32:21 +00:00
|
|
|
set: function (value)
|
|
|
|
{
|
|
|
|
if (value <= this.y)
|
|
|
|
{
|
|
|
|
this.height = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.height = value - this.y;
|
|
|
|
}
|
2016-12-20 17:07:16 +00:00
|
|
|
}
|
|
|
|
|
2017-03-29 14:05:06 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
centerX: {
|
|
|
|
|
|
|
|
get: function ()
|
|
|
|
{
|
|
|
|
return this.x + (this.width / 2);
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value)
|
|
|
|
{
|
|
|
|
this.x = value - (this.width / 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
centerY: {
|
|
|
|
|
|
|
|
get: function ()
|
|
|
|
{
|
|
|
|
return this.y + (this.height / 2);
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value)
|
|
|
|
{
|
|
|
|
this.y = value - (this.height / 2);
|
|
|
|
}
|
|
|
|
|
2016-12-20 17:07:16 +00:00
|
|
|
}
|
|
|
|
|
2016-12-22 01:32:21 +00:00
|
|
|
});
|
2016-12-20 17:07:16 +00:00
|
|
|
|
|
|
|
module.exports = Rectangle;
|