A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: Which segment am I in

  1. #1
    Senior Member Boombanguk's Avatar
    Join Date
    Jun 2000
    Posts
    1,194

    Which segment am I in

    Hi,

    See attached image.

    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.

    thanks for any help.
    boombanguk
    Attached Images Attached Images

  2. #2
    Senior Member UnknownGuy's Avatar
    Join Date
    Jul 2003
    Location
    Canada
    Posts
    1,361
    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.

    Hope that helps!

  3. #3
    Senior Member rachil0's Avatar
    Join Date
    Jul 2007
    Location
    Columbus, OH.
    Posts
    465
    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).

  4. #4
    ism BlinkOk's Avatar
    Join Date
    Aug 2001
    Location
    , location, location
    Posts
    5,002
    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

  5. #5
    Script kiddie VENGEANCE MX's Avatar
    Join Date
    Jun 2004
    Location
    England
    Posts
    2,590
    http://www.birchlabs.co.uk/
    You know you want to.

  6. #6
    SaphuA SaphuA's Avatar
    Join Date
    Oct 2002
    Location
    The Netherlands
    Posts
    2,180
    This does the trick too:

    Code:
    var w = 400; // Width
    var h = 100; // Height
    var r = w/h; // Ratio
    
    this.createEmptyMovieClip("cont", 1);
    
    var s1 = cont.createEmptyMovieClip("s1", 1);
    s1.beginFill(0xFF0000, 20);
    s1.lineTo(w/2, h/2);
    s1.lineTo(0, h);
    s1.lineTo(0, 0);
    s1.endFill();
    
    var s2 = cont.createEmptyMovieClip("s2", 2);
    s2.beginFill(0x00FFFF, 20);
    s2.lineTo(w, 0);
    s2.lineTo(w/2, h/2);
    s2.lineTo(0, 0);
    s2.endFill();
    
    cont.moveTo(w, h);
    
    var s3 = cont.createEmptyMovieClip("s3", 3);
    s3.beginFill(0x00FF00, 20);
    s3.lineTo(w/2, h/2);
    s3.lineTo(0, h);
    s3.lineTo(w, h);
    s3.endFill();
    
    var s4 = cont.createEmptyMovieClip("s4", 4);
    s4.beginFill(0xFF00FF, 20);
    s4.lineTo(w/2, h/2);
    s4.lineTo(w, 0);
    s4.lineTo(w, h);
    s4.endFill();
    
    s1._alpha = 20;
    s3._alpha = 20;
    s2._alpha = 20;
    s4._alpha = 20;
    
    cont._x = Stage.width/2-cont._width/2;
    cont._y = Stage.height/2-cont._height/2;
    cont.onMouseMove = function() {
    	var x = this._xmouse;
    	var y = this._ymouse;
    	s1._alpha = 20;
    	s3._alpha = 20;
    	s2._alpha = 20;
    	s4._alpha = 20;
    	if (x>w || y>h || x<0 || y<0) {
    	} else if (x/r>y) {
    		if ((w-x)/r>y) s2._alpha = 100;
    		else s4._alpha = 100;
    	} else {
    		if (x/r>h-y) s3._alpha = 100;
    		else s1._alpha = 100;
    	}
    };

  7. #7
    Senior Member Boombanguk's Avatar
    Join Date
    Jun 2000
    Posts
    1,194
    thanks everyone for your help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center