<<

A point-in-polygon program

S. W. SLOAN

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

is closer to the side than either of the vertices only if the This note describes an improved version of the normal from the point to the side strikes the side between Nordbeck and Rystedt algorithm for determining its vertices. Otherwise the point is closer to one of the whether a point is inside a polygon of arbitrary vertices. shape. A FORTRAN 77 implementation of the Consider the points P and Q shown inside the concave method is given. polygon of . 2. Point P is closer to its nearest side (side 2-3) than its nearest vertex (vertex 2). Conversely, point Q Key words: Point-in-polygon, program 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, Figure 1. Distance from a point to a line segment then a point which is inside the polygon will always lie to the left of each side. Let Vi 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) (xi+l- Xp) > 0 (1) A ~ - 2 (Yi -- Yp) (Yi + 1 -- Yp) where (xp, yp) are the co-ordinates of the point P. If A is equal to , 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 f breaks down and it is necessary to devise an alternative o ° ! algorithm. One of the most useful theorems for determin- ! p@ 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. Figure 2. Nearest sides and nearest vertices for points P Accepted July 1984. Discussion closes March 1985. 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 cate P is closest to vertex 1. Values of t greater than 1 indi- arbitrary shape consists of two parts: cate that P is closest to vertex 2. As each side is t~ealed in turn, in anticlockwise order, intersections fo~ which t ~s (1) A point P is inside a polygon if it lies closer to its greater than 1 may be ignored. For cases where t is between nearest side V/Vz.+1 than to its nearest vertex and the 0 and 1, the normal distance fromP to the side is given by: area of the triangle V~.V/+IP is positive. (2) A point P is inside a polygon if it lies closer to its near- d=N/~lp+ tX2112 + [Ylp+ O'21] ~' (3) est vertex Vi than to its nearest side and the vertex V~. is concave. For cases where t is less than zero, point P is closest to In this definition it is assumed that the polygon is speci- vertex 1 and the appropriate distance is given by: fied by a set of vertices listed in anticlockwise order. The algorithm for implementing the enlarged orientation d = x/X{p + yzp (4) theorem is as follows: By treating each side in turn, equations {2), (3) and (4) (1) Enter with the co-ordinates of the point P and the co- may be used to determine whether the point is closer to its ordinates of the vertices defining the polygon (stored nearest vertex or closer to its nearest side. If for any side it in anticlockwise order). is found that t is between 0 and 1 and d is approximately (2) Loop over each side of the polygon and determine zero in equation (3), then the point lies on this side. If for whether P is closer to a vertex or a side. Store the any side it is found that t is less than zero and d is approxi- number of the vertex or side that P is closest to and the mately zero in equation (4), then the point is coincident corresponding distance. If the distance from P to its with vertex 1. nearest side or nearest vertex is zero, then P lies on a The advantages of the formulation above, as compared side of the polygon. with that of Nordbeck and Rystedt, x is that the nearest side (3) If P is closer to its nearest side than to its nearest or nearest vertex is found in one sweep through all the sides vertex, compute the area of the triangle ViVi+IP where of the polygon (together with the corresponding distance). VtV/+ 1 are the vertices defining the nearest side. If this In the Nordbeck and Rystedt ~ implementation, two sweeps area is positive, P is inside the polygon. Else P is out- are conducted; one to find the nearest side and one to find side the polygon. the nearest vertex. Note that whilst looping over each side (4) If P is closer to its nearest vertex than to its nearest of the polygon to find the nearest side or nearest vertex, side, determine whether this vertex is concave. If so,P costly square root computations may be avoided by com- is inside the polygon. Else P is outside the polygon. paring the squares of the distances. A FORTRAN 77 implementation of the above scheme is given in Appendix 1. Subroutine PINPOL returns a value IMPLEMENTATION MINDST which indicates the position of the point with respect to the polygon. MINDST is the distance to the near- In step (2) of the above algorithm, it is necessary to deter- est point on the polygon. If MINDST is less than zero, the mine whether a point is closer to a side or a vertex of the point is outside the polygon. If MINDST is equal to zero polygon. This step may be implemented conveniently by the point lies on a side of the polygon. If MINDST is considering each side of the polygon as a line segment, and greater than zero, the point is inside the polygon. This expressing each line segment as an infinite parametric line representation is convenient for many applications as it of the form: allows an additional check to see if the point in question x = x 1 + t(x2 - x 1) lies within a prescribed distance from the edges of the polygon. The function DET uses the determinant formula Y =Ya + t(y2--Yl) given in equation (1) to evaluate twice the area of the where (xl, Yl), (x2,y2) are the co-ordinates of the vertices triangle defined by three points with co-ordinates (xt, Yl), 1 and 2. The parameter t ranges from 0 at x~ to 1 at x2. (x2,Ya) and (x3, y3), i.e.: With reference to Fig. 1, it is possible to calculate where the normal passing through point P strikes the infinite line 2A_[(xl-x3) (x2--x3) 1 passing through points 1 and 2. In terms of the parameter t, (yl-y3) (y~-y3) the intersection point is given by: The implementation is written in double precision arith- t = - [xlpX21 -}- YlpY21] (2) metic and conforms to standard FORTRAN 77 syntax. It X21 + y21 may be changed to simple precision arithmetic by replacing all DOUBLE PRECISION type declarations with REAL where type declarations and changing the constants defined in X21 = X 2 -- X 1 the PARAMETER statement. Y21 =Y2 --Yl Xlp = X 1 -- Xp REFERENCE

Ylp =Yl --Yp 1 Nordbeek, S. and Rydstedt, B. Computer cartography point-in- polygon programs, BIT 1967, 7, 39 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- See overleaf for Appendix with Program Listing

46 Adv. Eng. Software, 1985, Vol. 7, No. 1 C SIDE APPENDIX 1: A FORTRAN 77 POINT-IN-POLYGON C PROGRAM SNEAR= . FALSE. MINDST=D SUBROUTINE PINPOL( N, SMALLD, LARGED, X, Y, XPOINT, YPO INT, MINDST ) J=I C END IF ************************************************************************ ELSE IF( T. LE. C00001 )THEN C SUBPROGRAM P INPOL C C CH~CK IF POINT IS INSIDE A GENERAL POLYGON C NORMAL INTERSECTS SIDE C C C INPUT pARAMeTERS : DX=XlP+T *X21 C DY=YIP+T-Y2 l C 'N' IS THE NUMBER OF SIDES/VERTICES DEFINING THE POLYGON D=DX *DX+DY*DY C 'SMALLD' IS A SMALL DOUBLE PRECISION NUMBER IF ( D, LT. MINDST )THEN C 'LARGED' IS A LARUB DOUBLE PRECISION NDMBER C C 'X' IS A VECTOR OF NODAL X-COORDS (ANTICLOCKWISE ORDER) C POINT IS CLOSER TO THIS SIDE THAN TO ANY C 'Y' IS A VECTOR OF NODAL Y-COORDS (ANTICL~CKWISE ORDER) C OTHZR SIDE OR VERTEX C BOTH OF T~ESE VECTORS MUST HE OF LENGTH N+2 WH~RE C C X( N+I )--X( 1 ), X( N+2 )--X( 2 ) SNEAR--. TRUE. C Y( N+I )=Y( 1 ), Y( N+2 )=Y( 2 ) MINDST--D C ')[POINT' IS TH~ X-COORD OF THE POINT TO BE TESTED J--I C 'Y~OINT' IS THE Y-COORD OF TRE POINT TO BE TESTED END IF C END IF C OUTPUT pARAMETERS : iO CONTINUE C MINDST.-SQRT ( MINDST ) C 'MIRDST'TH~ DISTANCE FROM TH~ POINT TO TH~ NEAR~ST POINT IF( MINDST. LT. ~ )T~N C ON TH~ POLYGON C C IF 'MINDST' IS LT ZERO THEN POINT IS OUTSIDE THE POLYGON C POINT IS ON SIDE OF POLYGON C IF 'MINDST' IS EQ ZERO THEN POINT IS ON A SIDE OF TH~ POLYGON C C IF 'MINDST' IS GT ZERO THEN POINT IS INSIDE TR~ POLYGON MINDST~C000O0 c ELSE c t,mTEs : c IF( SNEAR )TNE~ C c AN IMPROVED VERSION OF TNE ALGO~TIm~ oF NORDSECX A.D N'mSTm~T 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 C IF POINT IS TO LEFT OF SIDE IT IS INSIDE POLYGON, ELSE INTEGER N,I,J C POINT IS OUTSIDE POLYGON c C DOUBLE PRECISION X( *),Y( *),XPOINT,YPOINT,SM~%LLD,LARGED,D,AREA,DET, AREA-DET( X( J ), X( J+l ), X~OINT, ¥( J ), Y ( J+l ), YPOI~T ) + MINDST, XI, YI, XIP, YIP, X21, Y21, T, DX, DY, MI NDST~S IGN( MIMDST, AREA ) + C00000, C00001 ELSE C C LOGICAL SNEAR C POINT IS CLOSER TO ITS NEAREST VERTEX THAN ITS NEAREST SIDE, C C CHECK IF MF,ARZST VERTEX IS CONCAVE p~( C00O00--O. 0DO, C IF THZ NEAREST VERTEX IS CONCAVE T~EN POINT IS INSIDE THE + C00001-1. ODO ) C POLYGON, ELSE THZ POINT IS Oi2rSIDE THE POLYGON C C C 'SNEAR' IS .TR~/E. IF DISTANCE TO NEAREST SIDE IS LESS THAN IF( J. EQ. 1 )THEN C DISTANDE TO NEAREST JmN+l C 'SMEAR' IS .FALSE. IF DISTANCE TO NEAREST VERTEX IS LESS THAH END IF C DISTANCE TO NEAREST SIDE AREA-DET( X( J+l ), X( J ), X( J-i ), Y( J+l ), Y( J ), Y( J-i ) ) C 'MINDST' IS SQ~&RE OF DISTANCE TO CLOSEST POINT ON THE POLYGON MINDST--S ZGN( ~U~DST, AREA) C END IF MINDST--LARG~D END IF C C C LOOP OVER EACK SIDE DEFINING POLYGON END C FDIqCTION DET(XI,X2,XS,YIoY2,Y3 ) DO i0 I'I,N C C C.==.=w.*~**~...****.~ ~**=*=.-****-*-~**'*~*'''='='******w*'w''~**'**" C START OF SIDE ~ COORDS (XI,YI) C FDHCTXON DET C END OF SIDE ~ COORDS (X2,Y2) C CO~UTE TWICE '~ AREA OF THE TRIANGLE IXl"IHED BY T~ POINTS C POINT HAS COORDS (XI~OINT,YPOIMT) C WITH COORDS (XI,¥1), (X2,1~2) AND (XS,YS) USING ~H]&NT C C FORMULA XI'X( I ) C YI-Y( I ) C INPUT pARAMETERS : X/I-X( I+i )-XI C Y21-Y( I+l )-YI C 'XI,YI' COORDS OF POINT 1 XIP-XI-XPOINT C 'X2,Y2' COORDS OF POINT 2 yIP--YI-TPOINT C 'XS,YS' COORDS OF POINT 3 C C C POINTS ON INFINITE LINE DEFINED BY C OUTPUT pARAMETERS I C X-XI+T'( XI-X2 ) C C Y'YI+T" ( YI-Y2 ) C 'DET' TWICE TH~ AREA OF ~ TRIANGLE DEFINED BY THZ THREE POINTS C C C TwO AT (XI,YI) C NOTES : C T~I AT (X2,Y2) C C FIND WRERE NORMAL pASSING THROOG~ (XPOINT,YPOINT) C DET IS POSITIVE IF POINTS 1,2 AND 3 DEFINE TRIANGLE IN C INTERSECTS INFINITE LINE C ANTI~ SE ORDER C C DET IS NEGATIVE IF POINTS 1,2 AND 3 DEFINE TRIANGLE IN T~-( XIP "X21+YI]~ *Y21 )/( X21 wX21+Y21 "Y21 ) C CLOCKWISE ORDER IF( T. LT. C00000 )T~EN C DET IS ZERO IF AT LEAST TWO OF THE POINTS ARE COINCIDENT OR IF C C ALL THREE POINTS ARE COLLINEAR C NORMAL DOES NOT INTERSECT SIDE C C POINT IS CLOSEST TO VERTEX (XI,YI) C COMPUTE SQUARE OF DISTANCE TO TEIS VERTEX C C DOUBLE PRECISION XI,X2,XS,YI,Y2,YS,DET D.-XIP *XIP+YIP w¥1P C IF( D •LT •MINDST )T~EN DET,-( XI-X3 ) w( Y2-Y3 )-( X2-X3 ) w( YI-Y3 ) C C CLOSER TO (XI,Y1) O'n~ZR