Created
February 14, 2024 19:18
-
-
Save unitycoder/e769e6e7f4d0ee50f983d1265e6f15d9 to your computer and use it in GitHub Desktop.
PointInPolygon2D
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using TMPro; | |
using UnityEngine; | |
public class PointInPolygon2D : MonoBehaviour | |
{ | |
public Transform point; | |
public PolygonCollider2D polygon; | |
public TextMeshProUGUI statusText; | |
Vector2[] verts; | |
SpriteRenderer sr; | |
void Start() | |
{ | |
sr = point.GetComponent<SpriteRenderer>(); | |
verts = polygon.points; | |
} | |
void Update() | |
{ | |
Vector2 lineStart = point.position; | |
Vector2 lineEnd = point.position + Vector3.up * -999; | |
Debug.DrawLine(lineStart, lineEnd, Color.gray); | |
int hitCount = 0; | |
// loop lines in collider | |
for (int i = 0; i < verts.Length; i++) | |
{ | |
var vertStart = polygon.transform.InverseTransformPoint(verts[i]); | |
var vertEnd = polygon.transform.InverseTransformPoint(verts[(i + 1) % verts.Length]); | |
// skip if above point Y | |
if (vertStart.y > lineStart.y && vertEnd.y > lineStart.y) | |
{ | |
Debug.DrawLine(vertStart, vertEnd, Color.gray); | |
continue; | |
} | |
// skip if start/end x is away from point x | |
if ((vertStart.x < lineStart.x && vertEnd.x < lineStart.x) || (vertStart.x > lineStart.x && vertEnd.x > lineStart.x)) | |
{ | |
Debug.DrawLine(vertStart, vertEnd, Color.black); | |
continue; | |
} | |
if (Intersects(lineStart, lineEnd, vertStart, vertEnd)) | |
{ | |
hitCount++; | |
Debug.DrawLine(vertStart, vertEnd, Color.red); | |
} | |
else | |
{ | |
Debug.DrawLine(vertStart, vertEnd, Color.green); | |
} | |
} | |
if (hitCount % 2 == 0) | |
{ | |
sr.color = Color.green; | |
statusText.text = "outside"; | |
} | |
else | |
{ | |
sr.color = Color.red; // inside | |
statusText.text = "inside"; | |
} | |
polygon.transform.Rotate(0,0,30*Time.deltaTime); | |
} | |
// https://forum.unity.com/threads/line-intersection.17384/#post-4442284 | |
bool Intersects(Vector2 line1point1, Vector2 line1point2, Vector2 line2point1, Vector2 line2point2) | |
{ | |
Vector2 a = line1point2 - line1point1; | |
Vector2 b = line2point1 - line2point2; | |
Vector2 c = line1point1 - line2point1; | |
float alphaNumerator = b.y * c.x - b.x * c.y; | |
float betaNumerator = a.x * c.y - a.y * c.x; | |
float denominator = a.y * b.x - a.x * b.y; | |
if (denominator == 0) | |
{ | |
return false; | |
} | |
else if (denominator > 0) | |
{ | |
if (alphaNumerator < 0 || alphaNumerator > denominator || betaNumerator < 0 || betaNumerator > denominator) | |
{ | |
return false; | |
} | |
} | |
else if (alphaNumerator > 0 || alphaNumerator < denominator || betaNumerator > 0 || betaNumerator < denominator) | |
{ | |
return false; | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment