有n个图形问每个图形与哪些图形相交
正方形只给出对角两个点,已经对角点(a, b) (c,d),则另外两个对角点坐标为((a+b+c-d)/2, (-a+b+c+d)/2)和((a-b+c+d)/2, (a+b-c+d)/2), 由于负号的位置不同,所以直接把负号的位置背下来就行:4,1,2,3
矩形给出三个点,第四个点很好求,利用对角点坐标相加除以2等于中心坐标就行了
然后就是漫长的写作文了
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const double eps = 1e-8;
const int mx = 35;
int tot;
struct Point {
double x, y;
}a, b, c, po[mx];
struct Line{
double x1, y1, x2, y2;
Line() {}
Line (double _x1, double _y1, double _x2, double _y2) {
x1 = _x1; y1 = _y1; x2 = _x2; y2 = _y2;
}
}tmp;
struct Shape {
char name;
int cnt, inst[mx], num;
Line line[mx];
}shape[mx];
bool cmp(Shape a, Shape b) {
return a.name < b.name;
}
void calc(double a, double b, double c, double d, double & x1, double & y1, double & x2, double &y2) {
x1 = (a+b+c-d) / 2;
y1 = (-a+b+c+d) / 2;
x2 = (a-b+c+d) / 2;
y2 = (a+b-c+d) / 2;
}
double cross(double x1, double y1, double x2, double y2) {
return x1*y2 - x2*y1;
}
bool judge(Shape a, Shape(b)) {
for (int i = 1; i <= a.cnt; i++) {
for (int j = 1; j <= b.cnt; j++) {
if (cross(a.line[i].x1-b.line[j].x1, a.line[i].y1-b.line[j].y1, b.line[j].x2-b.line[j].x1, b.line[j].y2-b.line[j].y1)
* cross(a.line[i].x2-b.line[j].x1, a.line[i].y2-b.line[j].y1, b.line[j].x2-b.line[j].x1, b.line[j].y2-b.line[j].y1) < eps
&& cross(b.line[j].x1-a.line[i].x2,b.line[j].y1-a.line[i].y2, a.line[i].x1-a.line[i].x2, a.line[i].y1-a.line[i].y2)
* cross(b.line[j].x2-a.line[i].x2,b.line[j].y2-a.line[i].y2, a.line[i].x1-a.line[i].x2, a.line[i].y1-a.line[i].y2) < eps)
return true;
}
}
return false;
}
void solve() {
for (int i = 0; i < tot; i++) {
for (int j = 0; j < tot; j++) {
if (i == j) continue;
if (judge(shape[i], shape[j])) {
shape[i].inst[shape[i].num++] = j;
}
}
}
for (int i = 0; i < tot; i++) {
if (shape[i].num == 0) printf("%c has no intersections\n", shape[i].name);
else if (shape[i].num == 2) printf("%c intersects with %c and %c\n",shape[i].name, shape[shape[i].inst[0]].name, shape[shape[i].inst[1]].name);
else {
printf("%c intersects with",shape[i].name);
for (int j = 0; j < shape[i].num; j++) {
if (j == 0) printf(" %c", shape[shape[i].inst[j]].name);
else if (j == shape[i].num-1) printf(", and %c",shape[shape[i].inst[j]].name);
else printf(", %c", shape[shape[i].inst[j]].name);
}
puts("");
}
}
puts("");
tot = 0;
}
int main() {
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
char type[mx], name[mx];
tot = 0;
while (scanf("%s", name) != EOF) {
if (name[0] == '.') break;
if (name[0] == '-') {
solve();
continue;
}
//线, 三角, 正方形, 矩形, 多边形
scanf("%s",type);
if (type[0] == 'l') {
shape[tot].name = name[0];
shape[tot].cnt = 1;
shape[tot].num = 0;
scanf(" (%lf,%lf) (%lf,%lf)", &a.x, &a.y, &b.x, &b.y);
shape[tot].line[1] = Line(a.x,a.y,b.x,b.y);
tot++;
}
if (type[0] == 't') {
shape[tot].name = name[0];
shape[tot].cnt = 3;
shape[tot].num = 0;
scanf(" (%lf,%lf) (%lf,%lf) (%lf,%lf)", &a.x, &a.y, &b.x, &b.y, &c.x, &c.y);
shape[tot].line[1] = Line(a.x,a.y,b.x,b.y);
shape[tot].line[2] = Line(c.x,c.y,b.x,b.y);
shape[tot].line[3] = Line(a.x,a.y,c.x,c.y);
tot++;
}
if (type[0] == 's') {
shape[tot].name = name[0];
shape[tot].cnt = 4;
shape[tot].num = 0;
scanf(" (%lf,%lf) (%lf,%lf)", &a.x, &a.y, &b.x, &b.y);
double x1, y1, x2, y2;
calc(a.x, a.y, b.x, b.y, x1, y1, x2, y2);
shape[tot].line[1] = Line(a.x,a.y,x1,y1);
shape[tot].line[2] = Line(x1,y1,b.x,b.y);
shape[tot].line[3] = Line(b.x,b.y,x2,y2);
shape[tot].line[4] = Line(x2,y2,a.x,a.y);
tot++;
}
if (type[0] == 'r') {
shape[tot].name = name[0];
shape[tot].cnt = 4;
shape[tot].num = 0;
double x1, y1, x2, y2;
scanf(" (%lf,%lf) (%lf,%lf) (%lf,%lf)", &a.x, &a.y, &x1, &y1, &b.x, &b.y);
x2 = a.x + b.x - x1;
y2 = a.y + b.y - y1;
shape[tot].line[1] = Line(a.x,a.y,x1,y1);
shape[tot].line[2] = Line(x1,y1,b.x,b.y);
shape[tot].line[3] = Line(b.x,b.y,x2,y2);
shape[tot].line[4] = Line(x2,y2,a.x,a.y);
tot++;
}
if (type[0] == 'p') {
shape[tot].name = name[0];
scanf("%d",&shape[tot].cnt);
shape[tot].num = 0;
for (int i = 1; i <= shape[tot].cnt; i++) {
scanf(" (%lf,%lf)", &po[i].x, &po[i].y);
if (i >= 2) {
shape[tot].line[i] = Line(po[i-1].x,po[i-1].y,po[i].x,po[i].y);
}
shape[tot].line[1] = Line(po[1].x,po[1].y,po[shape[tot].cnt].x,po[shape[tot].cnt].y);
}
tot++;
}
sort(shape, shape+tot, cmp);
}
return 0;
}