Skip to content

Commit 4f7ed5c

Browse files
committed
Added in Line geometry functions.
1 parent 4252526 commit 4f7ed5c

12 files changed

Lines changed: 238 additions & 0 deletions

File tree

v3/src/geom/line/Angle.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
var Angle = function (line)
2+
{
3+
return Math.atan2(line.y2 - line.y1, line.x2 - line.x1);
4+
};
5+
6+
module.exports = Angle;

v3/src/geom/line/GetNormal.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var MATH_CONST = require('../../math/const');
2+
var Angle = require('./Angle');
3+
var Point = require('../point/Point');
4+
5+
var GetNormal = function (line, out)
6+
{
7+
if (out === undefined) { out = new Point(); }
8+
9+
var a = Angle(line) - MATH_CONST.TAU;
10+
11+
out.x = Math.cos(a);
12+
out.y = Math.sin(a);
13+
14+
return out;
15+
};
16+
17+
module.exports = GetNormal;

v3/src/geom/line/Height.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
var Height = function (line)
2+
{
3+
return Math.abs(line.y1 - line.y2);
4+
};
5+
6+
module.exports = Height;

v3/src/geom/line/Length.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
var Length = function (line)
2+
{
3+
return Math.sqrt((line.x2 - line.x1) * (line.x2 - line.x1) + (line.y2 - line.y1) * (line.y2 - line.y1));
4+
};
5+
6+
module.exports = Length;

v3/src/geom/line/Line.js

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
// Defines a Line segment, a part of a line between two endpoints
2+
3+
var Line = function (x1, y1, x2, y2)
4+
{
5+
if (x1 === undefined) { x1 = 0; }
6+
if (y1 === undefined) { y1 = 0; }
7+
if (x2 === undefined) { x2 = 0; }
8+
if (y2 === undefined) { y2 = 0; }
9+
10+
this.x1 = x1;
11+
12+
this.y1 = y1;
13+
14+
this.x2 = x2;
15+
16+
this.y2 = y2;
17+
};
18+
19+
Line.prototype.constructor = Line;
20+
21+
Line.prototype = {
22+
23+
setTo: function (x1, y1, x2, y2)
24+
{
25+
if (x1 === undefined) { x1 = 0; }
26+
if (y1 === undefined) { y1 = 0; }
27+
if (x2 === undefined) { x2 = 0; }
28+
if (y2 === undefined) { y2 = 0; }
29+
30+
this.x1 = x1;
31+
this.y1 = y1;
32+
33+
this.x2 = x2;
34+
this.y2 = y2;
35+
36+
return this;
37+
}
38+
39+
};
40+
41+
Object.defineProperties(Line.prototype, {
42+
43+
left: {
44+
45+
enumerable: true,
46+
47+
get: function ()
48+
{
49+
return Math.min(this.x1, this.x2);
50+
},
51+
52+
set: function (value)
53+
{
54+
if (this.x1 <= this.x2)
55+
{
56+
this.x1 = value;
57+
}
58+
else
59+
{
60+
this.x2 = value;
61+
}
62+
}
63+
64+
},
65+
66+
right: {
67+
68+
enumerable: true,
69+
70+
get: function ()
71+
{
72+
return Math.max(this.x1, this.x2);
73+
},
74+
75+
set: function (value)
76+
{
77+
if (this.x1 > this.x2)
78+
{
79+
this.x1 = value;
80+
}
81+
else
82+
{
83+
this.x2 = value;
84+
}
85+
}
86+
87+
},
88+
89+
top: {
90+
91+
enumerable: true,
92+
93+
get: function ()
94+
{
95+
return Math.min(this.y1, this.y2);
96+
},
97+
98+
set: function (value)
99+
{
100+
if (this.y1 <= this.y2)
101+
{
102+
this.y1 = value;
103+
}
104+
else
105+
{
106+
this.y2 = value;
107+
}
108+
}
109+
110+
},
111+
112+
bottom: {
113+
114+
enumerable: true,
115+
116+
get: function ()
117+
{
118+
return Math.max(this.y1, this.y2);
119+
},
120+
121+
set: function (value)
122+
{
123+
if (this.y1 > this.y2)
124+
{
125+
this.y1 = value;
126+
}
127+
else
128+
{
129+
this.y2 = value;
130+
}
131+
}
132+
133+
}
134+
135+
});
136+
137+
module.exports = Line;

v3/src/geom/line/NormalAngle.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
var MATH_CONST = require('../../math/const');
2+
var Wrap = require('../../math/Wrap');
3+
var Angle = require('./Angle');
4+
5+
var NormalAngle = function (line)
6+
{
7+
var angle = Angle(line) - MATH_CONST.TAU;
8+
9+
return Wrap(angle, -Math.PI, Math.PI);
10+
};
11+
12+
module.exports = NormalAngle;

v3/src/geom/line/NormalX.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var MATH_CONST = require('../../math/const');
2+
var Angle = require('./Angle');
3+
4+
var NormalX = function (line)
5+
{
6+
return Math.cos(Angle(line) - MATH_CONST.TAU);
7+
};
8+
9+
module.exports = NormalX;

v3/src/geom/line/NormalY.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var MATH_CONST = require('../../math/const');
2+
var Angle = require('./Angle');
3+
4+
var NormalY = function (line)
5+
{
6+
return Math.sin(Angle(line) - MATH_CONST.TAU);
7+
};
8+
9+
module.exports = NormalY;

v3/src/geom/line/PerpSlope.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
var PerpSlope = function (line)
2+
{
3+
return -((line.x2 - line.x1) / (line.y2 - line.y1));
4+
};
5+
6+
module.exports = PerpSlope;

v3/src/geom/line/ReflectAngle.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var Angle = require('./Angle');
2+
var NormalAngle = require('./NormalAngle');
3+
4+
/**
5+
* Returns the reflected angle between two lines.
6+
* This is the outgoing angle based on the angle of Line 1 and the normalAngle of Line 2.
7+
*
8+
* @method Phaser.Line.reflect
9+
* @param {Phaser.Line} a - The base line.
10+
* @param {Phaser.Line} b - The line to be reflected from the base line.
11+
* @return {number} The reflected angle in radians.
12+
*/
13+
var ReflectAngle = function (lineA, lineB)
14+
{
15+
return (2 * NormalAngle(lineB) - Math.PI - Angle(lineA));
16+
};
17+
18+
module.exports = ReflectAngle;

0 commit comments

Comments
 (0)