#include<iostream>
using namespace std;
class CPoint3D//定义的类(class),三维空间中的点类
{
public:
float x, y, z;
CPoint3D diff(CPoint3D);//差
CPoint3D add(CPoint3D);
float dot(CPoint3D);
CPoint3D cross(CPoint3D);
};
//矢量和
CPoint3D CPoint3D::add(CPoint3D p1)
{
CPoint3D p;
p.x = x + p1.x;
p.y = y + p1.y;
p.z = z + p1.z;
return p;
}
//叉乘——空间向量两点坐标叉乘公式:(u1,u2,u3)(v1,v2,v3)
//两坐标叉乘后:(u2v3-v2u3,u3v1-v3u1,u1v2-v1u2)
CPoint3D CPoint3D::cross(CPoint3D p1)
{
CPoint3D p;
p.x = y*p1.z-p1.y*z;
p.y = z*p1.x-p1.z*x;
p.z = x*p1.y-p1.x*y;
return p;
}
//点乘——x1x2+y1y2+z1z2
float CPoint3D::dot(CPoint3D p1)
{
float a = x * p1.x + y * p1.y + z * p1.z;
return a;
}
//矢量差
CPoint3D CPoint3D:: diff(CPoint3D p1)
{
CPoint3D p;
p.x = x - p1.x;
p.y = y - p1.y;
p.z = z - p1.z;
return p;
}
int main()
{
CPoint3D p1,p2,p3,p4,p5;
p1.x = 10.2; p1.y = 21.3; p1.z = 1.5;//任意坐标点
p2.x = 6.5; p2.y = 1.6; p2.z = 9.8;
p3=p2.diff(p1);
p4 = p2.add(p1);
p5 = p2.cross(p1);
float a = p2.dot(p1);
cout << "向量差结果为:p3.x="<<p3.x<<" "
<< "p3.y=" << p3.y << " "
<< "p3.z=" << p3.z<< " " << endl;
cout << "向量和结果为:p4.x=" << p4.x << " "
<< "p4.y=" << p4.y << " "
<< "p4.z=" << p4.z << " " << endl;
cout << "叉乘结果为:p5.x=" << p5.x << " "
<< "p5.y=" << p5.y << " "
<< "p5.z=" << p5.z << " " << endl;
cout << "点乘结果" << a << endl;
system("pause");
return 0;
}
//叉乘是一个向量,点乘就变成了一个数
类--两个坐标点的矢量和、矢量差、点乘、叉乘
最新推荐文章于 2024-04-22 14:11:58 发布