Sie sind auf Seite 1von 4

See

discussions, stats, and author profiles for this publication at: https://www.researchgate.net/publication/238807713

A point-in-polygon program

Article in Advances in Engineering Software (1978) January 1985


DOI: 10.1016/0141-1195(85)90094-4

CITATIONS READS

7 514

1 author:

Scott William Sloan


University of Newcastle
332 PUBLICATIONS 6,927 CITATIONS

SEE PROFILE

Some of the authors of this publication are also working on these related projects:

Computational modelling of hydraulic fracture View project

Third Medium Contact Mechanics View project

All content following this page was uploaded by Scott William Sloan on 29 July 2014.

The user has requested enhancement of the downloaded file.


A point-in-polygon program

S. W. S L O A N

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

is closer t o the side than either of the vertices only if the


This n o t e describes an i m p r o v e d version of the normal from the point to the side strikes the side between
N o r d b e c k and R y s t e d t a l g o r i t h m f o r d e t e r m i n i n g its vertices. Otherwise the point is closer to one of the
w h e t h e r a p o i n t is inside a polygon of a r b i t r a r y vertices.
shape. A F O R T R A N 77 i m p l e m e n t a t i o n o f the Consider the points P and Q shown inside the concave
m e t h o d is given. polygon of Fig. 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) ( x i + l - Xp) > 0 (1)
A ~ -
2 (Yi - - Y p ) (Yi + 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 o f x p - x i is not
equal to the sign of x p - 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 o !
f
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 f r o m P 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 + [ Y l p + 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 s o , 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 ( y 2 - - Y l ) 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 = Y 2 - - Y l
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 F O R T R A N 77 POINT-IN-POLYGON C
. FALSE.
PROGRAM SNEAR=

MINDST=D
SUBROUTINE PINPOL( N, SMALLD, LARGED, X, Y, XPOINT, YPO INT, M I N D S T ) J=I
C END IF
************************************************************************
ELSE IF( T. LE. C00001 )THEN
C S U B P R O G R A M P INPOL C
C C H ~ C K IF P O I N T IS INSIDE A GENERAL POLYGON C NORMAL INTERSECTS SIDE
C C
C INPUT p A R A M e T E R S : DX=XlP+T *X21
C DY=YIP+T-Y2 l
C 'N' IS THE N U M B E R OF S I D E S / V E R T I C E S D E F I N I N G THE P O L Y G O N D=DX *DX+DY*DY
C 'SMALLD' IS A SMALL DOUBLE P R E C I S I O N N U M B E R IF ( D, LT. M I N D S T )THEN
C 'LARGED' IS A LARUB DOUBLE P R E C I S I O N N D M B E R C
C 'X' IS A V E C T O R OF N O D A L X - C O O R D S ( A N T I C L O C K W I S E ORDER) C POINT IS C L O S E R TO THIS SIDE THAN TO A N Y
C 'Y' IS A V E C T O R OF N O D A L Y - C O O R D S ( A N T I C L ~ C K W I S E ORDER) C OTHZR SIDE OR V E R T E X
C BOTH OF T ~ E S E V E C T O R S MUST HE OF L E N G T H N+2 W H ~ R E 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 - C O O R D OF T H E P O I N T TO BE T E S T E D J--I
C 'Y~OINT' IS THE Y - C O O R D OF TRE P O I N T TO BE T E S T E D END IF
C E N D IF
C OUTPUT pARAMETERS : iO C O N T I N U E
C MINDST.-SQRT (M I N D S T )
C 'MIRDST'TH~ DISTANCE F R O M TH~ P O I N T TO TH~ N E A R ~ S T P O I N T IF( MINDST. LT. ~ )T~N
C ON TH~ P O L Y G O N
C
C IF 'MINDST' IS LT ZERO THEN P O I N T IS O U T S I D E THE P O L Y G O N
C POINT IS ON SIDE OF P O L Y G O N
C IF 'MINDST' IS EQ ZERO T H E N P O I N T IS ON A SIDE OF TH~ P O L Y G O N
C
C IF 'MINDST' IS GT ZERO T H E N P O I N T IS INSIDE TR~ P O L Y G O N
MINDST~C000O0
c
ELSE
c t,mTEs : IF( S N E A R )TNE~
c C
c AN IMPROVED VERSION OF TNE ALGO~TIm~ oF NORDSECX A.D N'mSTm~T C P O I N T IS C L O S E R TO ITS N E A R Z S T SIDE T H A N TO ITS N E A R E S T
C VERTEX, C H E C K IF P O I N T IS TO L E P T O R R I G H T O F T R I S SIDE
c C IF P O I N T IS TO L E F T OF SIDE IT IS INSIDE POLYGON, E L S E
INTEGER N,I,J
C P O I N T IS O U T S I D E P O L Y G O N
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 ), Y P O I ~ T )
+ MINDST, XI, YI, XIP, YIP, X21, Y21, T, DX, DY, MI N D S T ~ S IGN( MIMDST, A R E A )
+ C00000, C00001 ELSE
C C
LOGICAL SNEAR C P O I N T IS C L O S E R TO ITS N E A R E S T V E R T E X THAN ITS N E A R E S T SIDE,
C C C H E C K IF MF,A R Z S T V E R T E X IS C O N C A V E
p ~ ( C00O00--O. 0DO, C IF THZ N E A R E S T V E R T E X IS C O N C A V E T ~ E N P O I N T IS INSIDE T H E
+ C00001-1. ODO ) C POLYGON, E L S E THZ P O I N T IS Oi2rSIDE T H E P O L Y G O N
C C
C 'SNEAR' IS .TR~/E. IF D I S T A N C E TO N E A R E S T SIDE IS LESS T H A N IF( J. EQ. 1 )THEN
C D I S T A N D E TO NEAREST JmN+l
C 'SMEAR' IS .FALSE. IF D I S T A N C E TO N E A R E S T V E R T E X IS LESS THAH E N D IF
C D I S T A N C E TO N E A R E S T SIDE AREA-DET( X( J + l ), X( J ), X( J-i ), Y( J+l ), Y( J ), Y( J-i ) )
C 'MINDST' IS SQ~&RE O F D I S T A N C E TO C L O S E S T P O I N T O N T H E P O L Y G O N MINDST--S ZGN( ~U~DST, AREA)
C E N D IF
MINDST--LARG~D E N D IF
C C
C LOOP OVER EACK SIDE D E F I N I N G POLYGON END
C FDIqCTION D E T ( X I , X 2 , X S , Y I o Y 2 , Y 3
)
DO i0 I'I,N C
C C.==.=w.*~**~...****.~ ~**=*=.-****-*-~**'*~*'''='='******w*'w''~**'**"
C S T A R T OF SIDE ~ COORDS (XI,YI) C FDHCTXON DET
C END OF SIDE ~ COORDS (X2,Y2) C CO~UTE T W I C E ' ~ A R E A O F T H E T R I A N G L E IXl"IHED B Y T ~ POINTS
C P O I N T H A S C O O R D S (XI~OINT,YPOIMT) C W I T H C O O R D S (XI,1), (X2,1~2) A N D (XS,YS) U S I N G ~ H ] & N T
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 P O I N T 1
XIP-XI-XPOINT C 'X2,Y2' COORDS OF P O I N T 2
yIP--YI-TPOINT C 'XS,YS' COORDS OF P O I N T 3
C C
C P O I N T S ON I N F I N I T E LINE D E F I N E D BY C OUTPUT pARAMETERS I
C X-XI+T'( X I - X 2 ) C
C Y ' Y I + T " (Y I - Y 2 ) C 'DET' T W I C E T H ~ A R E A OF ~ TRIANGLE DEFINED BY T H Z T H R E E POINTS
C C
C T w O A T (XI,YI) C NOTES :
C T ~ I A T (X2,Y2) C
C FIND WRERE NORMAL pASSING THROOG~ (XPOINT,YPOINT) C D E T IS P O S I T I V E IF P O I N T S 1,2 A N D 3 D E F I N E T R I A N G L E IN
C I N T E R S E C T S INFINITE LINE C A N T I ~ SE O R D E R
C C DET IS N E G A T I V E IF POINTS 1,2 A N D 3 D E F I N E T R I A N G L E IN
T~-( X I P "X21+YI]~ *Y21 )/( X21 wX21+Y21 "Y21 ) C CLOCKWISE ORDER
IF( T. LT. C00000 )T~EN C D E T IS Z E R O IF A T L E A S T T W O OF THE P O I N T S A R E C O I N C I D E N T O R IF
C C A L L THREE POINTS A R E C O L L I N E A R
C N O R M A L DOES NOT I N T E R S E C T SIDE C
C P O I N T IS C L O S E S T TO V E R T E X (XI,YI)
C C O M P U T E SQUARE OF D I S T A N C E TO TEIS V E R T E X C
C DOUBLE PRECISION XI,X2,XS,YI,Y2,YS,DET
D.-XIP *XIP+YIP w1P C
IF( D L T M I N D S T )T~EN DET,-( XI-X3 )w( Y2-Y3 )-( X2-X3 )w( YI-Y3 )
C C
C L O S E R TO (XI,Y1) O'n~ZR

View publication stats

Das könnte Ihnen auch gefallen