Skip to content

Commit 42315ae

Browse files
committed
Added Triangle.GetPoint and Triangle.GetPoints. Both working with tests in the examples repo.
1 parent e554307 commit 42315ae

3 files changed

Lines changed: 140 additions & 0 deletions

File tree

v3/src/geom/triangle/GetPoint.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
var Point = require('../point/Point');
2+
var Length = require('../line/Length');
3+
4+
// Position is a value between 0 and 1
5+
var GetPoint = function (triangle, position, out)
6+
{
7+
if (out === undefined) { out = new Point(); }
8+
9+
var line1 = triangle.getLineA();
10+
var line2 = triangle.getLineB();
11+
var line3 = triangle.getLineC();
12+
13+
if (position <= 0 || position >= 1)
14+
{
15+
out.x = line1.x1;
16+
out.y = line1.y1;
17+
18+
return out;
19+
}
20+
21+
var length1 = Length(line1);
22+
var length2 = Length(line2);
23+
var length3 = Length(line3);
24+
25+
var perimeter = length1 + length2 + length3;
26+
27+
var p = perimeter * position;
28+
var localPosition = 0;
29+
30+
// Which line is it on?
31+
32+
if (p < length1)
33+
{
34+
// Line 1
35+
localPosition = p / length1;
36+
37+
out.x = line1.x1 + (line1.x2 - line1.x1) * localPosition;
38+
out.y = line1.y1 + (line1.y2 - line1.y1) * localPosition;
39+
}
40+
else if (p > length1 + length2)
41+
{
42+
// Line 3
43+
p -= length1 + length2;
44+
localPosition = p / length3;
45+
46+
out.x = line3.x1 + (line3.x2 - line3.x1) * localPosition;
47+
out.y = line3.y1 + (line3.y2 - line3.y1) * localPosition;
48+
}
49+
else
50+
{
51+
// Line 2
52+
p -= length1;
53+
localPosition = p / length2;
54+
55+
out.x = line2.x1 + (line2.x2 - line2.x1) * localPosition;
56+
out.y = line2.y1 + (line2.y2 - line2.y1) * localPosition;
57+
}
58+
59+
return out;
60+
};
61+
62+
module.exports = GetPoint;

v3/src/geom/triangle/GetPoints.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
var Length = require('../line/Length');
2+
var Point = require('../point/Point');
3+
4+
var GetPoints = function (triangle, quantity, stepRate, out)
5+
{
6+
if (out === undefined) { out = []; }
7+
8+
var line1 = triangle.getLineA();
9+
var line2 = triangle.getLineB();
10+
var line3 = triangle.getLineC();
11+
12+
var length1 = Length(line1);
13+
var length2 = Length(line2);
14+
var length3 = Length(line3);
15+
16+
var perimeter = length1 + length2 + length3;
17+
18+
// If quantity is a falsey value (false, null, 0, undefined, etc) then we calculate it based on the stepRate instead.
19+
if (!quantity)
20+
{
21+
quantity = perimeter / stepRate;
22+
}
23+
24+
for (var i = 0; i < quantity; i++)
25+
{
26+
var p = perimeter * (i / quantity);
27+
var localPosition = 0;
28+
29+
var point = new Point();
30+
31+
// Which line is it on?
32+
33+
if (p < length1)
34+
{
35+
// Line 1
36+
localPosition = p / length1;
37+
38+
point.x = line1.x1 + (line1.x2 - line1.x1) * localPosition;
39+
point.y = line1.y1 + (line1.y2 - line1.y1) * localPosition;
40+
}
41+
else if (p > length1 + length2)
42+
{
43+
// Line 3
44+
p -= length1 + length2;
45+
localPosition = p / length3;
46+
47+
point.x = line3.x1 + (line3.x2 - line3.x1) * localPosition;
48+
point.y = line3.y1 + (line3.y2 - line3.y1) * localPosition;
49+
}
50+
else
51+
{
52+
// Line 2
53+
p -= length1;
54+
localPosition = p / length2;
55+
56+
point.x = line2.x1 + (line2.x2 - line2.x1) * localPosition;
57+
point.y = line2.y1 + (line2.y2 - line2.y1) * localPosition;
58+
}
59+
60+
out.push(point);
61+
}
62+
63+
return out;
64+
};
65+
66+
module.exports = GetPoints;

v3/src/geom/triangle/Triangle.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
// A triangle is a plane created by connecting three points.
@@ -23,6 +25,16 @@ var Triangle = new Class({
2325
this.setTo(x1, y1, x2, y2, x3, y3);
2426
},
2527

28+
getPoint: function (position, output)
29+
{
30+
return GetPoint(this, position, output);
31+
},
32+
33+
getPoints: function (quantity, stepRate, output)
34+
{
35+
return GetPoints(this, quantity, stepRate, output);
36+
},
37+
2638
getRandomPoint: function (point)
2739
{
2840
return Random(this, point);

0 commit comments

Comments
 (0)