-
Notifications
You must be signed in to change notification settings - Fork 450
/
Copy pathTriangle2D.cs
67 lines (54 loc) · 2 KB
/
Triangle2D.cs
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using UnityEngine;
namespace UnityLibrary
{
// if you need to run collision checks within a triangular area, the easiest
// way to do that in Unity is to use a polygon collider. this is not very
// performant. this class allows you to run those collision checks without
// the performance overhead.
public class Triangle2D
{
Vector2[] vertices = new Vector2[3];
public Triangle2D(Vector2 v1, Vector2 v2, Vector2 v3)
{
Update(v1, v2, v3);
}
// update triangle by defining all its vertices
public void Update(Vector2 v1, Vector2 v2, Vector2 v3)
{
vertices[0] = v1;
vertices[1] = v2;
vertices[2] = v3;
}
// update triangle by redefining its origin (remaining points update relative to that)
public void Update(Vector2 v1)
{
Vector2 delta = v1 - vertices[0];
vertices[0] = v1;
vertices[1] += delta;
vertices[2] += delta;
}
// update triangle with rotation and pivot point
public void Update(Vector2 v1, Vector2 v2, Vector2 v3, float rotation, Vector2 pivot)
{
vertices[0] = v1.Rotate(rotation, pivot);
vertices[1] = v2.Rotate(rotation, pivot);
vertices[2] = v3.Rotate(rotation, pivot);
}
float Sign(Vector2 p1, Vector2 p2, Vector2 p3)
{
return (p1.x - p3.x) * (p2.y - p3.y) - (p2.x - p3.x) * (p1.y - p3.y);
}
public bool Contains(Vector2 pt, bool debug = false)
{
float d1, d2, d3;
bool has_neg, has_pos;
d1 = Sign(pt, vertices[0], vertices[1]);
d2 = Sign(pt, vertices[1], vertices[2]);
d3 = Sign(pt, vertices[2], vertices[0]);
has_neg = (d1 < 0) || (d2 < 0) || (d3 < 0);
has_pos = (d1 > 0) || (d2 > 0) || (d3 > 0);
bool contains = ! (has_neg && has_pos);
return contains;
}
}
}