-
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!
-
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
-
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...
-
Senior Member
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.
-
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";
-
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
-
Senior Member
mine requires less computations and in fact works for any concave polygon with holes.
-
Senior Member
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.
-
Wow! Thats really cool! How did you do that?
-
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.
-
Senior Member
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?
-
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.
-
Senior Member
Last edited by realMakc; 08-21-2009 at 07:05 AM.
-
ahh, thanks for the correction
-
Senior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|