求c++代码,已知两空间向量的坐标,求他们夹角

向量A(a,b,c)向量B(d,e,f)求夹角

#include <iostream>

#include <math.h>
using namespace std;

#define PI 3.1415926    //定义π
struct vect
{
 double x;
 double y;
 double z;
};
int main()
{  
 vect a,b;
 double tt,kk,sum;
 cout<<"please input the coordinate a:";
 cin>>a.x>>a.y>>a.z;
 cout<<"please input the coordinate b:";
 cin>>b.x>>b.y>>b.z;
 tt=a.x*a.x+a.y*b.y+a.z*a.z;
 kk=b.x*b.x+b.y*b.y+b.z*b.z;
 kk=sqrt(tt+kk);
 tt=a.x*b.x+a.y*b.y+a.z*b.z;
 sum=tt/kk;
 sum=acos(sum);
 tt=sum/PI;
 cout<<"the corner is: "<<sum<<endl;
 cout<<"the corner is: "<<tt<<"PI“<<endl;
 return 0 ;
}

 

运行结果示例:

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-06-27
double delta = (a*d + b*e + c*f)/sqrt((a*a+b*b+c*c)*(d*d+e*e+f*f));
double theta = acos(delta);

theta就是夹角值追问

#define PI 3.1415926 //定义π 这句是不是可以省略的?

追答

可以省略的,没有用到

相似回答