Re: Points In/Out in 3D shapes

• To: mathgroup at smc.vnet.net
• Subject: [mg101198] Re: Points In/Out in 3D shapes
• From: "Sjoerd C. de Vries" <sjoerd.c.devries at gmail.com>
• Date: Fri, 26 Jun 2009 06:52:54 -0400 (EDT)
• References: <h1vm8i\$afl\$1@smc.vnet.net>

```A brute force approach:

1. Find an arbitrary point outside the volume (for instance by finding
the Max of all coordinates and adding 1).

2. Count the number of intersections of a line connecting this point
and the test point with the polygons making up the volume.

3. If the number of intersections is odd, the test point is in the
volume, otherwise it's not.

Cheers -- Sjoerd

On Jun 25, 1:15 pm, M <xrayspect... at googlemail.com> wrote:
> Hallo All Mathematica gurus,
> I have the following code which helps to find if a point is inside or
> outside the 2D polygon.
>
> pnPoly[{testx_, testy_}, pts_List] :=
>  Xor @@ ((Xor[#[[1, 2]] > testy, #[[2, 2]] >
>          testy] && ((testx - #[[2,
>             1]]) < (#[[1, 1]] - #[[2,
>              1]]) (testy - #[[2, 2]])/(#[[1, 2]] - #[[2, 2]]))) & /@
>     Partition[pts, 2, 1, {2, 2}])
>
> pol = {{-1, -1}, {0, 0}, {1, -1}, {0, 1}, {1, 0}};
> Graphics[{PointSize[Large], {FaceForm[LightGray], EdgeForm[Black],
>    Polygon[pol]},
>   If[pnPoly[#, pol], {Blue, Point[#]}, {Red, Point[#]}] & /@
>    RandomReal[{-1, 1}, {400, 2}]}]
> Clear[pol]
>
> Ok. this works really good!.
> How can I test if a random point is inside or outside in a given
> volume of any 3D shaped object.
> I need to count the points that are inside the volume. Any
> suggestons ?
>