题目大意:
给出多个多边形及其编号
按编号顺序输出每个多边形与其相交的其他多边形编号
注意一个两个多个的不同输出
将每个多边形处理成多条边 然后去判断与其他多边形的边是否相交
计算正方形另外两点的方法https://blog.csdn.net/qq_33328072/article/details/51655746
根据对角线
x0 + x2 = x1 + x3
y0 + y2 = y1 + y3
根据全等三角形
x1 - x3 = y2 - y1
y1 - y3 = x2 - x0
得到
x1 = (x0 + x2 + y2 - y0) / 2
x3 = (x0 + x2 + y0 - y2) / 2
y1 = (y0 + y2 + x0 - x2) / 2
y3 = (y0 + y2 - x0 + x2) / 2
而且注意这里算出了x1后 不能利用x1的结果来计算x3的结果
也就是不能 x3 = x0 + x2 - x1
原因(猜测)可能是x1计算时存在细微的误差
那么用有误差的x1再计算x3的话 误差就更大了 y1y3同理

#include <cstdio> #include <string.h> #include <cmath> #include <algorithm> #include <map> #include <vector> #include <string> #include <iostream> using namespace std; const double eps=1e-10; double add(double a,double b) { if(abs(a+b)<eps*(abs(a)+abs(b))) return 0; return a+b; } struct P { double x, y; P(){}; P(double _x, double _y):x(_x),y(_y){}; P operator - (P p) { return P(add(x,-p.x),add(y,-p.y)); } P operator + (P p) { return P(add(x,p.x),add(y,p.y)); } P operator * (double d) { return P(x*d,y*d); } double dot (P p) { return add(x*p.x,y*p.y); } double det (P p) { return add(x*p.y,-y*p.x); } }; struct L { P s,e; L(){}; L(P _s,P _e):s(_s),e(_e){}; }; vector <L> vec[30]; vector <int> ans[30]; bool onSeg(P a,P b,P c) { return (a-c).det(b-c)==0 && (a-c).dot(b-c)<=0; } P ins(P a,P b,P c,P d) { return a+(b-a)*((d-c).det(c-a)/(d-c).det(b-a)); } bool insSS(int a,int b) { for(int i=0;i<vec[a].size();i++) { L s1=vec[a][i]; for(int j=0;j<vec[b].size();j++) { L s2=vec[b][j]; bool flag=0; if((s1.s-s1.e).det(s2.s-s2.e)==0) { flag= onSeg(s1.s,s1.e,s2.s) || onSeg(s1.s,s1.e,s2.e) || onSeg(s2.s,s2.e,s1.s) || onSeg(s2.s,s2.e,s1.e); } else { P t=ins(s1.s,s1.e,s2.s,s2.e); //printf("%.2f %.2f ",t.x,t.y); flag= onSeg(s1.s,s1.e,t) && onSeg(s2.s,s2.e,t); } if(flag) return 1; } } return 0; } void solve() { for(int i=0;i<30;i++) { if(vec[i].size()==0) continue; for(int j=i+1;j<30;j++) { if(vec[j].size()==0) continue; if(insSS(i,j)) { ans[i].push_back(j); ans[j].push_back(i); } } } for(int i=0;i<30;i++) { if(vec[i].size()==0) continue; if(ans[i].size()==0) { printf("%c has no intersections ",i+'A'); continue; } printf("%c intersects with %c",i+'A',ans[i][0]+'A'); int n=ans[i].size(); if(n==2) { printf(" and %c",ans[i][1]+'A'); } else { for(int j=1;j<n;j++) { if(j+1==n) printf(", and %c",ans[i][j]+'A'); else printf(", %c",ans[i][j]+'A'); } } printf(" "); } printf(" "); } void init() { for(int i=0;i<30;i++) vec[i].clear(), ans[i].clear(); } int main() { string id,ty; while(cin>>id) { if(id[0]=='-') { solve(); init(); continue; } if(id[0]=='.') break; cin>>ty; if(ty=="square") { P p0,p1,p2,p3; scanf(" (%lf,%lf) (%lf,%lf)",&p0.x,&p0.y,&p2.x,&p2.y); p3.x=(p0.x+p2.x-p2.y+p0.y)/2.0; p1.x=(p0.x+p2.x+p2.y-p0.y)/2.0; p3.y=(p0.y+p2.y-p0.x+p2.x)/2.0; p1.y=(p0.y+p2.y+p0.x-p2.x)/2.0; vec[id[0]-'A'].push_back(L(p0,p1)); vec[id[0]-'A'].push_back(L(p2,p1)); vec[id[0]-'A'].push_back(L(p2,p3)); vec[id[0]-'A'].push_back(L(p0,p3)); } else if(ty=="rectangle") { P p0,p1,p2,p3; scanf(" (%lf,%lf) (%lf,%lf) (%lf,%lf)",&p0.x,&p0.y,&p1.x,&p1.y,&p2.x,&p2.y); p3.x=p0.x-p1.x+p2.x; p3.y=p0.y+p2.y-p1.y; vec[id[0]-'A'].push_back(L(p0,p1)); vec[id[0]-'A'].push_back(L(p2,p1)); vec[id[0]-'A'].push_back(L(p2,p3)); vec[id[0]-'A'].push_back(L(p0,p3)); } else if(ty=="line") { P s,e; scanf(" (%lf,%lf) (%lf,%lf)",&s.x,&s.y,&e.x,&e.y); vec[id[0]-'A'].push_back(L(s,e)); } else if(ty=="triangle") { P p0,p1,p2; scanf(" (%lf,%lf) (%lf,%lf) (%lf,%lf)",&p0.x,&p0.y,&p1.x,&p1.y,&p2.x,&p2.y); vec[id[0]-'A'].push_back(L(p0,p1)); vec[id[0]-'A'].push_back(L(p2,p1)); vec[id[0]-'A'].push_back(L(p2,p0)); } else if(ty=="polygon") { int n; scanf("%d",&n); P a,b; scanf(" (%lf,%lf)",&a.x,&a.y); P c=a; for(int i=1;i<n;i++) { b=c; scanf(" (%lf,%lf)",&c.x,&c.y); vec[id[0]-'A'].push_back(L(b,c)); } vec[id[0]-'A'].push_back(L(c,a)); } } return 0; }