forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetPoint.js
More file actions
71 lines (62 loc) · 1.67 KB
/
Copy pathGetPoint.js
File metadata and controls
71 lines (62 loc) · 1.67 KB
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
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
var Perimeter = require('./Perimeter');
var Point = require('../point/Point');
/**
* Position is a value between 0 and 1 where 0 = the top-left of the rectangle and 0.5 = the bottom right.
*
* @function Phaser.Geom.Rectangle.GetPoint
* @since 3.0.0
*
* @generic {Phaser.Geom.Point} O - [out,$return]
*
* @param {Phaser.Geom.Rectangle} rectangle - [description]
* @param {number} position - [description]
* @param {(Phaser.Geom.Point|object)} [out] - [description]
*
* @return {Phaser.Geom.Point} [description]
*/
var GetPoint = function (rectangle, position, out)
{
if (out === undefined) { out = new Point(); }
if (position <= 0 || position >= 1)
{
out.x = rectangle.x;
out.y = rectangle.y;
return out;
}
var p = Perimeter(rectangle) * position;
if (position > 0.5)
{
p -= (rectangle.width + rectangle.height);
if (p <= rectangle.width)
{
// Face 3
out.x = rectangle.right - p;
out.y = rectangle.bottom;
}
else
{
// Face 4
out.x = rectangle.x;
out.y = rectangle.bottom - (p - rectangle.width);
}
}
else if (p <= rectangle.width)
{
// Face 1
out.x = rectangle.x + p;
out.y = rectangle.y;
}
else
{
// Face 2
out.x = rectangle.right;
out.y = rectangle.y + (p - rectangle.width);
}
return out;
};
module.exports = GetPoint;