Re: testing if a point is inside a polygon
- To: mathgroup at smc.vnet.net
- Subject: [mg96263] Re: testing if a point is inside a polygon
- From: "Sjoerd C. de Vries" <sjoerd.c.devries at gmail.com>
- Date: Tue, 10 Feb 2009 05:53:41 -0500 (EST)
- References: <gmp0mt$btn$1@smc.vnet.net>
Here is a function based on the code in Robert Sedgewick's Algorithms
book:
PointInPoly[{x_, y_}, poly_List] :=
Module[{i, j, c = False, npol = Length[poly]},
For[i = 1; j = npol, i <= npol, j = i++,
If[((((poly[[i, 2]] <= y) && (y <
poly[[j, 2]])) || ((poly[[j, 2]] <= y) && (y <
poly[[i, 2]])))
&& (x < (poly[[j, 1]] -
poly[[i, 1]])*(y - poly[[i, 2]])/(poly[[j, 2]] -
poly[[i, 2]]) + poly[[i, 1]])), c = \[Not] c
];
];
Return [c];
]
Cheers -- Sjoerd
On Feb 9, 12:31 pm, Mitch Murphy <mi... at lemma.ca> wrote:
> is there a way to test whether a point is inside a polygon? ie.
>
> PointInsidePolygonQ[point_,polygon_] -> True or False
>
> i'm trying to do something like ...
>
> ListContourPlot[table,RegionFunction->CountryData["Canada=
","Polygon"]]
>
> to create what would be called a "clipping mask" in photoshop.
>
> cheers,
> Mitch