本文共 3361 字,大约阅读时间需要 11 分钟。
繁琐。
处理出来所有的线段,再判断相交。
对于正方形的已知对角顶点求剩余两顶点 (列出4个方程求解)
p[1].x=(p[0].x+p[2].x+p[2].y-p[0].y)/2;p[1].y=(p[0].y+p[2].y+p[0].x-p[2].x)/2;p[3].x=(p[0].x+p[2].x-p[2].y+p[0].y)/2;p[3].y=(p[0].y+p[2].y-p[0].x+p[2].x)/2;
1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 #include 9 #include 10 #include 11 using namespace std; 12 #define N 600 13 #define LL long long 14 #define INF 0xfffffff 15 #define zero(x) (((x)>0?(x):-(x)) >2; 19 map f; 20 vector ed[30]; 21 int g; 22 struct point 23 { 24 double x,y; 25 point(double x=0,double y=0):x(x),y(y) {} 26 } p[600]; 27 typedef point pointt; 28 pointt operator -(point a,point b) 29 { 30 return pointt(a.x-b.x,a.y-b.y); 31 } 32 struct line 33 { 34 pointt u,v; 35 int flag; 36 char c; 37 } li[N]; 38 vector dd[30]; 39 char s1[10],s2[15],s[30]; 40 int dcmp(double x) 41 { 42 if(fabs(x) eps; 87 } 88 89 //判两线段相交,包括端点和部分重合 90 91 int intersect_in(point u1,point u2,point v1,point v2) 92 { 93 if (!dots_inline(u1,u2,v1)||!dots_inline(u1,u2,v2)) 94 return !same_side(u1,u2,v1,v2)&&!same_side(v1,v2,u1,u2); 95 return dot_online_in(u1,v1,v2)||dot_online_in(u2,v1,v2)||dot_online_in(v1,u1,u2)||dot_online_in(v2,u1,u2); 96 } 97 void init(int kk,char c) 98 { 99 int i;100 int k = c-'A';101 if(kk==1)102 {103 for(i = 0; i <= 2 ; i+=2)104 {105 scanf(" (%lf,%lf)",&p[i].x,&p[i].y);106 }107 p[1].x=(p[0].x+p[2].x+p[2].y-p[0].y)/2;108 p[1].y=(p[0].y+p[2].y+p[0].x-p[2].x)/2;109 p[3].x=(p[0].x+p[2].x-p[2].y+p[0].y)/2;110 p[3].y=(p[0].y+p[2].y-p[0].x+p[2].x)/2;111 p[4] = p[0];112 for(i = 0; i < 4 ; i++)113 {114 li[++g].u = p[i];115 li[g].v = p[i+1];116 dd[k].push_back(li[g]);117 }118 }119 else if(kk==2)120 {121 for(i = 1; i <= 3 ; i++)122 {123 scanf(" (%lf,%lf)",&p[i].x,&p[i].y);124 }125 point pp = point((p[1].x+p[3].x),(p[1].y+p[3].y));126 p[4] = point(pp.x-p[2].x,pp.y-p[2].y);127 //printf("%.3f %.3f\n",p[4].x,p[4].y);128 p[5] = p[1];129 for(i = 1; i <= 4; i++)130 {131 li[++g].u = p[i];132 li[g].v = p[i+1];133 li[g].c = c;134 dd[k].push_back(li[g]);135 }136 }137 else if(kk==3)138 {139 for(i = 1; i <= 2; i++)140 scanf(" (%lf,%lf)",&p[i].x,&p[i].y);141 li[++g].u = p[1];142 li[g].v = p[2];143 li[g].c = c;144 dd[k].push_back(li[g]);145 }146 else if(kk==4)147 {148 for(i = 1; i <= 3 ; i++)149 scanf(" (%lf,%lf)",&p[i].x,&p[i].y);150 p[4] = p[1];151 for(i = 1; i <= 3 ; i++)152 {153 li[++g].u = p[i];154 li[g].v = p[i+1];155 li[g].c = c;156 dd[k].push_back(li[g]);157 }158 }159 else if(kk==5)160 {161 int n;162 scanf("%d",&n);163 for(i = 1; i <= n ; i++)164 scanf(" (%lf,%lf)",&p[i].x,&p[i].y);165 p[n+1] = p[1];166 for(i = 1; i <= n ; i++)167 {168 li[++g].u= p[i];169 li[g].v = p[i+1];170 li[g].c = c;171 dd[k].push_back(li[g]);172 }173 }174 }175 176 int main()177 {178 f["square"] = 1;179 f["rectangle"] = 2;180 f["line"] = 3;181 f["triangle"] = 4;182 f["polygon"] = 5;183 int i,j,k;184 while(scanf("%s",s1)!=EOF)185 {186 if(s1[0]=='.') break;187 if(s1[0]=='-') continue;188 for(i =0 ; i < 26 ; i++)189 {190 ed[i].clear();191 dd[i].clear();192 }193 g = 0;194 k=0;195 scanf("%s",s2);196 s[++k] = s1[0];197 init(f[s2],s1[0]);198 while(scanf("%s",s1)!=EOF)199 {200 if(s1[0]=='-') break;201 //cout< <
转载于:https://www.cnblogs.com/shangyu/p/3885905.html