Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: testing if a point is inside a polygon

  • To: mathgroup at smc.vnet.net
  • Subject: [mg96369] Re: testing if a point is inside a polygon
  • From: "Sjoerd C. de Vries" <sjoerd.c.devries at gmail.com>
  • Date: Thu, 12 Feb 2009 06:38:41 -0500 (EST)
  • References: <gmp0mt$btn$1@smc.vnet.net>

Since timing is often important for functions like this one, I've
compared the various algorithms posted here using David's two tests.
The results are below:

{
 {"Daniel", 0.828, 1.219},
 {"Daniel/Sjoerd", 0.688, 1.078},
 {"Sedgewick/Sjoerd", 0.688, 0.954},
 {"David1", 41.766, 81.89},
 {"David2", 1.578, 2.531},
 {"Godkin/Frank", 1.485, 2.359}
 }

In the "Daniel/Sjoerd" method I have used Daniel's code and taken out
every intermediate step. This saves an additional 20%. The code is now
completely gibberish, but it still works ;-)

PointInsidePolygonQ[pt_, poly_] :=
 Module[{},
  Equal @@
   Sign@MapThread[
     Dot, {pt - # & /@
       poly, {{0, 1}, {-1, 0}}.# & /@ (Subtract @@ # & /@
         Partition[Append[poly, poly[[1]]], 2, 1])}]
  ]

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



  • Prev by Date: Re: linear regression with errors in both variables
  • Next by Date: Re: Logarithmic Plot
  • Previous by thread: Re: testing if a point is inside a polygon
  • Next by thread: Re: Re: testing if a point is inside a polygon