Skip to content

Commit 1deac35

Browse files
committed
Added in more intersection functions and line functions.
1 parent 4f7ed5c commit 1deac35

14 files changed

Lines changed: 195 additions & 7 deletions

File tree

v3/src/geom/circle/Random.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
var Point = require('../point/Point');
2+
13
/**
24
* Returns a uniformly distributed random point from anywhere within this Circle.
35
*
@@ -8,7 +10,7 @@
810
*/
911
var Random = function (circle, out)
1012
{
11-
if (out === undefined) { out = { x: 0, y: 0 }; }
13+
if (out === undefined) { out = new Point(); }
1214

1315
var t = 2 * Math.PI * Math.random();
1416
var u = Math.random() + Math.random();

v3/src/geom/ellipse/Random.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
var Point = require('../point/Point');
2+
13
var Random = function (ellipse, out)
24
{
3-
if (out === undefined) { out = { x: 0, y: 0 }; }
5+
if (out === undefined) { out = new Point(); }
46

57
var p = Math.random() * Math.PI * 2;
68
var s = Math.sqrt(Math.random());
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
var PointToLine = function (point, line)
2+
{
3+
return ((point.x - line.x1) * (line.y2 - line.y1) === (line.x2 - line.x1) * (point.y - line.y1));
4+
};
5+
6+
module.exports = PointToLine;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var PointToLine = require('./PointToLine');
2+
3+
var PointToLineSegment = function (point, line)
4+
{
5+
if (!PointToLine(point, line))
6+
{
7+
return false;
8+
}
9+
10+
var xMin = Math.min(line.x1, line.x2);
11+
var xMax = Math.max(line.x1, line.x2);
12+
var yMin = Math.min(line.y1, line.y2);
13+
var yMax = Math.max(line.y1, line.y2);
14+
15+
return ((point.x >= xMin && point.x <= xMax) && (point.y >= yMin && point.y <= yMax));
16+
};
17+
18+
module.exports = PointToLineSegment;

v3/src/geom/intersects/index.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11

22
module.exports = {
33

4-
CircleVsCircle: require('./CircleVsCircle'),
5-
CircleVsRectangle: require('./CircleVsRectangle'),
4+
CircleToCircle: require('./CircleToCircle'),
5+
CircleToRectangle: require('./CircleToRectangle'),
66
GetRectangleIntersection: require('./GetRectangleIntersection'),
7-
RectangleVsRectangle: require('./RectangleVsRectangle'),
8-
RectangleVsValues: require('./RectangleVsValues')
7+
LineToLine: require('./LineToLine'),
8+
LineToRectangle: require('./LineToRectangle'),
9+
PointToLine: require('./PointToLine'),
10+
PointToLineSegment: require('./PointToLineSegment'),
11+
RectangleToRectangle: require('./RectangleToRectangle'),
12+
RectangleToValues: require('./RectangleToValues')
913

1014
};

v3/src/geom/line/CenterOn.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
var CenterOn = function (line, x, y)
3+
{
4+
var tx = x - ((line.x1 + line.x2) / 2);
5+
var ty = y - ((line.y1 + line.y2) / 2);
6+
7+
line.x1 += tx;
8+
line.y1 += ty;
9+
10+
line.x2 += tx;
11+
line.y2 += ty;
12+
13+
return line;
14+
};
15+
16+
module.exports = CenterOn;

v3/src/geom/line/GetMidPoint.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var Point = require('../point/Point');
2+
3+
var GetMidPoint = function (line, out)
4+
{
5+
if (out === undefined) { out = new Point(); }
6+
7+
out.x = (line.x1 + line.x2) / 2;
8+
out.y = (line.y1 + line.y2) / 2;
9+
10+
return out;
11+
};
12+
13+
module.exports = GetMidPoint;
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* Using Bresenham's line algorithm this will return an array of all coordinates on this line.
3+
* The start and end points are rounded before this runs as the algorithm works on integers.
4+
*
5+
* @method Phaser.Line#coordinatesOnLine
6+
* @param {number} [stepRate=1] - How many steps will we return? 1 = every coordinate on the line, 2 = every other coordinate, etc.
7+
* @param {array} [results] - The array to store the results in. If not provided a new one will be generated.
8+
* @return {array} An array of coordinates.
9+
*/
10+
var GetPointsOnLine = function (line, stepRate, results)
11+
{
12+
if (stepRate === undefined) { stepRate = 1; }
13+
if (results === undefined) { results = []; }
14+
15+
var x1 = Math.round(line.x1);
16+
var y1 = Math.round(line.y1);
17+
var x2 = Math.round(line.x2);
18+
var y2 = Math.round(line.y2);
19+
20+
var dx = Math.abs(x2 - x1);
21+
var dy = Math.abs(y2 - y1);
22+
var sx = (x1 < x2) ? 1 : -1;
23+
var sy = (y1 < y2) ? 1 : -1;
24+
var err = dx - dy;
25+
26+
results.push([ x1, y1 ]);
27+
28+
var i = 1;
29+
30+
while (!((x1 === x2) && (y1 === y2)))
31+
{
32+
var e2 = err << 1;
33+
34+
if (e2 > -dy)
35+
{
36+
err -= dy;
37+
x1 += sx;
38+
}
39+
40+
if (e2 < dx)
41+
{
42+
err += dx;
43+
y1 += sy;
44+
}
45+
46+
if (i % stepRate === 0)
47+
{
48+
results.push([ x1, y1 ]);
49+
}
50+
51+
i++;
52+
}
53+
54+
return results;
55+
};
56+
57+
module.exports = GetPointsOnLine;

v3/src/geom/line/Random.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var Point = require('../point/Point');
2+
3+
var Random = function (line, out)
4+
{
5+
if (out === undefined) { out = new Point(); }
6+
7+
var t = Math.random();
8+
9+
out.x = line.x1 + t * (line.x2 - line.x1);
10+
out.y = line.y1 + t * (line.y2 - line.y1);
11+
12+
return out;
13+
};
14+
15+
module.exports = Random;

v3/src/geom/line/Rotate.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
var RotateAroundXY = require('./RotateAroundXY');
2+
3+
var Rotate = function (line, angle)
4+
{
5+
var x = (line.x1 + line.x2) / 2;
6+
var y = (line.y1 + line.y2) / 2;
7+
8+
return RotateAroundXY(line, x, y, angle);
9+
};
10+
11+
module.exports = Rotate;

0 commit comments

Comments
 (0)