/
TriangleToLine.js
49 lines (42 loc) · 1.32 KB
/
TriangleToLine.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2020 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var LineToLine = require('./LineToLine');
/**
* Checks if a Triangle and a Line intersect.
*
* The Line intersects the Triangle if it starts inside of it, ends inside of it, or crosses any of the Triangle's sides. Thus, the Triangle is considered "solid".
*
* @function Phaser.Geom.Intersects.TriangleToLine
* @since 3.0.0
*
* @param {Phaser.Geom.Triangle} triangle - The Triangle to check with.
* @param {Phaser.Geom.Line} line - The Line to check with.
*
* @return {boolean} `true` if the Triangle and the Line intersect, otherwise `false`.
*/
var TriangleToLine = function (triangle, line)
{
// If the Triangle contains either the start or end point of the line, it intersects
if (triangle.contains(line.x1, line.y1) || triangle.contains(line.x2, line.y2))
{
return true;
}
// Now check the line against each line of the Triangle
if (LineToLine(triangle.getLineA(), line))
{
return true;
}
if (LineToLine(triangle.getLineB(), line))
{
return true;
}
if (LineToLine(triangle.getLineC(), line))
{
return true;
}
return false;
};
module.exports = TriangleToLine;