2017-09-29 17:54:51 +00:00
|
|
|
var Rectangle = require('../../geom/rectangle/Rectangle');
|
2017-11-01 23:56:36 +00:00
|
|
|
var RotateAround = require('../../math/RotateAround');
|
|
|
|
var Vector2 = require('../../math/Vector2');
|
2017-09-29 17:54:51 +00:00
|
|
|
|
2017-02-23 17:15:41 +00:00
|
|
|
var GetBounds = {
|
2016-12-07 01:40:56 +00:00
|
|
|
|
2017-11-11 03:51:28 +00:00
|
|
|
getCenter: function (output)
|
|
|
|
{
|
|
|
|
if (output === undefined) { output = new Vector2(); }
|
|
|
|
|
|
|
|
output.x = this.x - (this.displayWidth * this.originX) + (this.displayWidth / 2);
|
|
|
|
output.y = this.y - (this.displayHeight * this.originY) + (this.displayHeight / 2);
|
|
|
|
|
|
|
|
return output;
|
|
|
|
},
|
|
|
|
|
2017-11-01 23:56:36 +00:00
|
|
|
getTopLeft: function (output)
|
|
|
|
{
|
|
|
|
if (output === undefined) { output = new Vector2(); }
|
|
|
|
|
|
|
|
output.x = this.x - (this.displayWidth * this.originX);
|
|
|
|
output.y = this.y - (this.displayHeight * this.originY);
|
|
|
|
|
|
|
|
if (this.rotation !== 0)
|
|
|
|
{
|
|
|
|
RotateAround(output, this.x, this.y, this.rotation);
|
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
},
|
|
|
|
|
|
|
|
getTopRight: function (output)
|
|
|
|
{
|
|
|
|
if (output === undefined) { output = new Vector2(); }
|
|
|
|
|
|
|
|
output.x = (this.x - (this.displayWidth * this.originX)) + this.displayWidth;
|
|
|
|
output.y = this.y - (this.displayHeight * this.originY);
|
|
|
|
|
|
|
|
if (this.rotation !== 0)
|
|
|
|
{
|
|
|
|
RotateAround(output, this.x, this.y, this.rotation);
|
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
},
|
|
|
|
|
|
|
|
getBottomLeft: function (output)
|
|
|
|
{
|
|
|
|
if (output === undefined) { output = new Vector2(); }
|
|
|
|
|
|
|
|
output.x = this.x - (this.displayWidth * this.originX);
|
|
|
|
output.y = (this.y - (this.displayHeight * this.originY)) + this.displayHeight;
|
|
|
|
|
|
|
|
if (this.rotation !== 0)
|
|
|
|
{
|
|
|
|
RotateAround(output, this.x, this.y, this.rotation);
|
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
},
|
|
|
|
|
|
|
|
getBottomRight: function (output)
|
|
|
|
{
|
|
|
|
if (output === undefined) { output = new Vector2(); }
|
|
|
|
|
|
|
|
output.x = (this.x - (this.displayWidth * this.originX)) + this.displayWidth;
|
|
|
|
output.y = (this.y - (this.displayHeight * this.originY)) + this.displayHeight;
|
|
|
|
|
|
|
|
if (this.rotation !== 0)
|
|
|
|
{
|
|
|
|
RotateAround(output, this.x, this.y, this.rotation);
|
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
},
|
|
|
|
|
2017-02-24 01:58:27 +00:00
|
|
|
getBounds: function (output)
|
2016-12-07 01:40:56 +00:00
|
|
|
{
|
2017-09-29 17:54:51 +00:00
|
|
|
if (output === undefined) { output = new Rectangle(); }
|
2016-12-07 01:40:56 +00:00
|
|
|
|
2017-11-02 00:30:04 +00:00
|
|
|
// We can use the output object to temporarily store the x/y coords in:
|
2017-11-01 23:56:36 +00:00
|
|
|
|
2017-11-02 00:30:04 +00:00
|
|
|
this.getTopLeft(output);
|
2017-11-01 23:56:36 +00:00
|
|
|
|
2017-11-02 00:30:04 +00:00
|
|
|
var TLx = output.x;
|
|
|
|
var TLy = output.y;
|
|
|
|
|
|
|
|
this.getTopRight(output);
|
|
|
|
|
|
|
|
var TRx = output.x;
|
|
|
|
var TRy = output.y;
|
|
|
|
|
|
|
|
this.getBottomLeft(output);
|
|
|
|
|
|
|
|
var BLx = output.x;
|
|
|
|
var BLy = output.y;
|
|
|
|
|
|
|
|
this.getBottomRight(output);
|
|
|
|
|
|
|
|
var BRx = output.x;
|
|
|
|
var BRy = output.y;
|
|
|
|
|
|
|
|
output.x = Math.min(TLx, TRx, BLx, BRx);
|
|
|
|
output.y = Math.min(TLy, TRy, BLy, BRy);
|
|
|
|
output.width = Math.max(TLx, TRx, BLx, BRx) - output.x;
|
|
|
|
output.height = Math.max(TLy, TRy, BLy, BRy) - output.y;
|
|
|
|
|
|
|
|
return output;
|
|
|
|
},
|
|
|
|
|
|
|
|
// Works but only if originX/Y = 0
|
|
|
|
|
|
|
|
/*
|
|
|
|
getBounds: function (output)
|
|
|
|
{
|
|
|
|
if (output === undefined) { output = new Rectangle(); }
|
2017-11-01 23:56:36 +00:00
|
|
|
|
|
|
|
var x = this.x;
|
|
|
|
var y = this.y;
|
2016-12-07 01:40:56 +00:00
|
|
|
|
2017-03-02 04:00:39 +00:00
|
|
|
var w = this.displayWidth;
|
|
|
|
var h = this.displayHeight;
|
2017-02-24 01:58:27 +00:00
|
|
|
|
|
|
|
var r = this.rotation;
|
|
|
|
|
|
|
|
var wct = w * Math.cos(r);
|
|
|
|
var hct = h * Math.cos(r);
|
|
|
|
|
|
|
|
var wst = w * Math.sin(r);
|
|
|
|
var hst = h * Math.sin(r);
|
|
|
|
|
2017-02-24 01:45:15 +00:00
|
|
|
var xMin = x;
|
|
|
|
var xMax = x;
|
|
|
|
var yMin = y;
|
|
|
|
var yMax = y;
|
2016-12-07 01:40:56 +00:00
|
|
|
|
2017-02-24 01:45:15 +00:00
|
|
|
if (r > 0)
|
2017-02-23 17:15:41 +00:00
|
|
|
{
|
2017-02-24 01:45:15 +00:00
|
|
|
if (r < 1.5707963267948966)
|
2017-02-23 17:15:41 +00:00
|
|
|
{
|
|
|
|
// 0 < theta < 90
|
2017-02-24 01:45:15 +00:00
|
|
|
yMax = y + hct + wst;
|
|
|
|
xMin = x - hst;
|
|
|
|
xMax = x + wct;
|
2017-02-23 17:15:41 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// 90 <= theta <= 180
|
2017-02-24 01:45:15 +00:00
|
|
|
yMin = y + hct;
|
|
|
|
yMax = y + wst;
|
|
|
|
xMin = x - hst + wct;
|
2017-02-23 17:15:41 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-24 01:58:27 +00:00
|
|
|
else if (r > -1.5707963267948966)
|
|
|
|
{
|
|
|
|
// -90 < theta <= 0
|
|
|
|
yMin = y + wst;
|
|
|
|
yMax = y + hct;
|
|
|
|
xMax = x + wct - hst;
|
|
|
|
}
|
2017-02-23 17:15:41 +00:00
|
|
|
else
|
|
|
|
{
|
2017-02-24 01:58:27 +00:00
|
|
|
// -180 <= theta <= -90
|
|
|
|
yMin = y + wst + hct;
|
|
|
|
xMin = x + wct;
|
|
|
|
xMax = x - hst;
|
2017-02-23 17:15:41 +00:00
|
|
|
}
|
2016-12-07 01:40:56 +00:00
|
|
|
|
2017-02-24 01:58:27 +00:00
|
|
|
output.x = xMin;
|
|
|
|
output.y = yMin;
|
|
|
|
output.width = xMax - xMin;
|
|
|
|
output.height = yMax - yMin;
|
|
|
|
|
|
|
|
return output;
|
2017-02-23 17:15:41 +00:00
|
|
|
}
|
2017-11-02 00:30:04 +00:00
|
|
|
*/
|
2016-12-07 01:40:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = GetBounds;
|