## Name

MGH_POLY_INSIDE

## Purpose

Determine whether a point or set of points is inside a polygon.

## Calling Sequence

Result = MGH_POLY_INSIDE(XP, YP, X, Y)

## Inputs

XP,YP: Vectors of X, Y positions defining the polygon.

X,Y: X, Y position(s) defining the point(s) to be tested. Can

be vectors.

## Input Keywords

EDGE: Set this keyword to accept edge (& vertex) points as

"inside". Default is to reject them.

NAN: Set this keyword to specify that all points for which

X or Y is not finite (eg Nan, Inf) are to return 0.

Default is to process non-finite points, which leads

to floating point errors and an undefined result for

that point.

## Outputs

The function returns an array of the same shape as X. Each element

is 0 if the point is outside the polygon, 1 if it is inside the polygon.

## Procedure

This routine calculates the displacement vectors from each point to

all the vertices of the polygon and then takes angles between each pair

of successive vectors. The sum of the angles is zero

for a point outside the polygon, and +/- 2*pi for a point

inside. A point on an edge will have one such angle

equal to +/- pi. Points on a vertex have a zero displacement vector.

## References

Note that the question of how to determine whether a point is

inside or outside a polygon was discussed on comp.lang.idl-pvwave

in October 1999. The following is quoted from a post by Randall Frank

<randall-frank@computer.org>:

I would suggest you read the Graphics FAQ on this issue and also

check Graphics Gem (I think volume 1) for a more detailed explanation

of this problem. The upshot is that there really are three core methods

and many variants. In general, you can sum angles, sum signed areas or

clip a line. There are good code examples of all these approaches on the

net which can be coded into IDL very quickly. It also depends on how

you intend to use the function. If, you are going to repeatedly test many

points, you are better off using one of the sorted variants of the line

clipping techniques. In general, the line clipping techniques are the

fastest on the average, but have poor worst case performance without

the sorting overhead. The angle sum is one of the slowest methods

unless you can get creative and avoid the transcendentals (and you

can). The area sum approach generally falls in between. In IDL code,

I believe you can vectorize the latter with some setup overhead, making

it the fastest for .pro code when testing multiple points with one

point per call.

## Further Resources

* "Misc Notes - WR Franklin", http://www.ecse.rpi.edu/Homepages/wrf/misc.html:

includes a reference (broken @ Jul 2001) to his point-in-polygon code.

* Comp.graphics.algorithms FAQ, http://www.faqs.org/faqs/graphics/algorithms-faq/:

See subject 2.03

## See Also

MGH_PNPOLY, which implements a line-clipping technique.

###########################################################################

This software is provided subject to the following conditions:

1. NIWA makes no representations or warranties regarding the

accuracy of the software, the use to which the software may

be put or the results to be obtained from the use of the

software. Accordingly NIWA accepts no liability for any loss

or damage (whether direct of indirect) incurred by any person

through the use of or reliance on the software.

2. NIWA is to be acknowledged as the original author of the

software where the software is used or presented in any form.

###########################################################################

## Modification History

Mark Hadfield, June 1995:

Written based on ideas in MATLAB routine INSIDE.M in the WHOI

Oceanography Toolbox v1.4 (R. Pawlowicz, 14 Mar 94,

rich@boreas.whoi.edu).

Mark Hadfield, Dec 2000:

Updated.

Mark Hadfield, Jul 2001:

Changed argument order: polygon vertices are now before test

position(s).