Skip to content

Commit e009ec5

Browse files
authored
Merge pull request phaserjs#5096 from samme/feature/Rectangle-FromXY
Add Phaser.Geom.Rectangle.FromXY()
2 parents bb9eb3b + dfe4219 commit e009ec5

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

src/geom/rectangle/FromXY.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* @author samme
3+
* @copyright 2020 Photon Storm Ltd.
4+
* @license {@link https://opensource.org/licenses/MIT|MIT License}
5+
*/
6+
7+
var Rectangle = require('./Rectangle');
8+
9+
/**
10+
* Create the smallest Rectangle containing two coordinate pairs.
11+
*
12+
* @function Phaser.Geom.Rectangle.FromXY
13+
* @since 3.23.0
14+
*
15+
* @generic {Phaser.Geom.Rectangle} O - [out,$return]
16+
*
17+
* @param {number} x1 - The X coordinate of the first point.
18+
* @param {number} y1 - The Y coordinate of the first point.
19+
* @param {number} x2 - The X coordinate of the second point.
20+
* @param {number} y2 - The Y coordinate of the second point.
21+
* @param {Phaser.Geom.Rectangle} [out] - Optional Rectangle to adjust.
22+
*
23+
* @return {Phaser.Geom.Rectangle} The adjusted `out` Rectangle, or a new Rectangle if none was provided.
24+
*/
25+
var FromXY = function (x1, y1, x2, y2, out)
26+
{
27+
if (out === undefined) { out = new Rectangle(); }
28+
29+
return out.setTo(
30+
Math.min(x1, x2),
31+
Math.min(y1, y2),
32+
Math.abs(x1 - x2),
33+
Math.abs(y1 - y2)
34+
);
35+
};
36+
37+
module.exports = FromXY;

src/geom/rectangle/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Rectangle.FitOutside = require('./FitOutside');
2222
Rectangle.Floor = require('./Floor');
2323
Rectangle.FloorAll = require('./FloorAll');
2424
Rectangle.FromPoints = require('./FromPoints');
25+
Rectangle.FromXY = require('./FromXY');
2526
Rectangle.GetAspectRatio = require('./GetAspectRatio');
2627
Rectangle.GetCenter = require('./GetCenter');
2728
Rectangle.GetPoint = require('./GetPoint');

0 commit comments

Comments
 (0)