Matt
Matt

Reputation: 4190

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

Upvotes: 5

Views: 5652

Answers (3)

Florian_Schaf
Florian_Schaf

Reputation: 45

If you are drawing Shapes on a Canvas this is a quick and easy Solution.

    private void Canvas_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.OriginalSource is Polygon)
        {
            //do something
        }
    }

"Polygon" can be any shape from System.Windows.Shapes.

Upvotes: 0

KeithS
KeithS

Reputation: 71573

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.

Upvotes: 2

Guffa
Guffa

Reputation: 700342

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.

Upvotes: 16

Related Questions