Skip to content

Commit c07ef79

Browse files
committed
New Triangle methods: BuildRight, BuildEquilateral, Clone, CopyFrom, Rotate, RotateAroundPoint and RotateAroundXY.
1 parent 12e075a commit c07ef79

10 files changed

Lines changed: 128 additions & 4 deletions

File tree

v3/src/checksum.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
var CHECKSUM = {
2-
build: '36a75180-14e9-11e7-ba0a-191be9c0e55a'
2+
build: '7e90d280-1540-11e7-adf9-8b058a0651ee'
33
};
44
module.exports = CHECKSUM;

v3/src/geom/line/CopyFrom.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/**
2-
* Copies the x, y and diameter properties from any given object to this Circle.
3-
* @method Phaser.Circle#copyFrom
2+
* Copies the x, y and diameter properties from any given object to this Line.
3+
* @method Phaser.Line#copyFrom
44
* @param {any} source - The object to copy from.
5-
* @return {Circle} This Circle object.
5+
* @return {Line} This Line object.
66
*/
77
var CopyFrom = function (source, dest)
88
{
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
var Triangle = require('./Triangle');
2+
3+
// Builds an equilateral triangle.
4+
// In the equilateral triangle, all the sides are the same length (congruent)
5+
// and all the angles are the same size (congruent).
6+
7+
// The x/y specifies the top-middle of the triangle (x1/y1) and length
8+
// is the length of each side
9+
10+
var BuildEquilateral = function (x, y, length)
11+
{
12+
var height = length * (Math.sqrt(3) / 2);
13+
14+
var x1 = x;
15+
var y1 = y;
16+
17+
var x2 = x + (length / 2);
18+
var y2 = y + height;
19+
20+
var x3 = x - (length / 2);
21+
var y3 = y + height;
22+
23+
return new Triangle(x1, y1, x2, y2, x3, y3);
24+
};
25+
26+
module.exports = BuildEquilateral;

v3/src/geom/triangle/BuildRight.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
var Triangle = require('./Triangle');
2+
3+
// Builds a right triangle, with one 90 degree angle and two acute angles
4+
// The x/y is the coordinate of the 90 degree angle (and will map to x1/y1 in the resulting Triangle)
5+
// w/h can be positive or negative and represent the length of each side
6+
7+
var BuildRight = function (x, y, width, height)
8+
{
9+
if (height === undefined) { height = width; }
10+
11+
// 90 degree angle
12+
var x1 = x;
13+
var y1 = y;
14+
15+
var x2 = x;
16+
var y2 = y - height;
17+
18+
var x3 = x + width;
19+
var y3 = y;
20+
21+
return new Triangle(x1, y1, x2, y2, x3, y3);
22+
};
23+
24+
module.exports = BuildRight;

v3/src/geom/triangle/Clone.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var Triangle = require('./Triangle');
2+
3+
var Clone = function (source)
4+
{
5+
return new Triangle(source.x1, source.y1, source.x2, source.y2, source.x3, source.y3);
6+
};
7+
8+
module.exports = Clone;

v3/src/geom/triangle/CopyFrom.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Copies the x1, y1 - x3, y3 properties from any given object to this Triangle.
3+
* @method Phaser.Line#copyFrom
4+
* @param {any} source - The object to copy from.
5+
* @return {Line} This Line object.
6+
*/
7+
var CopyFrom = function (source, dest)
8+
{
9+
return dest.setTo(source.x1, source.y1, source.x2, source.y2, source.x3, source.y3);
10+
};
11+
12+
module.exports = CopyFrom;

v3/src/geom/triangle/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+
var InCenter = require('./InCenter');
3+
4+
var Rotate = function (triangle, angle)
5+
{
6+
var point = InCenter(triangle);
7+
8+
return RotateAroundXY(triangle, point.x, point.y, angle);
9+
};
10+
11+
module.exports = Rotate;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var RotateAroundXY = require('./RotateAroundXY');
2+
3+
var RotateAroundPoint = function (triangle, point, angle)
4+
{
5+
return RotateAroundXY(triangle, point.x, point.y, angle);
6+
};
7+
8+
module.exports = RotateAroundPoint;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
var RotateAroundXY = function (triangle, x, y, angle)
3+
{
4+
var c = Math.cos(angle);
5+
var s = Math.sin(angle);
6+
7+
var tx = triangle.x1 - x;
8+
var ty = triangle.y1 - y;
9+
10+
triangle.x1 = tx * c - ty * s + x;
11+
triangle.y1 = tx * s + ty * c + y;
12+
13+
tx = triangle.x2 - x;
14+
ty = triangle.y2 - y;
15+
16+
triangle.x2 = tx * c - ty * s + x;
17+
triangle.y2 = tx * s + ty * c + y;
18+
19+
tx = triangle.x3 - x;
20+
ty = triangle.y3 - y;
21+
22+
triangle.x3 = tx * c - ty * s + x;
23+
triangle.y3 = tx * s + ty * c + y;
24+
25+
return triangle;
26+
};
27+
28+
module.exports = RotateAroundXY;

v3/src/geom/triangle/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,18 @@
22

33
var Triangle = require('./Triangle');
44

5+
Triangle.Area = require('./Area');
6+
Triangle.BuildEquilateral = require('./BuildEquilateral');
7+
Triangle.BuildRight = require('./BuildRight');
58
Triangle.Centroid = require('./Centroid');
69
Triangle.CircumCenter = require('./CircumCenter');
710
Triangle.CircumCircle = require('./CircumCircle');
11+
Triangle.Clone = require('./Clone');
812
Triangle.Contains = require('./Contains');
913
Triangle.ContainsPoint = require('./ContainsPoint');
1014
Triangle.InCenter = require('./InCenter');
15+
Triangle.Rotate = require('./Rotate');
16+
Triangle.RotateAroundPoint = require('./RotateAroundPoint');
17+
Triangle.RotateAroundXY = require('./RotateAroundXY');
1118

1219
module.exports = Triangle;

0 commit comments

Comments
 (0)