Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine if (x,y,z) point is inside a shape defined by an array of points

If I have an array of points (x,y,z) and am given a single point (x,y,z), what code do I use to determine if that point resides within the shape defined by the array?

I am drawing a blank on this one...

I'm using C#

EDIT

Thanks for the responses guys, from the comments I have found this link (http://alienryderflex.com/polygon/) which explains the process quite well.

Thanks!

FYI:

bool pointInPolygon() {
    
      int      i, j=polySides-1 ;
      boolean  oddNodes=NO      ;
    
      for (i=0; i<polySides; i++) {
        if (polyY[i]<y && polyY[j]>=y
        ||  polyY[j]<y && polyY[i]>=y) {
          if (polyX[i]+(y-polyY[i])/(polyY[j]-polyY[i])*(polyX[j]-polyX[i])<x) {
            oddNodes=!oddNodes; }}
        j=i; }
     
      return oddNodes; }

It'll need some work, but thats the guts of it.

Thanks again

like image 974
Matt Avatar asked Feb 10 '11 21:02

Matt


2 Answers

Use a point that you know is outside the shape, and check if the line from that point to the given point passes through the surfaces of the shape. If it passes through an odd number of surfaces, the given point is inside the shape.

like image 161
Guffa Avatar answered Oct 07 '22 23:10

Guffa


Further to Guffa's answer, it's harder than it sounds to determine if a line intersects a surface. Here's the math behind that: Intersection of lines and planes. You have to take that basic algorithm (which involves finding the normal of each surface to that point, then determining the angle between the normal and the line to form a right triangle that you find the third point of; WPF's Media3D library has functions on Points and Vectors that make all this easier), then determine if the point you found intersects the plane of the surface within the bounds of that surface. To do THAT, you can take any 2D projection of that surface that has an area > 0, and perform the "point in polygon" test, which is the 2D version of the "point in polyhedron" test you're trying to do.

Good luck.

like image 39
KeithS Avatar answered Oct 07 '22 23:10

KeithS