A Point in a Polygon Program

Embed Size (px)

DESCRIPTION

Describes how to detect a point if it is inside a polygon or not.

Citation preview

  • A point-in-polygon program

    S. W. SLOAN

    Department of Engineering Science, Parks Road, Oxford 0)(1 3P J, UK*

    This note describes an improved version of the Nordbeck and Rystedt algorithm for determining whether a point is inside a polygon of arbitrary shape. A FORTRAN 77 implementat ion of the method is given.

    Key words: Point-in-polygon, program

    is closer to the side than either of the vertices only if the normal from the point to the side strikes the side between its vertices. Otherwise the point is closer to one of the vertices.

    Consider the points P and Q shown inside the concave polygon of Fig. 2. Point P is closer to its nearest side (side 2-3) than its nearest vertex (vertex 2). Conversely, point Q is closer to its nearest vertex (vertex 3) than its nearest side (side 4-5).

    INTRODUCTION

    The problem of determining whether a point lies in, on or outside a polygon is one which occurs frequently in geometrical programming. Applications of point-in-polygon programs include automatic mesh generation for finite element grids and analysis of real estate data.

    A detailed discussion of various approaches to this prob- lem has been given by Nordbeck and Rystedt. l For poly- gons which are strictly convex, it is relatively simple to ascertain whether a point is inside a polygon by using the so-called 'orientation theorem'. If a convex polygon is defined by a collection of vertices in anticlockwise order, then a point which is inside the polygon will always lie to the left of each side. Let V i and Vi+l denote the vertices defining a given side with co-ordinates (Xi, Yi) and (xi+ 1, Yi+x), then a point P lies to the left of the side if the area of the triangle defined by V/V/+ 1P is positive, i.e. if:

    1 (xi - xp) (x i+l- Xp) > 0 (1) A ~ -

    2 (Yi - - Yp) (Y i + 1 - - Yp) where (xp, yp) are the co-ordinates of the point P. If A is equal to zero, P lies on the side if the sign ofxp-x i is not equal to the sign of xp- xi+l. IfA is less than zero, P is to the right of the side and outside the polygon.

    In the case of concave polygons, this simple approach breaks down and it is necessary to devise an alternative algorithm. One of the most useful theorems for determin- ing the position of a point relative to a polygon of arbitrary shape is the 'enlarged orientation theorem' devised by Nordbeck and Rystedt. l This note describes an efficient implementation of this theorem.

    THE ENLARGED ORIENTATION THEOREM

    Consider a typical side of a polygon, defined by a line seg- ment with vertices 1 and 2, as shown in Fig. 1. The point P

    * Now at Department of Civil Engineering and Surveying, University of Newcastle, NSW 2308, Australia.

    Accepted July 1984. Discussion closes March 1985.

    Figure 1. Distance from a point to a line segment

    o f ! ! p@

    Figure 2. Nearest sides and nearest vertices for points P and Q

    0141-1195[85]010045-03 $2.00 1985 CML Publications Adv. Eng. Software, 198.5, VoL 7, No. 1 45

  • The enlarged orientation theorem for a polygon of arbitrary shape consists of two parts:

    (1) A point P is inside a polygon if it lies closer to its nearest side V/Vz.+ 1 than to its nearest vertex and the area of the triangle V~.V/+IP is positive.

    (2) A point P is inside a polygon if it lies closer to its near- est vertex V i than to its nearest side and the vertex V~. is concave.

    In this definition it is assumed that the polygon is speci- fied by a set of vertices listed in anticlockwise order.

    The algorithm for implementing the enlarged orientation theorem is as follows:

    (1) Enter with the co-ordinates of the point P and the co- ordinates of the vertices defining the polygon (stored in anticlockwise order).

    (2) Loop over each side of the polygon and determine whether P is closer to a vertex or a side. Store the number of the vertex or side that P is closest to and the corresponding distance. If the distance from P to its nearest side or nearest vertex is zero, then P lies on a side of the polygon.

    (3) If P is closer to its nearest side than to its nearest vertex, compute the area of the triangle ViVi+IP where VtV/+ 1 are the vertices defining the nearest side. If this area is positive, P is inside the polygon. Else P is out- side the polygon.

    (4) If P is closer to its nearest vertex than to its nearest side, determine whether this vertex is concave. If so,P is inside the polygon. Else P is outside the polygon.

    IMPLEMENTATION

    In step (2) of the above algorithm, it is necessary to deter- mine whether a point is closer to a side or a vertex of the polygon. This step may be implemented conveniently by considering each side of the polygon as a line segment, and expressing each line segment as an infinite parametric line of the form:

    x = x 1 + t(x2 - x 1)

    Y =Ya + t(y2--Yl)

    where (xl, Yl), (x2,y2) are the co-ordinates of the vertices 1 and 2. The parameter t ranges from 0 at x~ to 1 at x2. With reference to Fig. 1, it is possible to calculate where the normal passing through point P strikes the infinite line passing through points 1 and 2. In terms of the parameter t, the intersection point is given by:

    t = - [xlpX21 -}- YlpY21] (2) X21 + y21

    where

    X21 = X 2 -- X 1

    Y21 =Y2 - -Y l

    Xlp = X 1 -- Xp

    Y lp =Yl - -Yp Values of t between 0 and 1 indicate that P is closer to the side than either of the vertices. Values of t less than 0 indi-

    cate P is closest to vertex 1. Values of t greater than 1 indi- cate that P is closest to vertex 2. As each side is t~ealed in turn, in anticlockwise order, intersections fo~ which t ~s greater than 1 may be ignored. For cases where t is between 0 and 1, the normal distance f romP to the side is given by:

    d=N/~lp+ tX2112 + [Y lp+ O'21] ~' (3)

    For cases where t is less than zero, point P is closest to vertex 1 and the appropriate distance is given by:

    d = x/X{p + yzp (4)

    By treating each side in turn, equations {2), (3) and (4) may be used to determine whether the point is closer to its nearest vertex or closer to its nearest side. If for any side it is found that t is between 0 and 1 and d is approximately zero in equation (3), then the point lies on this side. If for any side it is found that t is less than zero and d is approxi- mately zero in equation (4), then the point is coincident with vertex 1.

    The advantages of the formulation above, as compared with that of Nordbeck and Rystedt, x is that the nearest side or nearest vertex is found in one sweep through all the sides of the polygon (together with the corresponding distance). In the Nordbeck and Rystedt ~ implementation, two sweeps are conducted; one to find the nearest side and one to find the nearest vertex. Note that whilst looping over each side of the polygon to find the nearest side or nearest vertex, costly square root computations may be avoided by com- paring the squares of the distances.

    A FORTRAN 77 implementation of the above scheme is given in Appendix 1. Subroutine PINPOL returns a value MINDST which indicates the position of the point with respect to the polygon. MINDST is the distance to the near- est point on the polygon. If MINDST is less than zero, the point is outside the polygon. If MINDST is equal to zero the point lies on a side of the polygon. If MINDST is greater than zero, the point is inside the polygon. This representation is convenient for many applications as it allows an additional check to see if the point in question lies within a prescribed distance from the edges of the polygon. The function DET uses the determinant formula given in equation (1) to evaluate twice the area of the triangle defined by three points with co-ordinates (xt, Yl), (x2,Ya) and (x3, y3), i.e.:

    2A_[ (x l -x3) (x2--x3) 1 (yl-y3) (y~-y3)

    The implementation is written in double precision arith- metic and conforms to standard FORTRAN 77 syntax. It may be changed to simple precision arithmetic by replacing all DOUBLE PRECISION type declarations with REAL type declarations and changing the constants defined in the PARAMETER statement.

    REFERENCE

    1 Nordbeek, S. and Rydstedt, B. Computer cartography point-in- polygon programs, BIT 1967, 7, 39

    See overleaf for Appendix with Program Listing

    46 Adv. Eng. Software, 1985, Vol. 7, No. 1

  • APPENDIX 1: A FORTRAN 77 POINT-IN-POLYGON

    PROGRAM SUBROUTINE PINPOL( N, SMALLD, LARGED, X, Y, XPOINT, YPO INT, MINDST )

    C ************************************************************************

    C SUBPROGRAM P INPOL C CH~CK IF POINT IS INSIDE A GENERAL POLYGON

    C C INPUT pARAMeTERS : C C 'N' IS THE NUMBER OF S IDES/VERTICES DEFINING THE POLYGON C 'SMALLD' IS A SMALL DOUBLE PRECIS ION NUMBER

    C 'LARGED' IS A LARUB DOUBLE PRECISION NDMBER C 'X' IS A VECTOR OF NODAL X-COORDS (ANTICLOCKWISE ORDER) C 'Y' IS A VECTOR OF NODAL Y-COORDS (ANTICL~CKWISE ORDER) C BOTH OF T~ESE VECTORS MUST HE OF LENGTH N+2 WH~RE C X( N+I )--X( 1 ), X( N+2 )--X( 2 ) C Y( N+I )=Y( 1 ), Y( N+2 )=Y( 2 ) C ')[POINT' IS TH~ X-COORD OF THE POINT TO BE TESTED C 'Y~OINT' IS THE Y-COORD OF TRE POINT TO BE TESTED C C OUTPUT pARAMETERS : C C 'MIRDST'TH~ DISTANCE FROM TH~ POINT TO TH~ NEAR~ST POINT C ON TH~ POLYGON C IF 'MINDST' IS LT ZERO THEN POINT IS OUTSIDE THE POLYGON C IF 'MINDST' IS EQ ZERO THEN POINT IS ON A SIDE OF TH~ POLYGON C IF 'MINDST' IS GT ZERO THEN POINT IS INSIDE TR~ POLYGON c c t, mTEs : c c AN IMPROVED VERSION OF TNE ALGO~TIm~ oF NORDSECX A .D N'mSTm~T

    c

    c

    INTEGER N, I , J

    DOUBLE PRECISION X( *),Y( *) ,XPOINT,YPOINT,SM~%LLD,LARGED,D,AREA,DET, + MINDST, XI, YI, XIP, YIP, X21, Y21, T, DX, DY, + C00000, C00001

    C LOGICAL SNEAR

    C p ~ ( C00O00--O. 0DO,

    + C00001-1. ODO ) C C 'SNEAR' IS .TR~/E. IF DISTANCE TO NEAREST SIDE IS LESS THAN C DISTANDE TO NEAREST C 'SMEAR' IS .FALSE. IF DISTANCE TO NEAREST VERTEX IS LESS THAH C DISTANCE TO NEAREST SIDE C 'MINDST' IS SQ~&RE OF DISTANCE TO CLOSEST POINT ON THE POLYGON

    C MINDST--LARG~D

    C C LOOP OVER EACK SIDE DEFINING POLYGON

    C DO i0 I ' I ,N

    C

    C START OF SIDE ~ COORDS (XI ,Y I ) C END OF S IDE ~ COORDS (X2,Y2) C POINT HAS COORDS (XI~OINT,YPOIMT) C

    XI'X( I ) YI-Y( I ) X/I-X( I+i )-XI Y21-Y( I+l )-YI X IP -X I -XPOINT yIP--YI-TPOINT

    C C POINTS ON INFINITE LINE DEFINED BY C X-XI+T'( XI -X2 ) C Y'YI+T" ( Y I -Y2 ) C C TwO AT (XI ,YI) C T~I AT (X2,Y2) C F IND WRERE NORMAL pASSING THROOG~ (XPOINT,YPOINT) C INTERSECTS INFINITE LINE C

    T~-( XIP "X21+YI]~ *Y21 )/( X21 wX21+Y21 "Y21 ) IF( T. LT. C00000 )T~EN

    C C NORMAL DOES NOT INTERSECT SIDE C POINT IS CLOSEST TO VERTEX (XI,YI) C COMPUTE SQUARE OF DISTANCE TO TEIS VERTEX

    C D.-XIP *XIP+YIP w1P IF( D LT MINDST )T~EN

    C CLOSER TO (XI ,Y1) O'n~ZR

    C SIDE

    C SNEAR= . FALSE. MINDST=D J=I

    END IF ELSE IF( T. LE. C00001 )THEN

    C C NORMAL INTERSECTS SIDE

    C DX=XlP+T *X21 DY=YIP+T-Y2 l D=DX *DX+DY*DY IF ( D, LT. MINDST )THEN

    C C POINT IS CLOSER TO THIS SIDE THAN TO ANY

    OTHZR SIDE OR VERTEX C C

    SNEAR--. TRUE. MINDST--D J--I

    END IF END IF

    iO CONTINUE MINDST.-SQRT ( MINDST ) IF( MINDST. LT. ~ )T~N

    C C POINT IS ON SIDE OF POLYGON C

    MINDST~C000O0 ELSE

    IF( SNEAR )TNE~ C C POINT IS CLOSER TO ITS NEARZST SIDE THAN TO ITS NEAREST C VERTEX, CHECK IF POINT IS TO LEPT OR RIGHT OF TRIS SIDE C IF POINT IS TO LEFT OF SIDE IT IS INSIDE POLYGON, ELSE C POINT IS OUTSIDE POLYGON C

    AREA-DET( X( J ), X( J+l ), X~OINT, ( J ), Y ( J+l ), YPOI~T ) MI NDST~S IGN( MIMDST, AREA )

    ELSE C C POINT IS CLOSER TO ITS NEAREST VERTEX THAN ITS NEAREST SIDE, C CHECK IF MF, ARZST VERTEX IS CONCAVE C IF THZ NEAREST VERTEX IS CONCAVE T~EN POINT IS INSIDE THE C POLYGON, ELSE THZ POINT IS Oi2rSIDE THE POLYGON

    C IF( J. EQ. 1 )THEN

    JmN+l END IF AREA-DET( X( J+l ), X( J ), X( J - i ), Y( J+l ), Y( J ), Y( J- i ) ) MINDST--S ZGN( ~U~DST, AREA)

    END IF END IF

    C END FDIqCTION DET(X I ,X2,XS,Y IoY2,Y3 )

    C C.==.=w.*~**~. . . **** .~ ~**=*=. -**** - * -~** ' *~* ' ' '= '= '******w* 'w ' '~** ' **"

    C FDHCTXON DET C CO~UTE TWICE '~ AREA OF THE TRIANGLE IXl"IHED BY T ~ POINTS C WITH COORDS (XI,1), (X2,1~2) AND (XS,YS) US ING ~ H ] & N T C FORMULA C C INPUT pARAMETERS : C C 'XI,YI' COORDS OF POINT 1 C 'X2,Y2' COORDS OF POINT 2 C 'XS,YS' COORDS OF POINT 3 C C OUTPUT pARAMETERS I C C 'DET' TWICE TH~ AREA OF ~ TRIANGLE DEFINED BY THZ THREE POINTS

    C C NOTES : C C DET IS POSIT IVE IF POINTS 1,2 AND 3 DEFINE TRIANGLE IN C A N T I ~ SE ORDER C DET IS NEGATIVE IF POINTS 1,2 AND 3 DEFINE TRIANGLE IN C CLOCKWISE ORDER C DET IS ZERO IF AT LEAST TWO OF THE POINTS ARE COINCIDENT OR IF C ALL THREE POINTS ARE COLL INEAR

    C

    C DOUBLE PRECIS ION XI ,X2,XS,Y I ,Y2 ,YS,DET

    C DET,-( XI-X3 ) w( Y2-Y3 )-( X2-X3 ) w( YI-Y3 )

    C