Skip to content

Triangle2D #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions Assets/Scripts/2D/Geometry/Triangle2D.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
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;
}
}
}

11 changes: 11 additions & 0 deletions Assets/Scripts/2D/Geometry/Triangle2D.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions Assets/Scripts/Extensions/Vector2Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using UnityEngine;

namespace UnityLibrary
{
static class Vector2Extensions
{
public static Vector2 Round(this Vector2 vector, int to = 0) => new Vector2(vector.x.Round(to), vector.y.Round(to));

public static Vector2 Rotate(this Vector2 vector, float angle, Vector2 pivot = default(Vector2))
{
Vector2 rotated = Quaternion.Euler(new Vector3(0f, 0f, angle)) * (vector - pivot);
return rotated + pivot;
}
}
}
11 changes: 11 additions & 0 deletions Assets/Scripts/Extensions/Vector2Extensions.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.