Any Way You Slice It (向量旋转 以及 判断线段是否相交)(模板)
http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=11353
1 #include<iostream> 2 #include<stdio.h> 3 #include<string> 4 #include<math.h> 5 #define PI acos(-1) 6 //using namespace std; 7 struct Nod 8 { 9 int dir; 10 int len; 11 }node[200]; 12 struct Point 13 { 14 double x; 15 double y; 16 }; 17 struct segmemt 18 { 19 Point s; 20 Point t; 21 }; 22 double MAX(double a,double b) 23 { 24 if(a>b)return a; 25 return b; 26 } 27 double MIN(double a,double b) 28 { 29 if(a<b)return a; 30 return b; 31 } 32 double mulpti(Point ps , Point pe , Point p) 33 { 34 return (pe.x-ps.x)*(p.y-ps.y)-(p.x-ps.x)*(pe.y-ps.y); 35 } 36 bool inser(Point p1, Point p2 , Point p3, Point p4) //判断线段是否相交 37 { 38 if(MAX(p1.x,p2.x)>=MIN(p3.x,p4.x) && 39 MAX(p3.x,p4.x)>=MIN(p1.x,p2.x) && 40 MAX(p1.y,p2.y)>=MIN(p3.y,p4.y) && 41 MAX(p3.y,p4.y)>=MIN(p1.y,p2.y) && 42 mulpti(p1,p2,p3)*mulpti(p1,p2,p4)<=0 && 43 mulpti(p3,p4,p1)*mulpti(p3,p4,p2)<=0) 44 return true; //相交 45 else 46 return false; //不相交 47 } 48 49 //得到向量的x坐标 50 double getX(double x,double y,int dir) //传入(x,y)向量以及角度dir 51 { 52 return x*cos(dir/180.0*PI)-y*sin(dir/180.0*PI); 53 } 54 //得到向量的y坐标 55 double getY(double x,double y,int dir) 56 { 57 return y*cos(dir/180.0*PI)+x*sin(dir/180.0*PI); 58 } 59 60 int main() 61 { 62 int n; 63 segmemt list[200]; 64 while(~scanf("%d",&n)&&n) 65 { 66 int i; 67 double x,y,tx,ty; 68 for(i=0;i<n;i++) 69 { 70 scanf("%d%d",&node[i].dir,&node[i].len); 71 if(node[i].dir<0) node[i].dir=360+node[i].dir; 72 } 73 list[0].s.x=0; 74 list[0].s.y=0; 75 x = getX(0,1,node[0].dir); 76 y = getY(0,1,node[0].dir); 77 list[0].t.x = x*node[0].len; 78 list[0].t.y = y*node[0].len; 79 for(i=1;i<n;i++) 80 { 81 list[i].s.x = list[i-1].t.x; 82 list[i].s.y = list[i-1].t.y; 83 tx = x; 84 ty = y; 85 x = getX(tx,ty,node[i].dir); 86 y = getY(tx,ty,node[i].dir); 87 list[i].t.x = x*node[i].len + list[i].s.x; 88 list[i].t.y = y*node[i].len + list[i].s.y; 89 } 90 int j,flag=0; 91 for(i=2;i<n;i++) 92 { 93 for(j=0;j<=i-2;j++) 94 { 95 if(inser(list[j].s,list[j].t,list[i].s,list[i].t)) 96 { 97 flag = 1; 98 printf("%d\n",i+1); 99 break; 100 } 101 } 102 if(flag) break; 103 } 104 if(!flag) 105 { 106 puts("SAFE"); 107 } 108 } 109 return 0; 110 }