Skip to content

Instantly share code, notes, and snippets.

@vo
Created April 28, 2012 17:15
Show Gist options
  • Save vo/2520342 to your computer and use it in GitHub Desktop.
Save vo/2520342 to your computer and use it in GitHub Desktop.
Point Plane Distance
// given 3 points on plane (x1,y1,z1), (x2,y2,z2), (x3,y3,z3)
// get distance to (x,y,z)
double plane_exp_point_dist_3d ( double x1, double y1, double z1, double x2,
double y2, double z2, double x3, double y3, double z3, double x, double y, double z )
{
double a;
double b;
double c;
double d;
double dist;
plane_exp2imp_3d ( x1, y1, z1, x2, y2, z2, x3, y3, z3,
&a, &b, &c, &d );
dist = plane_imp_point_dist_3d ( a, b, c, d, x, y, z );
return dist;
}
// convert explicit (3 points on plane) representation
// to implicit representation (A,B,C,D)
void plane_exp2imp_3d ( double x1, double y1, double z1, double x2, double y2,
double z2, double x3, double y3, double z3, double *a, double *b, double *c,
double *d )
{
*a = ( y2 - y1 ) * ( z3 - z1 ) - ( z2 - z1 ) * ( y3 - y1 );
*b = ( z2 - z1 ) * ( x3 - x1 ) - ( x2 - x1 ) * ( z3 - z1 );
*c = ( x2 - x1 ) * ( y3 - y1 ) - ( y2 - y1 ) * ( x3 - x1 );
*d = - x2 * *a - y2 * *b - z2 * *c;
return;
}
// determine distance from plane in implicit representation (A,B,C,D)
// to a point (x,y,z)
double plane_imp_point_dist_3d ( double a, double b, double c, double d,
double x, double y, double z )
{
double dist;
dist =
fabs ( a * x + b * y + c * z + d ) /
sqrt ( a * a + b * b + c * c );
return dist;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment