Feb
15
2024

Check if point is inside polygon 2D (using line intersection)

Info:
“Each time the ray crossed the boundary of the polygon, it would cross from the interior to the exterior, or vice versa. Therefore, the test point is on the interior if, and only if, the ray crosses the boundary an odd number of times
https://www.topcoder.com/thrive/articles/Geometry%20Concepts%20part%203:%20Using%20Geometry%20in%20Topcoder%20Problems

Tested the algorithm above and seems to work! : o

Source:
https://gist.github.com/unitycoder/e769e6e7f4d0ee50f983d1265e6f15d9

Line Intersection method:
https://forum.unity.com/threads/line-intersection.17384/#post-4442284

Note: Unity has build-in method already,
https://docs.unity3d.com/ScriptReference/Collider2D.OverlapPoint.html

Video: (making of)


Leave a comment

Connect

Twitter View LinkedIn profile Youtube Github Join Discord Twitch Instagram BlueSky

UnityLauncherPro

Get UnityLauncherPRO and work faster with Unity Projects!
*free unity hub alternative

@unitycoder_com

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.