ある3次元上の点が3次元平面上にあるかどうかを判定するアルゴリズム。
1 直線上にない 3 点 v1, v2, v3 から構成される平面があるとする。
ある点 p が、その平面上にあるかどうかを調べる。
typedef struct {
double x, y, z;
} Vector;
typedef struct {
Vector v1, v2, v3;
} Plane;
Vector vectorMinus(Vector v1, Vector v2)
{
Vector v;
v.x = v1.x - v2.x;
v.y = v1.y - v2.y;
v.z = v1.z - v2.z;
return v;
}
double dotProduct(Vector v1, Vector v2)
{
return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z;
}
Vector exteriorProduct(Vector v1, Vector v2)
{
Vector v;
v.x = v1.y*v2.z - v1.z*v2.y;
v.y = v1.z*v2.x - v1.x*v2.z;
v.z = v1.x*v2.y - v1.y*v2.x;
return v;
}
int isPointOnPlane(Vector v, Plane pl)
{
double val;
Vector v1, v2, nv;
v1 = vectorMinus(pl.v2, pl.v1);
v2 = vectorMinus(pl.v3, pl.v1);
nv = exteriorProduct(v1, v2);
v1 = vectorMinus(v, pl.v1);
val = dotProduct(v1, nv);
if(val == 0.0)
return 1;
return 0;
}
int main()
{
Plane pl = {{1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {0.0, 0.0, 1.0}};
Vector p1 = {1.0, 1.0, 1.0};
Vector p2 = {1.0, 0.0, 0.0};
Vector p3 = {0.5, 0.0, 0.5};
printf("%d\n", isPointOnPlane(p1, pl));
printf("%d\n", isPointOnPlane(p2, pl));
printf("%d\n", isPointOnPlane(p3, pl));
return 0;
}