4 views

Uploaded by chuyenvien94

A Point in Polygon Program

- Additional Mathematics
- Tiling
- final modified assessment
- Mittagseminar ETH Slides
- mathtechnewsletter
- gpoly
- 1. Extrude
- Computer Graphics 3D Graphic Coordinate System
- Constructing Tensegrity Structures From One-bar Elementary Cells by Li, Feng, Cao and Gao
- 10.1080@2151237X.2008.10129264
- 22
- paper hyu
- UVW Shortcuts 3ds Max
- PT Math
- Complete AutoCAD Commands 2
- URBAN RESSURECTION 2025: INTERVENTION AT THE HOWRAH TERMINUS PRECINCT: THE HOWRAH INTERMODAL INTERCHANGE:CHAPTER 8 - CHAPTER 12
- Matriks Dan Determinan
- Triangles I
- Lesson 3 - Shapes
- 2014_maths

You are on page 1of 4

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

A point-in-polygon program

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

CITATIONS READS

7 514

1 author:

University of Newcastle

332 PUBLICATIONS 6,927 CITATIONS

SEE PROFILE

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

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

A point-in-polygon program

S. W. S L O A N

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)

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.

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

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

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

- Additional MathematicsUploaded byJoanna Jackson
- TilingUploaded byLatoria Boyer
- final modified assessmentUploaded byapi-384616765
- Mittagseminar ETH SlidesUploaded byVissarion Phi
- mathtechnewsletterUploaded byapi-379875087
- gpolyUploaded byjamesyu
- 1. ExtrudeUploaded byIwan Kurniawan
- Computer Graphics 3D Graphic Coordinate SystemUploaded byjkkp
- Constructing Tensegrity Structures From One-bar Elementary Cells by Li, Feng, Cao and GaoUploaded byTensegrity Wiki
- 10.1080@2151237X.2008.10129264Uploaded byCalberto MorSa
- 22Uploaded byatpp651
- paper hyuUploaded bynewplato
- UVW Shortcuts 3ds MaxUploaded by1buzzingbat
- PT MathUploaded byMiko Co
- Complete AutoCAD Commands 2Uploaded byVictoria Tozlovanu
- URBAN RESSURECTION 2025: INTERVENTION AT THE HOWRAH TERMINUS PRECINCT: THE HOWRAH INTERMODAL INTERCHANGE:CHAPTER 8 - CHAPTER 12Uploaded bykingshukdatta
- Matriks Dan DeterminanUploaded byGvgfhj
- Triangles IUploaded byapi-3696277
- Lesson 3 - ShapesUploaded byPrecious Streviles
- 2014_mathsUploaded byjgkrishnan
- Review 1Uploaded byRuan Santiago
- Chapter 11 TrigonometryUploaded byCik Yana
- RBSE X Mathematics Solutions 2014Uploaded byNilesh Gupta
- the-place-of-geometry.pdfUploaded byJaume Santos DiazdelRio
- lesson plans 4 10Uploaded byapi-321400762
- 0167 Cat a Maths Io Paper AUploaded bysagar
- Math Paper FinalUploaded bySudhir Kumar
- Curso elemetal de Geometría de PETRUNINUploaded byCarlos Rodriguez
- rewritingratios5packUploaded byapi-261894355
- jacks origami projectUploaded byapi-255439671

- dodoxacsuat-2011Uploaded byVothe Dung
- v14n1-03Uploaded bychuyenvien94
- Convection SchemeUploaded byAlex Daniel
- Using Tenses in Scientific Writing Update 051112Uploaded byasuhass
- Lythuyettruong2012bannhapUploaded bychuyenvien94
- HuongdanLyXUploaded byKhoa Võ
- PHIEU_KHAO_SAT_3_20_10_2011 (1)Uploaded bychuyenvien94
- 2008-2009Uploaded bychuyenvien94
- Dethi09Uploaded bychuyenvien94
- AV4(FILEminimizer)Uploaded bychuyenvien94
- AMR2D PaperUploaded bychuyenvien94
- FrontUploaded bychuyenvien94
- Dethi 2012Uploaded bychuyenvien94
- Dethi2011Uploaded bychuyenvien94
- Dethi2010Uploaded bychuyenvien94
- 2003_giaitichUploaded bychuyenvien94
- 2000_daisoUploaded bychuyenvien94
- WEBcaratheodoryUploaded bymathematician7
- 2004_daisoUploaded bychuyenvien94
- 2004_giaitichUploaded bychuyenvien94
- Correction Homework 9Uploaded bychuyenvien94
- 2003_daisoUploaded bychuyenvien94
- Kiem Tra Do Do Tich PhanUploaded bychuyenvien94
- Dethi 2012Uploaded bychuyenvien94
- 2001_daisoUploaded bychuyenvien94
- 2000_giaitichUploaded bychuyenvien94
- 2002_daisoUploaded bychuyenvien94
- 2005_daisoUploaded bychuyenvien94
- 2005_giaitichUploaded bychuyenvien94

- Tim Hieu Ve Tuong Lua FIREWALLUploaded bywuyingwei
- PIPE EXAMUploaded byVon A. Damirez
- 22308110Uploaded bySuliman M. Ali
- BUC-lpodUploaded byJuan Eric Aabye
- PC1616 PC1832 PC1864 V4 2 Installation Manual EnUploaded byScarlat Madalin
- Lenth Method 1989Uploaded byJan Libbrecht
- Lesson Plan Em 2Uploaded byanbuelectrical
- Ipc Camera Ready XpressUploaded byBehrouz Azimian
- Emily PortfolioUploaded byemily russell
- Sol Mat HaeusslerUploaded bym1228m1228
- broniwUploaded byHan Qing
- Understanding and Managing the Risks of Analytics (166248369)Uploaded byEDUCAUSE
- Harry Potter and the Elemental's Power - Raul FictitiousUploaded byEvan
- w2c1Uploaded byMasAmirah
- qb-dbmsUploaded bysuru_bh25
- Muslim Leaders in Pre-Partition Sub-continent (Opinion by Naeem Baig)Uploaded byNaeem Baig
- OilUploaded bymdklya
- hwch9Uploaded byAnonymous hht06lypm1
- PX M5S DatasheetUploaded bypdmnbrao
- Use Cases and Usage ScenariosUploaded byHarsh Mohan
- Industrial Management [HU 802] EI Organizer 2012Uploaded byJoydeep Munshi
- 00364 Tef Cochlear Report 8-10-13 Next (2).PDFUploaded byBlack Tear Misery
- BookletUploaded bypipiculo
- LEP4104!01!15Temperature Dependence of Different Resistors and DiodesUploaded byBudaGk TaNah KuTaii
- Credit Point SystemUploaded byMurali Krishnan
- Environmental ScanningUploaded byIncubus
- Equity Research Report - CIMB 27 Sept 2016Uploaded byEndi Singarimbun
- Lactose BrothUploaded byTopheng D. Samarita
- Transformer differentialUploaded byerbvp22
- ENGLISH Answer KeyUploaded byKamal Suthar