Skip to content

Commit ca7acdc

Browse files
committed
Added GetPoint and GetPoints support to Circle, Ellipse and Line and exposed via the classes
1 parent 114ba23 commit ca7acdc

13 files changed

Lines changed: 186 additions & 5 deletions

File tree

v3/src/geom/circle/Circle.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
var Class = require('../../utils/Class');
2+
var GetPoint = require('./GetPoint');
3+
var GetPoints = require('./GetPoints');
24
var Random = require('./Random');
35

46
var Circle = new Class({
@@ -54,6 +56,16 @@ var Circle = new Class({
5456
this._diameter = radius * 2;
5557
},
5658

59+
getPoint: function (position, point)
60+
{
61+
return GetPoint(this, position, point);
62+
},
63+
64+
getPoints: function (steps, output)
65+
{
66+
return GetPoints(this, steps, output);
67+
},
68+
5769
/**
5870
* [description]
5971
*

v3/src/geom/circle/GetPoint.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
var CircumferencePoint = require('./CircumferencePoint');
2+
var FromPercent = require('../../math/FromPercent');
3+
var MATH_CONST = require('../../math/const');
4+
var Point = require('../point/Point');
5+
6+
// Returns a Point object containing the coordinates of a point on the circumference of the Circle
7+
// based on the given angle normalized to the range 0 to 1. I.e. a value of 0.5 will give the point
8+
// at 180 degrees around the circle.
9+
10+
/**
11+
* [description]
12+
*
13+
* @function Phaser.Geom.Circle.GetPoint
14+
* @since 3.0.0
15+
*
16+
* @param {Phaser.Geom.Circle} circle - The Circle to get the circumference point on.
17+
* @param {float} position - A value between 0 and 1, where 0 equals 0 degrees, 0.5 equals 180 degrees and 1 equals 360 around the circle.
18+
* @param {Phaser.Geom.Point|object} [out] - [description]
19+
*
20+
* @return {Phaser.Geom.Point|object} [description]
21+
*/
22+
var GetPoint = function (circle, position, out)
23+
{
24+
if (out === undefined) { out = new Point(); }
25+
26+
var angle = FromPercent(position, 0, MATH_CONST.PI2);
27+
28+
return CircumferencePoint(circle, angle, out);
29+
};
30+
31+
module.exports = GetPoint;

v3/src/geom/circle/GetPoints.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
var CircumferencePoint = require('./CircumferencePoint');
2+
var FromPercent = require('../../math/FromPercent');
3+
var MATH_CONST = require('../../math/const');
4+
var Point = require('../point/Point');
5+
6+
var GetPoints = function (circle, steps, out)
7+
{
8+
if (out === undefined) { out = []; }
9+
10+
var t = 0;
11+
var inc = MATH_CONST.PI2 / steps;
12+
13+
for (var i = 0; i < steps; i++)
14+
{
15+
var angle = FromPercent(i / steps, 0, MATH_CONST.PI2);
16+
17+
out.push(CircumferencePoint(circle, angle));
18+
19+
t += inc;
20+
}
21+
22+
return out;
23+
};
24+
25+
module.exports = GetPoints;

v3/src/geom/circle/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Circle.ContainsRect = require('./ContainsRect');
1212
Circle.CopyFrom = require('./CopyFrom');
1313
Circle.Equals = require('./Equals');
1414
Circle.GetBounds = require('./GetBounds');
15+
Circle.GetPoint = require('./GetPoint');
16+
Circle.GetPoints = require('./GetPoints');
1517
Circle.Offset = require('./Offset');
1618
Circle.OffsetPoint = require('./OffsetPoint');
1719
Circle.Random = require('./Random');

v3/src/geom/ellipse/CircumferencePoint.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
var Point = require('../point/Point');
22

33
/**
4-
* Given an angle this will returna Point object containing the coordinates of the point
4+
* Given an angle this will return a Point object containing the coordinates of the point
55
* on the circumference of the ellipse.
66
*
77
* @function Phaser.Geom.Ellipse.CircumferencePoint

v3/src/geom/ellipse/Ellipse.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
var Class = require('../../utils/Class');
2+
var GetPoint = require('./GetPoint');
3+
var GetPoints = require('./GetPoints');
24
var Random = require('./Random');
35

46
var Ellipse = new Class({
@@ -21,6 +23,16 @@ var Ellipse = new Class({
2123
this.height = height;
2224
},
2325

26+
getPoint: function (position, point)
27+
{
28+
return GetPoint(this, position, point);
29+
},
30+
31+
getPoints: function (steps, output)
32+
{
33+
return GetPoints(this, steps, output);
34+
},
35+
2436
getRandomPoint: function (point)
2537
{
2638
return Random(this, point);

v3/src/geom/ellipse/GetPoint.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
var CircumferencePoint = require('./CircumferencePoint');
2+
var FromPercent = require('../../math/FromPercent');
3+
var MATH_CONST = require('../../math/const');
4+
var Point = require('../point/Point');
5+
6+
// Returns a Point object containing the coordinates of a point on the circumference of the Ellipse
7+
// based on the given angle normalized to the range 0 to 1. I.e. a value of 0.5 will give the point
8+
// at 180 degrees around the ellipse.
9+
10+
/**
11+
* [description]
12+
*
13+
* @function Phaser.Geom.Ellipse.GetPoint
14+
* @since 3.0.0
15+
*
16+
* @param {Phaser.Geom.Ellipse} ellipse - The Ellipse to get the circumference point on.
17+
* @param {float} position - A value between 0 and 1, where 0 equals 0 degrees, 0.5 equals 180 degrees and 1 equals 360 around the ellipse.
18+
* @param {Phaser.Geom.Point|object} [out] - [description]
19+
*
20+
* @return {Phaser.Geom.Point|object} [description]
21+
*/
22+
var GetPoint = function (ellipse, position, out)
23+
{
24+
if (out === undefined) { out = new Point(); }
25+
26+
var angle = FromPercent(position, 0, MATH_CONST.PI2);
27+
28+
return CircumferencePoint(ellipse, angle, out);
29+
};
30+
31+
module.exports = GetPoint;

v3/src/geom/ellipse/GetPoints.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
var CircumferencePoint = require('./CircumferencePoint');
2+
var FromPercent = require('../../math/FromPercent');
3+
var MATH_CONST = require('../../math/const');
4+
var Point = require('../point/Point');
5+
6+
var GetPoints = function (ellipse, steps, out)
7+
{
8+
if (out === undefined) { out = []; }
9+
10+
var t = 0;
11+
var inc = MATH_CONST.PI2 / steps;
12+
13+
for (var i = 0; i < steps; i++)
14+
{
15+
var angle = FromPercent(i / steps, 0, MATH_CONST.PI2);
16+
17+
out.push(CircumferencePoint(ellipse, angle));
18+
19+
t += inc;
20+
}
21+
22+
return out;
23+
};
24+
25+
module.exports = GetPoints;

v3/src/geom/ellipse/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Ellipse.ContainsRect = require('./ContainsRect');
1111
Ellipse.CopyFrom = require('./CopyFrom');
1212
Ellipse.Equals = require('./Equals');
1313
Ellipse.GetBounds = require('./GetBounds');
14+
Ellipse.GetPoint = require('./GetPoint');
15+
Ellipse.GetPoints = require('./GetPoints');
1416
Ellipse.Offset = require('./Offset');
1517
Ellipse.OffsetPoint = require('./OffsetPoint');
1618
Ellipse.Random = require('./Random');

v3/src/geom/line/GetPoint.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@ var Point = require('../point/Point');
1010
* @since 3.0.0
1111
*
1212
* @param {Phaser.Geom.Line} line - [description]
13-
* @param {float} progress - [description]
13+
* @param {float} position - A value between 0 and 1, where 0 equals 0 degrees, 0.5 equals 180 degrees and 1 equals 360 around the circle.
1414
* @param {Phaser.Geom.Point|object} [out] - [description]
1515
*
1616
* @return {Phaser.Geom.Point|object} [description]
1717
*/
18-
var GetPoint = function (line, progress, out)
18+
var GetPoint = function (line, position, out)
1919
{
2020
if (out === undefined) { out = new Point(); }
2121

22-
out.x = line.x1 + (line.x2 - line.x1) * progress;
23-
out.y = line.y1 + (line.y2 - line.y1) * progress;
22+
out.x = line.x1 + (line.x2 - line.x1) * position;
23+
out.y = line.y1 + (line.y2 - line.y1) * position;
2424

2525
return out;
2626
};

0 commit comments

Comments
 (0)