Black_Red_Grape's blog

By Black_Red_Grape, history, 4 months ago, In English,

I have code of finding the perpendicular distance from a point to a 3D plane. But in this case, the plane is infinite. If the plane is not infinite, but limited to a certain piece, such as the face of an object, then how to find the minimum distance from a point to that face?

An example:

If the 3 points defining a "piece" of plane are:

1 0 0

0 1 0

0 0 0

And the query point is:

1 1 1

Then the answer will be .

Is there any implementation in C++ for this problem? If yes, then could someone please provide it? And if not, then sharing the idea of solving it will also help. Thanks in advance.

 
 
 
 
  • Vote: I like it  
  • +3
  • Vote: I do not like it  

»
4 months ago, # |
  Vote: I like it +15 Vote: I do not like it

Let h be the distance from query point Q to the plane Π that contains the face. Project Q onto Π, then find the distance d from this projection to the face (this becomes a 2D problem). Your answer is then .

And when I say "this becomes a 2D problem" I mean actually make 2D points: if you have two perpendicular unit vectors v and w in Π then you can find the 2D coordinates of some point P by computing (P·v, P·w).

For the code you would need something like this (details to come in my book once I've written that part):

struct p3 {double x,y,z;}
// assume operators +,-,etc. already defined
double operator|(p3 a, p3 b) {/* dot product */} // inspired from notation <a|b>
p3 operator*(p3 a, p3 b) {/* cross product */}
p3 unit(p3 a) {/* make unit vector from a */}

struct plane {
    p3 o, dx, dy, dz; // plane that contains o and extends in directions dx and dy
    // Use any three non-collinear points to create the plane
    plane(p3 a, p3 b, p3 c) : o(a), dx(unit(b-a)), dz(unit(dx*(c-a))), dy(dz*dx) {}
    double dist(p3 a) {return abs((a-o)|dz);}
    pt pos2d(p3 a) {return {(a-o)|dx, (a-o)|dy};} // where pt is a 2D point type
};

Use dist for h and then use pos2d on all the points (including Q) to make the problem 2D and compute d.

  • »
    »
    4 months ago, # ^ |
      Vote: I like it -8 Vote: I do not like it

    Did you stop working on your geometry book?

    • »
      »
      »
      4 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      No, I'm still working on it, it's just taking much more time than I anticipated (especially all the darn figures). I made some progress lately, and I will post an update in a few days. :)