home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.programming
- Path: sparky!uunet!paladin.american.edu!europa.asd.contel.com!darwin.sura.net!Sirius.dfn.de!math.fu-berlin.de!informatik.tu-muenchen.de!LRZnews!hns
- From: hns@ldv.e-technik.tu-muenchen.de (Hans Nikolaus Schaller)
- Subject: Re: How to decide if a point is inside a polygon?
- Message-ID: <hns.714926138@ldvhp14>
- Sender: news@news.lrz-muenchen.de (Mr. News)
- Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
- References: <7121@bigbird.hri.com.hri.com>
- Distribution: all
- Date: Thu, 27 Aug 1992 14:35:38 GMT
- Lines: 34
-
- The following idea for an algorithm should work if the corners of
- your polygon are sorted clockwise (or counterclockwise) and the
- polygon is convex:
-
- check if the point is on the "left" side of every line
- going through the vertices of your polygon.
-
- To check, if a point (x,y) is on the "left" side of the oriented line
- from (xa,ya) to (xb,yb),
-
- calculate the angle between (x-xa,y-ya) and the left rotated
- perpendicular vector to (xb-xa,yb-ya).
-
- If the angle is between -90 and 90 degrees, x is on the left side.
-
- To simplify the calculation: x is on the left side, if the cosine
- of the angle is positive. The sign of the cosine can be calculated
- by a scalar product. and the perpendicular vector to (x,y) is (-y,x).
-
- To summarize (let's hope, there is no mistake...):
-
- (x,y) is on the left side if
-
- (x-xa)*(ya-yb)+(y-ya)*(xb-xa) > 0.
- or
- (x-xa)*(ya-yb) > (y-ya)*(xa-xb)
-
- If the first term vanishes, the point is on the line.
-
- By the way, there are many good books about computer graphics that
- describe this problem (and I think, I got the idea described above
- from one of them but don't remember which).
-
- H. Nikolaus Schaller
-