设三点坐标为A(x1,y1,z1),B(x2,y2,z2),C(x3,y3,z3)
向量AB=(x2-x1,y2-y1,z2-z1),AC=(x3-x1,y3-y1,z3-z1)
AB、AC所在平面的法向量即AB×AC=(a,b,c),其中:
a=(y2-y1)(z3-z1)-(z2-z1)(y3-y1)
b=(z2-z1)(x3-x1)-(z3-z1)(x2-x1)
c=(x2-x1)(y3-y1)-(x3-x1)(y2-y1)
设 a=(ax,ay,az), b=(bx,by,bz)。i,j,k分别是X,Y,Z轴方向的单位向量,则:
a×b=(aybz-azby)i+(azbx-axbz)j+(axby-aybx)k
a·b=(axbx+ayby+az*bz)
Python代码
def normal_vector(p1, p2, p3):
x1, y1, z1 = p1
x2, y2, z2 = p2
x3, y3, z3 = p3
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)
return (a, b, c)
if __name__ == '__main__':
p1 = 1.0, 5.2, 0.0
p2 = 2.8, 3.9, 1.0
p3 = 7.6, 8.4, 2.0
p4 = normal_vector(p1, p2, p3)
print(p4)
# output (-5.800000000000001, 3.0, 14.340000000000002)
C++代码
#include
using namespace std;
//三维double矢量
struct Vec3d
{
double x, y, z;
Vec3d()
{
x = 0.0;
y = 0.0;
z = 0.0;
}
Vec3d(double dx, double dy, double dz)
{
x = dx;
y = dy;
z = dz;
}
void Set(double dx, double dy, double dz)
{
x = dx;
y = dy;
z = dz;
}
};
//计算三点成面的法向量
void Cal_Normal_3D(const Vec3d& v1, const Vec3d& v2, const Vec3d& v3, Vec3d &vn)
{
//v1(n1,n2,n3);
//平面方程: na * (x – n1) + nb * (y – n2) + nc * (z – n3) = 0 ;
double na = (v2.y - v1.y)*(v3.z - v1.z) - (v2.z - v1.z)*(v3.y - v1.y);
double nb = (v2.z - v1.z)*(v3.x - v1.x) - (v2.x - v1.x)*(v3.z - v1.z);
double nc = (v2.x - v1.x)*(v3.y - v1.y) - (v2.y - v1.y)*(v3.x - v1.x);
//平面法向量
vn.Set(na, nb, nc);
}
int main()
{
// Vec3d v1(1.0, 5.2, 3.7);
// Vec3d v2(2.8, 3.9, 4.5);
// Vec3d v3(7.6, 8.4, 6.2);
// 法向量为:-5.81 0.78 14.34;
Vec3d v1(1.0, 5.2, 0.0);
Vec3d v2(2.8, 3.9, 1.0);
Vec3d v3(7.6, 8.4, 2.0);
// 法向量为:-5.8 3 14.34
Vec3d vn;
Cal_Normal_3D(v1, v2, v3, vn);
// cout <<"法向量为:"<< vn.x << '\t' << vn.y << '\t' << vn.z << '\n';
cout <<"法向量为:"<< vn.x << " " << vn.y << " " << vn.z << '\n';
return 0;
}