tzc1200 求两直线的夹角

来源:互联网 发布:个性字体软件下载 编辑:程序博客网 时间:2024/05/23 11:58

题目链接:  tzc1200

方法:计算几何 

代码:

#include <iostream>#include <cmath>using namespace std;struct point {double x,y;};struct line { point s,e; };const double pi=acos(-1);//求两条直线的角度 //返回角度为弧度制,并且以逆时针方向为正方向double angle(line a,line b){double dtheta,theta1,theta2; a.s.x-=a.e.x;a.s.y-=a.e.y;b.s.x-=b.e.x;b.s.y-=b.e.y;theta1 = atan2(a.s.y,a.s.x);theta2 = atan2(b.s.y,b.s.x);dtheta = theta2 - theta1;while (dtheta > pi)dtheta -= pi*2;while (dtheta < -pi)dtheta += pi*2; return(dtheta);}int main(){line a,b;int t;cin>>t;while(t--){cin>>a.s.x>>a.s.y>>a.e.x>>a.e.y;cin>>b.s.x>>b.s.y>>b.e.x>>b.e.y;double ans=angle(a,b)/pi*180;if(ans<0)ans+=180;if(ans>180)ans-=180;if(ans>90)ans=180-ans;printf("%.1lf\n",ans);}return 0;}


原创粉丝点击