{"id":5764,"date":"2024-02-15T09:08:39","date_gmt":"2024-02-15T07:08:39","guid":{"rendered":"https:\/\/unitycoder.com\/blog\/?p=5764"},"modified":"2024-02-15T09:10:31","modified_gmt":"2024-02-15T07:10:31","slug":"check-if-point-is-inside-polygon-2d-using-line-intersection","status":"publish","type":"post","link":"https:\/\/unitycoder.com\/blog\/2024\/02\/15\/check-if-point-is-inside-polygon-2d-using-line-intersection\/","title":{"rendered":"Check if point is inside polygon 2D (using line intersection)"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2024\/02\/pointinsidepolygon2d-ezgif.com-optimize.gif?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"478\" height=\"402\" data-attachment-id=\"5765\" data-permalink=\"https:\/\/unitycoder.com\/blog\/2024\/02\/15\/check-if-point-is-inside-polygon-2d-using-line-intersection\/pointinsidepolygon2d-ezgif-com-optimize\/\" data-orig-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2024\/02\/pointinsidepolygon2d-ezgif.com-optimize.gif?fit=478%2C402&amp;ssl=1\" data-orig-size=\"478,402\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"pointinsidepolygon2d-ezgif.com-optimize\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2024\/02\/pointinsidepolygon2d-ezgif.com-optimize.gif?fit=300%2C252&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2024\/02\/pointinsidepolygon2d-ezgif.com-optimize.gif?fit=478%2C402&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2024\/02\/pointinsidepolygon2d-ezgif.com-optimize.gif?resize=478%2C402&#038;ssl=1\" alt=\"\" class=\"wp-image-5765\"\/><\/a><\/figure>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>Info:<\/strong><br>&#8220;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 <strong>point is on the interior if, and <span style=\"text-decoration: underline;\">only if<\/span>, the ray crosses the boundary an odd number of times<\/strong>&#8220;<br><a href=\"https:\/\/www.topcoder.com\/thrive\/articles\/Geometry%20Concepts%20part%203:%20Using%20Geometry%20in%20Topcoder%20Problems\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/www.topcoder.com\/thrive\/articles\/Geometry%20Concepts%20part%203:%20Using%20Geometry%20in%20Topcoder%20Problems<\/a><\/p>\n\n\n\n<p>Tested the algorithm above and seems to work! : o<br><br><strong>Source:<\/strong><br><a href=\"https:\/\/gist.github.com\/unitycoder\/e769e6e7f4d0ee50f983d1265e6f15d9\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/gist.github.com\/unitycoder\/e769e6e7f4d0ee50f983d1265e6f15d9<\/a><\/p>\n\n\n\n<p><strong>Line Intersection method:<\/strong><br><a href=\"https:\/\/forum.unity.com\/threads\/line-intersection.17384\/#post-4442284\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/forum.unity.com\/threads\/line-intersection.17384\/#post-4442284<\/a><\/p>\n\n\n\n<p>Note: Unity has build-in method already,<br><a href=\"https:\/\/docs.unity3d.com\/ScriptReference\/Collider2D.OverlapPoint.html\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/docs.unity3d.com\/ScriptReference\/Collider2D.OverlapPoint.html<\/a><\/p>\n\n\n\n<p><strong>Video: (making of)<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<span class=\"embed-youtube\" style=\"text-align:center; display: block;\"><iframe loading=\"lazy\" class=\"youtube-player\" width=\"640\" height=\"360\" src=\"https:\/\/www.youtube.com\/embed\/Szta-Dx1jU8?version=3&#038;rel=1&#038;showsearch=0&#038;showinfo=1&#038;iv_load_policy=1&#038;fs=1&#038;hl=en-US&#038;autohide=2&#038;wmode=transparent\" allowfullscreen=\"true\" style=\"border:0;\" sandbox=\"allow-scripts allow-same-origin allow-popups allow-presentation allow-popups-to-escape-sandbox\"><\/iframe><\/span>\n<\/div><\/figure>\n\n\n\n<p><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":5765,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":false,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[22],"tags":[65,93,1278,1279],"class_list":["post-5764","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-functions","tag-2d","tag-algorithm","tag-line-intersection","tag-point-inside-polygon"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2024\/02\/pointinsidepolygon2d-ezgif.com-optimize.gif?fit=478%2C402&ssl=1","jetpack_shortlink":"https:\/\/wp.me\/p1KTaT-1uY","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/posts\/5764","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/comments?post=5764"}],"version-history":[{"count":8,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/posts\/5764\/revisions"}],"predecessor-version":[{"id":5774,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/posts\/5764\/revisions\/5774"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/media\/5765"}],"wp:attachment":[{"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/media?parent=5764"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/categories?post=5764"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/tags?post=5764"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}