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.
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!");
}
}