A Flash Developer Resource Site

Results 1 to 15 of 15

Thread: Return true if a point is within a quadrilateral.

  1. #1
    Member
    Join Date
    Jul 2006
    Posts
    49

    Return true if a point is within a quadrilateral.

    I need to write an if statement (or more) to return either true if a point is inside of a quadrilateral else return false. My quadrilateral can can be in ANY shape or size, not just square, and that's where my problem occurs. However the lines cannot cross, if that helps. I have attempted to come up with a formula, but I have had no luck. I'm not too great with geometry and such, soo... I would be grateful for any help!

  2. #2
    Knows where you live
    Join Date
    Oct 2004
    Posts
    944
    Without seriously delving into possible solutions, my first suggestion would be to use a convex hull algorithm and make sure the target point is not one of the bounds.
    http://en.wikipedia.org/wiki/Convex_hull
    http://en.wikipedia.org/wiki/Convex_hull_algorithms

    I know there are also better ways to do this, such as linear inequalities, however the convex hull algorithms are well documented.
    The greatest pleasure in life is doing what people say you cannot do.
    - Walter Bagehot
    The height of cleverness is to be able to conceal it.
    - Francois de La Rochefoucauld

  3. #3
    Member
    Join Date
    Jul 2006
    Posts
    49
    Hmm...I read through those documents, and I must say...what? I somewhat understand what they are saying...but I don't really know how to use what I read to help me...

  4. #4
    Senior Member realMakc's Avatar
    Join Date
    Oct 2002
    Posts
    927
    another simple solution is to cast a ray out and count intersections with bounding line (if you do have this line). count % 2 will give 0 if outside, and 1 if inside.
    Last edited by realMakc; 03-11-2008 at 06:33 AM.
    who is this? a word of friendly advice: FFS stop using AS2

  5. #5
    Senior Member
    Join Date
    Mar 2001
    Location
    Location : Location
    Posts
    131
    Not done the code for it but something like;

    for quad abcd and point X within;

    angle(Xdc) < angle(adc) AND angle(Xba) < angle(cda)

    OR

    The point X must be within the 'lightcones' of two opposite points of the quadrilateral formed by the two adjacent sides to the point; either b and d (as above) or a and c.
    this[MCr.i + 'm_not_ok']._lyric = "you sing the words but you don't know what they mean";

  6. #6
    Knows where you live
    Join Date
    Oct 2004
    Posts
    944
    Id have to say that TechNoiZ solution is the best one so far.
    The greatest pleasure in life is doing what people say you cannot do.
    - Walter Bagehot
    The height of cleverness is to be able to conceal it.
    - Francois de La Rochefoucauld

  7. #7
    Senior Member realMakc's Avatar
    Join Date
    Oct 2002
    Posts
    927
    mine requires less computations and in fact works for any concave polygon with holes.
    who is this? a word of friendly advice: FFS stop using AS2

  8. #8
    Senior Member realMakc's Avatar
    Join Date
    Oct 2002
    Posts
    927
    ok, it turned out not so simple it is all just +/- and >/==, no atan-s etc, but code gets very hairy... I will soon release it (after I catch all bugs) along with tesselation (which I needed it for (sort of)), so keep an eye on my rss.
    Last edited by realMakc; 08-21-2009 at 07:05 AM.
    who is this? a word of friendly advice: FFS stop using AS2

  9. #9
    Member
    Join Date
    Jul 2006
    Posts
    49
    Wow! Thats really cool! How did you do that?

  10. #10
    Senior Member
    Join Date
    Nov 2003
    Location
    Las Vegas
    Posts
    770
    I'm not sure if it would be faster, but you could check parametric coordinates to insure the point is within one of the two triangles making up the quadrilateral. I use this method for ray/triangle (3d mesh) intersection checks, and it's 100% accurate, and pretty fast.

  11. #11
    Senior Member realMakc's Avatar
    Join Date
    Oct 2002
    Posts
    927
    actually js is right, you could use any triangle hittest, if you split quadrilateral along inner diagonal... but there a problem remains, how would you test that diagonal is inside?
    who is this? a word of friendly advice: FFS stop using AS2

  12. #12
    Senior Member
    Join Date
    Nov 2003
    Location
    Las Vegas
    Posts
    770
    hmm, isn't the shortest diagonal always inside? I'm thinking so in my minds eye, but I don't know if it's true.

  13. #13
    Senior Member realMakc's Avatar
    Join Date
    Oct 2002
    Posts
    927
    * * * * * *
    Last edited by realMakc; 08-21-2009 at 07:05 AM.
    who is this? a word of friendly advice: FFS stop using AS2

  14. #14
    Senior Member
    Join Date
    Nov 2003
    Location
    Las Vegas
    Posts
    770
    ahh, thanks for the correction

  15. #15
    Senior Member rachil0's Avatar
    Join Date
    Jul 2007
    Location
    Columbus, OH.
    Posts
    465
    If you know your quadrilateral (or any N-gon) is convex, if you can use four (or N) cross products to determine if the test point is in the positive or negative halfspace of each edge.

    If you can't guarantee the quadrilateral is convex, split it into the union of two triangles, and then do insideness tests for each triangles (which are 3-gons and guaranteed convex).

    Like makc's picture, there can be a right and wrong way to split a nonconvex quadrilateral. If you do it wrongly, one of the triangles will wind/circulate with the wrong handedness. So after you split, test that both triangles have the same circulation as the original quad (this is just more cross products).

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