hdu4998 旋转坐标系
生活随笔
收集整理的這篇文章主要介紹了
hdu4998 旋转坐标系
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題意:
? ? ? 一開始的時候有一個坐標系(正常的),然后有n個操作,每個操作是 x y d,意思是當前坐標系圍繞x,y點逆時針旋轉d度,最后讓你輸出三個數x y d,把這n個操作的最后結果,用一步等效過來,就是找到一個點,逆時針旋轉一個度數,等于當前的這個狀態。
思路:
? ? ? 一開始的時候有一個坐標系(正常的),然后有n個操作,每個操作是 x y d,意思是當前坐標系圍繞x,y點逆時針旋轉d度,最后讓你輸出三個數x y d,把這n個操作的最后結果,用一步等效過來,就是找到一個點,逆時針旋轉一個度數,等于當前的這個狀態。
思路:
? ? ? 我們可以用一個向量來代表當前坐標系,每次操作把當前向量拆成兩個點單獨操作,假如當前向量a,b,繞點c旋轉d度,那么我們可以等效向量c,a逆時針旋轉d,然后向量c,b逆時針旋轉d,這樣就的到了兩個新的向量,此時我們要根據這兩個新的向量求出當前這兩個點的新位置,然后再用當前的新位置和下一組操作,最后得到了最終的一個向量,現在我們只要求出初始向量和最終向量的轉換關系就行了,這個地方首先我們求轉換點,求法是兩個向量的x,x'連線,y.y'連線,兩條線段中垂線的交點,求出交點之后再用余弦定理求出夾角,然后在用向量的關系來判斷要不要用2PI-當前度數,具體看代碼。
#include<math.h> #include<algorithm> #include<stdio.h> #define maxn 60 #define eps 1e-7 #define PP (3.141592653589793238) using namespace std;int dcmp(double x) {if(fabs(x)<eps) return 0;else return x<0?-1:1; } double toRad(double deg) {return deg/180.0*acos(-1.0); } struct Point {double x,y;Point(){}Point(double x,double y):x(x),y(y) {}void input(){scanf("%lf %lf",&x,&y);} }; typedef Point Vector;Vector operator+( Vector A, Vector B ) {return Vector( A.x + B.x, A.y + B.y ); } Vector operator-(Vector A,Vector B) {return Vector( A.x - B.x, A.y - B.y ); } Vector operator*( Vector A, double p ) {return Vector( A.x * p, A.y * p ); } Vector operator/( Vector A, double p ) {return Vector( A.x / p, A.y / p ); } bool operator<(const Point& A, const Point& B ) {return dcmp( A.x - B.x ) < 0 || ( dcmp( A.x - B.x ) == 0 && dcmp( A.y - B.y ) < 0 ); } bool operator==( const Point& a, const Point& b ) {return dcmp( a.x - b.x ) == 0 && dcmp( a.y - b.y ) == 0; } struct Line {Point s,e;Vector v;Line() {}Line(Point s,Point v,int type):s(s),v(v){}Line(Point s,Point e):s(s),e(e){v=e-s;}}; double Dot(Vector A,Vector B) {return A.x*B.x+A.y*B.y; } double Length(Vector A) {return sqrt(Dot(A,A)); } double Angle(Vector A,Vector B) {return acos(Dot(A,B)/Length(A)/Length(B)); } double Cross(Vector A,Vector B) {return A.x*B.y-A.y*B.x; } double Area2(Point A,Point B,Point C ) {return Cross(B-A,C-A); } double Dist(Point A,Point B) {return Length(A-B); } Vector Rotate(Vector A, double rad) {return Vector(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad)); } Vector Normal(Vector A) {double L=Length(A);return Vector(-A.y/L,A.x/L); } Point GetLineIntersection(Line l1,Line l2) {Point P=l1.s;Vector v=l1.v;Point Q=l2.s;Vector w=l2.v;Vector u=P-Q;double t=Cross(w,u)/Cross(v,w);return P+v*t; } double DistanceToLine(Point P,Line L) {Point A,B;A=L.s,B=L.e;Vector v1=B-A,v2=P-A;return fabs(Cross(v1,v2))/Length(v1); } double DistanceToSegment(Point P, Line L) {Point A,B;A=L.s,B=L.e;if(A==B) return Length(P-A);Vector v1=B-A,v2=P-A,v3=P-B;if (dcmp(Dot(v1,v2))<0) return Length(v2);else if (dcmp(Dot(v1,v3))>0) return Length(v3);else return fabs(Cross(v1,v2)) / Length(v1); } Point GetLineProjection(Point P,Line L) {Point A,B;A=L.s,B=L.e;Vector v=B-A;return A+v*(Dot(v,P-A)/Dot(v,v)); }double abss(double x) {return x < 0 ? -x : x; }bool OnSegment(Point p,Line l) {Point a1=l.s;Point a2=l.e;return dcmp(Cross(a1-p,a2-p))==0&&dcmp(Dist(p,a1)+Dist(p,a2)-Dist(a1,a2))==0; } bool Paralled(Line l1,Line l2) {return dcmp(Cross(l1.e-l1.s,l2.e-l2.s))==0; } bool SegmentProperIntersection(Line l1,Line l2) {if(Paralled(l1,l2)){return false;}Point t=GetLineIntersection(l1,l2);if(OnSegment(t,l1)){return true;}return false; }int main () {double x ,y ,p;int T ,n ,i;scanf("%d" ,&T);while(T--){scanf("%d" ,&n);double nowx1 = 0 ,nowy1 = 0;double nowx2 = 0 ,nowy2 = 101.0;double sss = 0;;Vector A ,B;for(i = 1 ;i <= n ;i ++){scanf("%lf %lf %lf" ,&x ,&y ,&p);if(p == 0.0 || abss(p - PP * 2) <= 0.00001) continue;sss += p;A.x = nowx1 - x ,A.y = nowy1 - y;B = Rotate(A ,p);nowx1 = x + B.x ,nowy1 = y + B.y;A.x = nowx2 - x ,A.y = nowy2 - y;B = Rotate(A ,p);nowx2 = x + B.x ,nowy2 = y + B.y;}if(nowx1 == 0.0 && nowy1 == 0.0){double x4 = nowx2 ,y4 = nowy2;double x3 = 0 ,y3 = 0;double x1 = 0 ,y1 = 101.0;double aaa;double tmp = (x4 - x3) * (x1 - x3) + (y4 - y3) * (y1 - y3);tmp = tmp / (pow(x4 - x3 ,2.0) + pow(y4 - y3 ,2.0));aaa = acos(tmp);double q1 = 0 ,q2 = 0;if(nowx2 > 0.0) aaa = PP * 2 - aaa; if(abss(aaa - PP * 2) <= 0.00001)aaa = 0;printf("%lf %lf %lf\n" ,q1 ,q2 ,aaa);}else if(nowx2 == 0.0 && nowy2 == 101.0){double x4 = nowx1 ,y4 = nowy1;double x3 = 0 ,y3 = 101.0;double x1 = 0 ,y1 = 0;double aaa;double tmp = (x4 - x3) * (x1 - x3) + (y4 - y3) * (y1 - y3);tmp = tmp / (pow(x4 - x3 ,2.0) + pow(y4 - y3 ,2.0));aaa = acos(tmp);double q1 = 0 ,q2 = 101.0;if(nowx1 < 0) aaa = PP * 2 - aaa;if(abss(aaa - PP * 2) <= 0.00001)aaa = 0;printf("%lf %lf %lf\n" ,q1 ,q2 ,aaa);}else{ Point AA1;AA1.x = AA1.y = 0;Point BB1;BB1.x = nowx1 ,BB1.y = nowy1; Line now1 = Line((AA1 + BB1)/2 ,Normal(AA1 - BB1),1); Point AA2;AA2.x = 0 ,AA2.y = 101.0;Point BB2;BB2.x = nowx2 ,BB2.y = nowy2;Line now2 = Line((AA2 + BB2)/2 ,Normal(AA2 - BB2),1); Point now = GetLineIntersection(now1 ,now2);double x4 = nowx1 ,y4 = nowy1;double x3 = now.x ,y3 = now.y;double x1 = 0 ,y1 = 0;double aaa;double tmp = (x4 - x3) * (x1 - x3) + (y4 - y3) * (y1 - y3);tmp = tmp / (pow(x4 - x3 ,2.0) + pow(y4 - y3 ,2.0));double x2 ,y2;x1 = 0 ,y1 = 101;x2 = nowx2 - nowx1 ,y2 = nowy2 - nowy1;aaa = acos(tmp);if(x1*y2-x2*y1<0) aaa = PP * 2 - aaa;printf("%lf %lf %lf\n" ,now.x ,now.y ,aaa); }}return 0; }
總結
以上是生活随笔為你收集整理的hdu4998 旋转坐标系的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: hdu5012 水搜索
- 下一篇: hdu5015 矩阵快速幂233(好题)