# Region

Are there any built in functions to determine if a point in space is inside a region if you can provide the point in 2d (x,y) and the polygon as a series of coordinates in 2d as well. The coordinates do not need to refer to actual coordinates but just as an abstract function. I can provide the values for the coordinates. Something like PointInPoly.

Thanks for the help.

There’s Rect.Contains() but that’s only for rectangles.

–Eric

Thanks that might actually work for what I’m doing. If anybody knows of a way to do it with polygons let me know. Thanks.

I did a bit of Googling, found a code snippet in C and translated it, so here we are:

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

So obviously you’d use it something like

``````var polygonArray : Vector2[];	// Array of points making up polygon
var point : Vector2;

function Start () {
if (PolyContainsPoint(polygonArray, point)) {
print ("Inside! Yay!");
}
else {
print ("Outside! Brr, cold out here!");
}
}
``````

–Eric

Wow thanks so much for doing that. I hope you can find a use for it as well. Thanks so much!!