PolyContainsPoint

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m (Added UnityScript Notice)
(Added C# port of the method)
 
Line 32: Line 32:
 
   return inside;  
 
   return inside;  
 
}</syntaxhighlight>
 
}</syntaxhighlight>
 +
 +
== Poly.cs ==
 +
 +
<syntaxhighlight lang="csharp">
 +
using UnityEngine;
 +
 +
public static class Poly
 +
{
 +
    public static bool ContainsPoint(Vector2[] polyPoints, Vector2 p)
 +
    {
 +
        var j = polyPoints.Length - 1;
 +
        var inside = false;
 +
        for (int i = 0; i < polyPoints.Length; j = i++)
 +
        {
 +
            var pi = polyPoints[i];
 +
            var pj = polyPoints[j];
 +
            if (((pi.y <= p.y && p.y < pj.y) || (pj.y <= p.y && p.y < pi.y)) &&
 +
                (p.x < (pj.x - pi.x) * (p.y - pi.y) / (pj.y - pi.y) + pi.x))
 +
                inside = !inside;
 +
        }
 +
        return inside;
 +
    }
 +
}
 +
</syntaxhighlight>
  
 
[[Category: JavaScript]]
 
[[Category: JavaScript]]
 +
[[Category:C Sharp]]

Latest revision as of 10:29, 23 June 2019

End of support for UnityScript

Starting with Unity 2018.2, support for UnityScript (Unity's version of JavaScript) has ended. C# is now the only supported language going forward. You can learn more about the reasons for this change here. There is an official UnityScript-to-C# Automatic Conversion tool to help those wanting to transition their UnityScript projects over to C#.


Author: Eric Haines (Eric5h5)

Contents

[edit] Description

Rect.Contains got you down? You're feeling the need for more points in your polygons? Fear not, PolyContainsPoint is here to save the day! That's right, now you can test to see if a given point is inside any kind of polygon you can think of. Say goodbye to boring old rectangles!

[edit] Usage

Have this script somewhere in your project. Call the function by the name of the script (let's call it "Poly") plus ".ContainsPoint". It takes an array of Vector2s describing the polygon and a single Vector2 containing the point you want to test, and returns a boolean. A code example:

// Array of points making up polygon
var polygonArray = [Vector2(0.0, 1.0), Vector2(0.0, 2.0), Vector2(2.0, 2.0), Vector2(2.0, 1.0), Vector2(1.0, 0.0)];
var point = Vector2(0.5, 0.5);   // See if this point is inside the polygon
 
function Start () {
   if (Poly.ContainsPoint(polygonArray, point)) {
      print ("Inside! Yay, I'm safe!");
   }
   else {
      print ("Outside! Brr, cold out here!");
   }
}

[edit] Poly.js

static function ContainsPoint (polyPoints : Vector2[], p : Vector2) : boolean { 
   var j = polyPoints.Length-1; 
   var inside = false; 
   for (i = 0; i < polyPoints.Length; j = i++) { 
      if ( ((polyPoints[i].y <= p.y && p.y < polyPoints[j].y) || (polyPoints[j].y <= p.y && p.y < polyPoints[i].y)) && 
         (p.x < (polyPoints[j].x - polyPoints[i].x) * (p.y - polyPoints[i].y) / (polyPoints[j].y - polyPoints[i].y) + polyPoints[i].x)) 
         inside = !inside; 
   } 
   return inside; 
}

[edit] Poly.cs

using UnityEngine;
 
public static class Poly
{
    public static bool ContainsPoint(Vector2[] polyPoints, Vector2 p)
    {
        var j = polyPoints.Length - 1;
        var inside = false;
        for (int i = 0; i < polyPoints.Length; j = i++)
        {
            var pi = polyPoints[i];
            var pj = polyPoints[j];
            if (((pi.y <= p.y && p.y < pj.y) || (pj.y <= p.y && p.y < pi.y)) &&
                (p.x < (pj.x - pi.x) * (p.y - pi.y) / (pj.y - pi.y) + pi.x))
                inside = !inside;
        }
        return inside;
    }
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox