[AS2] Point in Polygon (beginfill is not instant)
I figured Point in Polygon would be a SNAP in flash. Just draw the polygond with beginfill, moveto, lineto, endfill and then perform a shapeflag = true hittest!
Drawing the shape and checking hittesting with mouse coordinate and onenterframe worked great. So I wasted a bunch of time making a polygon drawing function which I was going to use to draw a polygon, perform hittests against it, and then erase the polygon and associated movieclip.
Unfortunately
I don't know if it waits till the next frame or what, but you can't do a shapeflag = true hittest on movieclip in code that executes in the same frame. For instance:
PHP Code:
var square_mc:MovieClip = this.createEmptyMovieClip("square_mc", 1);
square_mc.beginFill(0xFF0000);
square_mc.moveTo(10, 10);
square_mc.lineTo(100, 10);
square_mc.lineTo(100, 100);
square_mc.lineTo(10, 100);
square_mc.lineTo(10, 10);
square_mc.endFill();
trace(square_mc.hitTest(50,50,true)); // returns false
trace(square_mc.hitTest(50,50,false)); // returns true
I am trying to make a bunch of objects (to be rendered as bitmaps) in a bricklike grid pattern within polygonal shapes. Works fine for rectangles, and I was going to use the same rectangle row/column code but with an added "is point in polygon" check for each object, creating it if it is inside the polygon.
Unfortunately the above issue prevents this from working right. I guess I could keep the movieclips with the polygons till the next frame, then check all the objects to see if they are THEN inside their appropriate polygons and THEN delete those extra movieclips, but that just seems like a very lame hack of a solution.
Anyone now a decen point in poly function for flash as2? Right now I have the vertices as arrays of point objects, but I could be flexible.
[AS2] Point in Polygon & Pick's Theorem for Geoboard
I know it's a year and a half later, but I have a question on this topic. Although the code above does indeed work if a point is clearly inside a polygon, it is a bit flaky when it comes to points that are on the line. How could this code be adjusted to determine if a point is on the line or not? I could do the hitTest thing I suppose, but it would be cool to do it with math. The reason I'd like to do this is because I'm working on a Geoboard and I'd love to use Pick's Theorem to find the area of an irregular polygon.
Here is the code modified for my purposes (not optimized yet). I don't think I mucked it up, but it's a possibility!
Code:
function pointInPolyCheck(thisBand, pointX, pointY){
var myPoints = thisBand.pointsClip.myPoints;
var crossings = 0;
// check point against all of the polygon's lines
for (var i=0; i<myPoints.length; i++){
var linePoint1 = thisBand.pointsClip["point"+myPoints[i]];
var linePoint2 = thisBand.pointsClip["point"+myPoints[(i+1)%(myPoints.length)]];
if (((linePoint1._y <= pointY) && (linePoint2._y > pointY)) || ((linePoint1._y > pointY) && (linePoint2._y <= pointY))){
var vt:Number = (pointY - linePoint1._y) / (linePoint2._y - linePoint1._y);
if (pointX < linePoint1._x + vt * (linePoint2._x - linePoint1._x)) {
crossings++;
}
}
}
//trace("crossings:"+crossings);
if (crossings % 2){
// point is in polygon
return true;
} else {
return false;
}
}
Any insight would be greatly appreciated.
Update: I just found this, which is definitely an easier approach. It seems to work great even on complex polygons so I should be all good. Here are examples for a triangle and a square if anyone else is ever looking to figure out the area of a polygon.
x1 = 0;
y1 = 0;
x2 = 10;
y2 = 0;
x3 = 10;
y3 = 10;
area = ((x1*y2 + x2*y3 + x3*y1) - (x2*y1 + x3*y2 + x1*y3))/2;
trace(area);
x1 = 0;
y1 = 0;
x2 = 10;
y2 = 0;
x3 = 10;
y3 = 10;
x4 = 0;
y4 = 10;
area = ((x1*y2 + x2*y3 + x3*y4 + x4*y1) - (x2*y1 + x3*y2 + x4*y3 + x1*y4))/2;
trace(area);
I suppose I should have started a new topic in the Math forum for this but I can't seem to figure out how to delete this. Sorry