您的位置:首页 > 其它

三维向量的标准化,两点距离,点乘,叉乘

2013-09-18 16:46 288 查看
#include <iostream>
using namespace std;
struct vector
{
float x;
float y;
float z;

};
vector c;
float Get_Length(vector &a)
{
return a.x*a.x+a.y*a.y+a.z*a.z;
}
float Distance(vector &a,vector &b)
{
vector c;
c.x = a.x -b.x;
c.y = a.y -b.y;
c.z = a.z -b.z;
float distance=sqrt(Get_Length(c));
return distance;
}
float Dot_Porduct(vector &a,vector &b)
{
return a.x*b.x+a.y*b.y+a.z*b.z;
}

vector Cross_Product(vector &a,vector &b)
{
vector c;
c.x=a.y*b.z-a.z-b.y;
c.y=a.z*b.x-a.x*b.z;
c.z=a.x*b.y-a.y*b.x;
return c;
}
vector Normal(vector &a)
{
float length=sqrt(Get_Length(a));
vector normal;
normal.x=a.x/length;
normal.y=a.y/length;
normal.z=a.z/length;
return normal;
}
void main()
{
vector a;
vector b;
vector normal;
cout<<"Enter 3 numbers :"<<endl;
cin>>a.x>>a.y>>a.z;
cout<<"Enter 3 numbers :"<<endl;
cin>>b.x>>b.y>>b.z;
cout<<"a=("<<a.x<<","<<a.y<<","<<a.z<<")"<<endl;
cout<<"b=("<<b.x<<","<<b.y<<","<<b.z<<")"<<endl;
normal= Normal(a);
cout<<"The normal of a is ("<<normal.x<<","<<normal.y<<","<<normal.z<<")"<<endl;
float dis= Distance(a,b);
cout<<"The distance a and b is "<<dis<<endl;
float dot = Dot_Porduct(a,b);
cout<<"The dot_product of a and b is "<<dot<<endl;
vector c = Cross_Product(a,b);
cout<<"The corss_product of a and b is ("<<c.x<<","<<c.y<<","<<c.z<<")"<<endl;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: