Вы находитесь на странице: 1из 3

A point-in-polygon program

S. W. SLOAN
Department of Engineering Science, Parks Road, Oxford 0)(1 3P J, UK*
Thi s n o t e des cr i bes a n i mp r o v e d vers i on o f t he
No r d b e c k a nd Ry s t e d t a l gor i t hm f o r d e t e r mi n i n g
wh e t h e r a p o i n t is i nsi de a po l y g o n o f a r b i t r a r y
s hape. A F ORT RAN 77 i mp l e me n t a t i o n o f t he
me t h o d is gi ven.
Key words: Point-in-polygon, program
is closer t o the side than either of the vertices only i f the
normal from the point to the side strikes the side between
its vertices. Otherwise the point is closer t o one of the
vertices.
Consider the points P and Q shown inside the concave
pol ygon 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,
t hen 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 , Y i ) 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 - X p) > 0 ( 1)
A ~ -
2 (Yi - - Y p ) ( Y i + 1 - - Y p )
where (xp, yp) are the co-ordinates of the point P. If A is
equal to zero, P lies on the side i f the sign o f x p - x i is not
equal to the sign of x p - xi+l. I f A is less than zero, P is t o
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 t heorem' devised by
Nordbeck and Rystedt. l This not e 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 ori ent at i on t heor em for a pol ygon of
arbi t rary shape consists o f t wo parts:
(1) A poi nt P is inside a pol ygon i f it lies closer t o its
nearest side V/Vz.+ 1 t han t o its nearest vertex and the
area of t he triangle V~.V/+IP is positive.
(2) A poi nt P is inside a pol ygon i f it lies closer t o its near-
est vert ex V i t han t o its nearest side and t he vertex V~.
is concave.
I n this defi ni t i on it is assumed t hat t he pol ygon is speci-
fied by a set of vertices listed in ant i cl ockwi se order.
The al gori t hm for i mpl ement i ng the enlarged or i ent at i on
t heor em is as follows:
(1) Ent er wi t h t he co-ordi nat es of t he poi nt P and t he co-
ordi nat es of t he vertices defining the pol ygon (st ored
in ant i cl ockwi se order).
(2) Loop over each side of t he pol ygon and det ermi ne
whet her P is closer t o a vert ex or a side. St ore the
number of the vert ex or side t hat P is closest t o and t he
cor r espondi ng distance. I f t he distance f r om P t o its
nearest side or nearest vert ex is zero, t hen P lies on a
side o f t he pol ygon.
(3) I f P is closer t o its nearest side t han t o its nearest
vert ex, comput e t he area of the triangle Vi Vi +I P where
VtV/+ 1 are t he vertices defining t he nearest side. I f this
area is positive, P is inside the pol ygon. Else P is out -
side t he pol ygon.
(4) I f P is closer t o its nearest vert ex t han t o its nearest
side, det ermi ne whet her this vert ex is concave. I f s o , P
is inside t he pol ygon. Else P is out si de t he pol ygon.
IMPLEMENTATION
I n st ep (2) o f t he above al gori t hm, it is necessary t o deter-
mi ne whet her a poi nt is closer t o a side or a vert ex of t he
pol ygon. This st ep may be i mpl ement ed conveni ent l y by
consi deri ng each side of t he pol ygon as a line segment , and
expressing each line segment as an infinite paramet ri c line
of t he f or m:
x = x 1 + t ( x2 - x 1)
Y =Y a + t ( y 2 - - Y l )
where ( x l , Yl ) , ( x2, y2) are t he co-ordi nat es of t he vertices
1 and 2. The paramet er t ranges f r om 0 at x~ t o 1 at x2.
With reference t o Fig. 1, it is possible t o calculate where the
nor mal passing t hr ough poi nt P strikes t he infinite line
passing t hr ough poi nt s 1 and 2. In terms of the paramet er t,
t he i nt ersect i on poi nt is given by:
t = - [ x l p X2 1 -}- Y l p Y 2 1 ] ( 2)
X21 + y21
where
X21 = X 2 - - X 1
Y21 = Y2 - - Yl
X l p = X 1 - - Xp
Y l p = Yl - - Y p
Values of t bet ween 0 and 1 indicate t hat P is closer t o t he
side t han ei t her o f t he vertices. Values of t less t han 0 indi-
cate P is closest to vertex 1. Values of t greater than 1 indi-
cate that P is closest t o vertex 2. As each side is t~ealed in
t urn, in ant i cl ockwi se order, i nt ersect i ons fo~ which t ~s
greater t han 1 may be ignored. For cases where t is bet ween
0 and 1, the normal distance f r o mP t o the side is given by:
d = N / ~ l p + tX2112 + [ Yl p + O'21] ~' ( 3)
For cases where t is less t han zero, poi nt P is closest t o
vertex 1 and t he appropri at e distance is given by:
d = x/ X{ p + y z p (4)
By t reat i ng each side in t urn, equat i ons {2), (3) and (4)
may be used t o det ermi ne whet her t he poi nt is closer t o its
nearest vert ex or closer t o its nearest side. I f for any side it
is f ound t hat t is bet ween 0 and 1 and d is appr oxi mat el y
zero in equat i on (3), t hen the poi nt lies on this side. I f for
any side it is f ound t hat t is less t han zero and d is approxi -
mat el y zero in equat i on (4), t hen the poi nt is coi nci dent
wi t h vert ex 1.
The advantages of t he f or mul at i on above, as compar ed
wi t h t hat o f Nor dbeck and Ryst edt , x is t hat t he nearest side
or nearest vertex is f ound in one sweep t hr ough all the sides
o f t he pol ygon ( t oget her wi t h t he correspondi ng distance).
In the Nor dbeck and Ryst edt ~ i mpl ement at i on, t wo sweeps
are conduct ed; one t o fi nd the nearest side and one t o find
the nearest vertex. Not e t hat whilst l oopi ng over each side
of the pol ygon t o find t he nearest side or nearest vertex,
cost l y square r oot comput at i ons may be avoi ded by com-
paring t he squares of the distances.
A FORTRAN 77 i mpl ement at i on of the above scheme is
given in Appendi x 1. Subrout i ne PI NPOL ret urns a value
MINDST whi ch indicates t he posi t i on of the poi nt with
respect t o t he pol ygon. MINDST is the distance t o the near-
est poi nt on the pol ygon. I f MI NDST is less t han zero, the
poi nt is out si de the pol ygon. I f MINDST is equal t o zero
the poi nt lies on a side of the pol ygon. I f MINDST is
greater t han zero, t he poi nt is inside t he pol ygon. This
represent at i on is conveni ent for many appl i cat i ons as it
allows an addi t i onal check t o see if the poi nt in quest i on
lies within a prescri bed distance f r om t he edges o f t he
pol ygon. The f unct i on DET uses the det ermi nant formul a
given in equat i on (1) t o evaluate twice the area of the
triangle defi ned by three poi nt s with co-ordi nat es (xt, Yl),
( x 2 , Ya ) and (x3, y3), i.e.:
2 A _ [ ( x l - x 3 ) ( x2- - x3) 1
(yl-y3) (y~-y3)
The i mpl ement at i on is wri t t en in doubl e precision arith-
metic and conf or ms t o st andard FORTRAN 77 synt ax. It
may be changed t o simple precision ari t hmet i c by replacing
all DOUBLE PRECI SI ON t ype declarations wi t h REAL
t ype declarations and changi ng t he const ant s defi ned in
t he PARAMETER st at ement .
REFERENCE
1 Nordbeek, S. and Rydstedt, B. Comput er cartography point-in-
polygon programs, B I T 1967, 7, 39
See overleaf for Appendix with Program Listing
46 Adv . Eng. Sof t war e, 1985, Vol. 7, No. 1
APPENDIX 1 : A F O R T R A N 7 7 POINT-IN-POLYGON
PROGRAM
S U B R O U T I N E P I N P O L ( N, S M A L L D , L A R G E D , X, Y, X P O I N T , Y P O INT, M I N D S T )
C
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
C S U B P R O G R A M P I N P O L
C C H ~ C K I F P O I N T IS I N S I D E A G E N E R A L P O L Y G O N
C
C I N P U T p A R A M e T E R S :
C
C 'N' I S T H E N U M B E R O F S I D E S / V E R T I C E S D E F I N I N G T H E P O L Y G O N
C ' S M A L L D ' I S A S M A L L D O U B L E P R E C I S I O N N U M B E R
C ' L A R G E D ' I S A L A R U B D O U B L E P R E C I S I O N N D M B E R
C 'X' I S A V E C T O R O F 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 O R D E R )
C 'Y' I S A V E C T O R O F N O D A L Y - C O O R D S ( A N T I C L ~ C K W I S E O R D E R )
C B O T H O F T ~ E S E V E C T O R S M U S T H E O F L E N G T H N + 2 W H ~ R E
C X( N + I )--X( 1 ), X( N + 2 )--X( 2 )
C Y( N + I )=Y( 1 ), Y( N + 2 )=Y( 2 )
C ')[POINT' I S T H ~ X - C O O R D O F T H E P O I N T T O B E T E S T E D
C ' Y ~ O I N T ' IS T H E Y - C O O R D O F T R E P O I N T T O B E T E S T E D
C
C O U T P U T p A R A M E T E R S :
C
C ' M I R D S T ' T H ~ D I S T A N C E F R O M T H ~ P O I N T T O T H ~ N E A R ~ S T P O I N T
C O N T H ~ P O L Y G O N
C I F ' M I N D S T ' I S L T Z E R O T H E N P O I N T I S O U T S I D E T H E P O L Y G O N
C I F ' M I N D S T ' I S E Q Z E R O T H E N P O I N T I S O N A S I D E O F T H ~ P O L Y G O N
C I F ' M I N D S T ' I S G T Z E R O T H E N P O I N T I S I N S I D E T R ~ P O L Y G O N
c
c t, mTEs :
c
c A N I M P R O V E D V E R S I O N O F T N E A L G O ~ T I m~ o F N O R D S E C X A . D N'mSTm~T
c
c
I N T E G E R N , I , J
D O U B L E P R E C I S I O N X( *),Y( * ) , X P O I N T , Y P O I N T , S M ~ % L L D , L A R G E D , D , A R E A , D E T ,
+ M I N D S T , XI, Y I , X I P , Y I P , X 2 1 , Y 2 1 , T, DX, DY,
+ C 0 0 0 0 0 , C 0 0 0 0 1
C
L O G I C A L S N E A R
C
p ~ ( C00O00--O. 0 D O ,
+ C 0 0 0 0 1 - 1 . O D O )
C
C ' S N E A R ' I S .TR~/E. I F D I S T A N C E T O N E A R E S T S I D E I S L E S S T H A N
C D I S T A N D E T O N E A R E S T
C ' S M E A R ' I S . F A L S E . I F D I S T A N C E T O N E A R E S T V E R T E X I S L E S S T H A H
C D I S T A N C E T O N E A R E S T S I D E
C ' M I N D S T ' I S S Q ~ & R E O F D I S T A N C E T O C L O S E S T P O I N T O N T H E P O L Y G O N
C
M I N D S T - - L A R G ~ D
C
C L O O P O V E R E A C K S I D E D E F I N I N G P O L Y G O N
C
D O i 0 I ' I , N
C
C S T A R T O F S I D E ~ C O O R D S ( X I , Y I )
C E N D OF S I D E ~ C O O R D S (X2,Y2)
C P O I N T HAS C O O R D S ( X I ~ O I N T , Y P O I M T )
C
X I ' X ( I )
Y I - Y ( I )
X / I - X ( I + i ) - X I
Y 2 1 - Y ( I + l ) - Y I
X I P - X I - X P O I N T
y I P - - Y I - T P O I N T
C
C P O I N T S O N I N F I N I T E L I N E D E F I N E D BY
C X - X I + T ' ( X I - X 2 )
C Y ' Y I + T " ( Y I - Y 2 )
C
C T w O A T ( X I , Y I )
C T ~ I A T ( X 2 , Y 2 )
C F I N D W R E R E N O R M A L p A S S I N G T H R O O G ~ ( X P O I N T , Y P O I N T )
C I N T E R S E C T S I N F I N I T E L I N E
C
T ~ - ( X I P "X21+YI]~ * Y 2 1 )/( X 2 1 w X 2 1 + Y 2 1 " Y 2 1 )
IF( T. LT. C 0 0 0 0 0 )T~EN
C
C N O R M A L D O E S N O T I N T E R S E C T S I D E
C P O I N T I S C L O S E S T T O V E R T E X ( X I , Y I )
C C O M P U T E S Q U A R E O F D I S T A N C E T O T E I S V E R T E X
C
D.-XIP * X I P + Y I P w 1 P
IF( D LT M I N D S T ) T ~ E N
C
C L O S E R T O ( X I , Y 1 ) O'n~ZR
C S I D E
C
S N E A R = . F A L S E .
M I N D S T = D
J = I
E N D I F
E L S E IF( T. LE. C 0 0 0 0 1 ) T H E N
C
C N O R M A L I N T E R S E C T S S I D E
C
D X = X l P + T * X 2 1
D Y = Y I P + T - Y 2 l
D = D X * D X + D Y * D Y
I F ( D, LT. M I N D S T ) T H E N
C
C P O I N T I S C L O S E R T O T H I S S I D E T H A N T O A N Y
O T H Z R S I D E O R V E R T E X
C
C
SNEAR--. T R U E .
MINDST--D
J--I
E N D I F
E N D I F
i O C O N T I N U E
M I N D S T . - S Q R T ( M I N D S T )
IF( M I N D S T . LT. ~ ) T ~ N
C
C P O I N T I S O N S I D E O F P O L Y G O N
C
M I N D S T ~ C 0 0 0 O 0
E L S E
IF( S N E A R ) T N E ~
C
C P O I N T I S C L O S E R T O I T S N E A R Z S T S I D E T H A N T O I T S N E A R E S T
C V E R T E X , C H E C K I F P O I N T I S T O L E P T O R R I G H T O F T R I S S I D E
C I F P O I N T I S T O L E F T O F S I D E I T I S I N S I D E P O L Y G O N , E L S E
C P O I N T I S O U T S I D E P O L Y G O N
C
A R E A - D E T ( X( J ), X( J + l ), X ~ O I N T , ( J ), Y ( J + l ), Y P O I ~ T )
M I N D S T ~ S IGN( M I M D S T , A R E A )
E L S E
C
C P O I N T I S C L O S E R T O I T S N E A R E S T V E R T E X T H A N I T S N E A R E S T S I D E ,
C C H E C K I F MF, A R Z S T V E R T E X I S C O N C A V E
C I F T H Z N E A R E S T V E R T E X I S C O N C A V E T ~ E N P O I N T I S I N S I D E T H E
C P O L Y G O N , E L S E T H Z P O I N T I S Oi2rSIDE T H E P O L Y G O N
C
IF( J. EQ. 1 ) T H E N
JmN+l
E N D I F
A R E A - D E T ( X( J + l ), X( J ), X( J - i ), Y( J + l ), Y( J ), Y( J - i ) )
MINDST--S ZGN( ~ U ~ D S T , A R E A )
E N D I F
E N D I F
C
E N D
F D I q C T I O N D E T ( X I , X 2 , X S , Y I o Y 2 , Y 3 )
C
C . = = . = w . * ~ * * ~ . . . * * * * . ~ ~ * * = * = . - * * * * - * - ~ * * ' * ~ * ' ' ' = ' = ' * * * * * * w * ' w ' ' ~ * * ' * * "
C F D H C T X O N D E T
C C O ~ U T E 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 ~ P O I N T S
C W I T H C O O R D S ( X I , 1 ) , (X2,1~2) A N D ( X S , Y S ) U S I N G ~ H ] & N T
C F O R M U L A
C
C I N P U T p A R A M E T E R S :
C
C ' X I , Y I ' C O O R D S O F P O I N T 1
C ' X 2 , Y 2 ' C O O R D S O F P O I N T 2
C ' X S , Y S ' C O O R D S O F P O I N T 3
C
C O U T P U T p A R A M E T E R S I
C
C 'DET' T W I C E T H ~ A R E A O F ~ T R I A N G L E D E F I N E D B Y T H Z T H R E E P O I N T S
C
C N O T E S :
C
C D E T I S P O S I T I V E I F 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 I N
C A N T I ~ S E O R D E R
C DET I S N E G A T I V E I F 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 I N
C C L O C K W I S E O R D E R
C D E T I S Z E R O I F A T L E A S T T W O O F T H E P O I N T S A R E C O I N C I D E N T O R I F
C A L L THREE P O I N T S A R E C O L L I N E A R
C
C
D O U B L E P R E C I S I O N X I , X 2 , X S , Y I , Y 2 , Y S , D E T
C
DET,-( X I - X 3 ) w( Y 2 - Y 3 )-( X 2 - X 3 ) w( Y I - Y 3 )
C

Вам также может понравиться