点が平面上にあるかどうか

アルゴリズム

ある3次元上の点が3次元平面上にあるかどうかを判定するアルゴリズム。

1 直線上にない 3 点 v1, v2, v3 から構成される平面があるとする。
ある点 p が、その平面上にあるかどうかを調べる。

  1. 平面に垂直なベクトルを求める。
    (ベクトル v2 - v1, v3 - v1 による外積 nv (ベクトル)を求める。)
  2. nv と p - v1 との内積を求める。
  3. 内積の値が 0 なら点 p は平面上にある。
[平面と点 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;
}


home | index
Mail