How would I find out which segment my points are in? I'm wondering if it would be done with an equation of a line or something, or if theres an easier way to figure it out.
If you find the equation of the lines, let say in the form of y=mx + b, you can find if a point is above or below a line. (Just set the x to your current coordinate, plug it into the equation and see if it is greater than the y).
So in this case, you have two lines, so if it is above both it is in the top quadrant, below both in the bottom quadrant, and depending which one and which one it isn't above, left or right quadrant it is in.
You could also use vector arithmetic to figure this out, you might find such an approach more robust when it comes to near-degeneracies (vertical or very steep lines with extremely large slopes).
The figure is a reproduction of your figure with different labels. Use the cross product to determine whether point P is on the left-halfplane or right-halfplane of the splitting line AB. For P to be inside triangle ABC, it must be in the left plane of AB. Continuing, it should be in the left plane of all three edges, AB, BC and CA. Here's psuedocode you can translate into actionscript...
function insideTriangleTest (P, A, B, C) : boolean
{
// Testing if P is in left-halfplane of AB
// Construct vectors AB, AP
AB.x = B.x - A.x
AB.y = B.y - A.y
AP.x = P.x - A.x
AP.y = P.y - A.y
// Cross product test. Positive value indicates P is left of AB.
var AB_cross_AP : Number = AB.x * AP.y - AB.y * AP.x;
// Testing if P is in left-halfplane of BC.
BC.x = C.x - B.x
BC.y = C.y - B.y
BP.x = P.x - B.x
BP.y = P.y - B.y
var BC_cross_BP : Number = BC.x * BP.y - BC.y * BP.x
// Testing if P is in left-halfplane of CA.
CA.x = A.x - C.x
CA.y = A.y - C.y
CP.x = P.x - C.x
CP.y = P.y - C.y
var CA_cross_CP : Number = CA.x * CP.y - CA.y * CP.x
// Making the final decision - was P in the left-halfplane of all 3 edges?
if (AB_cross_AP > 0 && BC_cross_BP > 0 && CA_cross_CP > 0)
return true;
// If any o of those tests fails, fall-through and return false - not inside triangle.
return false;
}
That just tests if the point is inside the triangle ("segment") bounded by points ABC. If this test fails, check the other triangles using the same function (with different arguments, of course). Careful, the points that you pass into the funtion as the A,B,C arguments should walk around the triangle in COUNTER-CLOCKWISE order. However, which point you pass in first is irrelevant - A,B,C or B,C,A or C,A,B would all be acceptable. But not C,B,A for example, because that walks around the triangle in CLOCKWISE order (wrong).
FYI, this test would work for any convex polygon regardless of the number of sides. Just check against each bounding edge (again, pass in the vertices counter-clockwise).
EDIT: attachment thingee is broken.. post back if you want the picture, I can try from a different PC (sorry).
try this;
1) create an mc that is a horizontal line at y=0
2) make two copies of that line mc on the stage
3) rotate them to make the cross you see inside your square like this
4) take one point and use localToGlobal to get its position within each line like this;
Code:
var p1 = { x: 0, y :0 };
point1Mc.localToGlobal(p1);
line1Mc.globalToLocal(p1);
var p2 = { x: 0, y :0 };
point1Mc.localToGlobal(p2);
line2Mc.globalToLocal(p2);
5) if p1.y and p2.y are both positive then you are in the top quadrant
if p1.y and p2.y are both negative then you are in the bottom quadrant
if p1.y is positive and p2.y is negative then it is in the right quadrant
if p1.y is negative and p2.y is positive then it is in the left quadrant
Last edited by BlinkOk; 08-30-2007 at 06:28 PM.
Graphics Attract, Motion Engages, Gameplay Addicts
XP Pro | P4 2.8Ghz | 2Gb | 80Gb,40Gb | 128Mb DDR ATI Radeon 9800 Pro