home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!ferkel.ucsb.edu!taco!rock!stanford.edu!agate!spool.mu.edu!sgiblab!munnari.oz.au!uniwa!watson
- From: watson@maths.uwa.oz.au (David Watson)
- Newsgroups: comp.graphics
- Subject: Re: Point inside a triangle
- Date: 16 Nov 1992 23:50:04 GMT
- Organization: The University of Western Australia
- Lines: 44
- Message-ID: <1e9c3cINN4hi@uniwa.uwa.edu.au>
- References: <7bv1svg@rpi.edu> <BxsLL1.5H5@slipknot.rain.com> <1992Nov16.115211.23169@sophia.smith.edu>
- NNTP-Posting-Host: madvax.maths.uwa.oz.au
-
- orourke@sophia.smith.edu (Joseph O'Rourke) writes:
-
- >In article <BxsLL1.5H5@slipknot.rain.com> robert@slipknot.rain.com.UUCP (Robert Reed) writes:
- >>The standard edge crossing test, which works for any other polygon, should work
- >>fine here. See page 34, Foley and van Dam Computer Graphics. In brief you
- >>count the number of polygon edges that cross a ray drawn from the point to
- >>infinity. An odd number of crossings means the point is interior.
-
- > Although this is a matter of taste, I find this inferior to
- >the algorithm that I use, which tests if the point is left-of-or-on
- >the three directed lines determined by pairs of vertices of the triangle.
- >In the edge crossing code, one must deal with the ray crossing a
- >vertex, being parallel to an edge, being collinear with an edge:
-
- > /\
- > / \
- > / \
- > *---+------+------>
-
- >oding these special cases requires care. The other method has no
- >no comparable special cases. ut all methods, as someone else pointed
- >out, are subject to tricky precision issues, because coordinates
- >are multiplied.
-
- A fail-proof method is to compute the barycentric coordinates. For a
- triangle {(x1,y1), (x2,y2), (x3,y3)} and some point (x0,y0), calculate
-
- b0 = (x2 - x1) * (y3 - y1) - (x3 - x1) * (y2 - y1)
- b1 = ((x2 - x0) * (y3 - y0) - (x3 - x0) * (y2 - y0)) / b0
- b2 = ((x3 - x0) * (y1 - y0) - (x1 - x0) * (y3 - y0)) / b0
- b3 = ((x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0)) / b0
-
- Then if b1, b2, and b3 are all > 0, (x0,y0) is strictly inside the
- triangle; if bi = 0 and the other two coordinates are positive, (x0,y0)
- lies on the edge opposite (xi,yi); if bi and bj = 0, (x0,y0) lies on
- (xk,yk); if bi < 0, (x0,y0) lies outside the edge opposite (xi,yi);
- if all three coordinates are negative, something else is wrong. This
- method does not depend on the cyclic order of the vertices.
-
- --
- Dave Watson Internet: watson@maths.uwa.edu.au
- Department of Mathematics
- The University of Western Australia Tel: (61 9) 380 3359
- Nedlands, WA 6009 Australia. FAX: (61 9) 380 1028
-