题意

给出若干几何体,问每一个几何体都与哪些几何体有交点?

思路

输入格式比较恶心,考虑使用类似快读的方法读入数据:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//string str <- getline(cin, str)
vector<Point> P;
int len = str.length();
for(int i = 0; i < len; i++){
if(str[i] == '('){
int nx, ny, pos = i, f = 1, val = 0;
//Calc NX
while(pos < len && str[pos] < '0' || str[pos] > '9'){
if(str[pos] == '-') f = -1;
pos++;
}
while(pos < len && str[pos] >= '0' && str[pos] <= '9'){
val = val * 10 + (str[pos] - '0');
pos++;
}
nx = val * f;
//Calc NY
f = 1, val = 0;
while(pos < len && str[pos] < '0' || str[pos] > '9'){
if(str[pos] == '-') f = -1;
pos++;
}
while(pos < len && str[pos] >= '0' && str[pos] <= '9'){
val = val * 10 + (str[pos] - '0');
pos++;
}
ny = val * f;
//Push Point
P.push_back(Point(nx, ny));
}
}

事实上用scanf("("%lf, %lf)", &x, &y)应该是一个更好的选择 =_=

将所有的点解析出来之后,要针对不同的几何图形进行处理。这一步可以简单分为三类:

  1. 正方形,给出的是对角点,可以依据以下代码进行求解,原理见下面附图。
1
2
3
4
5
6
7
if(str.find("square") != string::npos){
double cx = P[0].x, cy = P[0].y, tx = P[1].x, ty = P[1].y;
double nx1 = (cx + tx + cy - ty) / 2, ny1 = (cy + ty - cx + tx) / 2;
double nx2 = (cx + tx - cy + ty) / 2, ny2 = (cy + ty + cx - tx) / 2;
//Point(nx1, ny1), Point(nx2, ny2)
P.push_back(Point(nx1, ny1)); P.push_back(Point(nx2, ny2));
}

image-20220205121615861

  1. 矩形(事实上应该是平行四边形,在测试数据里发现并不保证$AB$垂直于$BC$),只需要$D=A+(C-B)$就可以得到剩余的那个点。
  2. 经过了以上处理后,所有数据均可以当成多边形处理,可以设计一个函数Polygon(vector<Point> P)P为多边形的点集,将其一一连线即可。
  3. 将所有图形的线段处理出来后,只要两两枚举,判断线段之间有无相交。
  4. 输出可以集成到一个函数里,便于判断,只要传入一个vector<char> ID,代表有哪些图形与其相交即可。

代码

开始以为卡cin,输出格式有点变动,写的丑了点(

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// 3449.cpp
#pragma GCC optimize(3,"Ofast","inline")
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#define maxn 100005
using namespace std;
const double eps = 1e-8;
int sgn(double x){
if(fabs(x) < eps) return 0;
return x > 0 ? 1 : -1;
}
struct Point{
double x, y;
Point(){}
Point(double _x, double _y){ x = _x; y = _y; }
Point operator +(const Point &A){
return Point(x + A.x, y + A.y);
}
Point operator -(const Point &A){
return Point(x - A.x, y - A.y);
}
double operator *(const Point &A){
return x * A.x + y * A.y;
}
double operator ^(const Point &A){
return x * A.y - y * A.x;
}
void print() {
printf("[Point] x:%.2lf, y:%.2lf\n", x, y);
}
};
struct Line{
Point s, e;
Line(){}
Line(Point _s, Point _e){ s = _s; e = _e; }
Point crosspoint(Line v){
double a1 = (v.e - v.s) ^ (s - v.s);
double a2 = (v.e - v.s) ^ (e - v.s);
return Point((s.x * a2 - e.x * a1) / (a2 - a1), (s.y * a2 - e.y * a1) / (a2 - a1));
}
int segcrossseg(Line v) {
int d1 = sgn((e - s) ^ (v.s - s));
int d2 = sgn((e - s) ^ (v.e - s));
int d3 = sgn((v.e - v.s) ^ (s - v.s));
int d4 = sgn((v.e - v.s) ^ (e - v.s));
if ((d1 ^ d2) == -2 && (d3 ^ d4) == -2) return 2;
return (d1 == 0 && sgn((v.s - s) * (v.s - e)) <= 0) ||
(d2 == 0 && sgn((v.e - s) * (v.e - e)) <= 0) ||
(d3 == 0 && sgn((s - v.s) * (s - v.e)) <= 0) ||
(d4 == 0 && sgn((e - v.s) * (e - v.e)) <= 0);
}
void print() {
printf("[Line] x1:%.2lf, y1:%.2lf x2:%.2lf y2:%.2lf\n", s.x, s.y, e.x, e.y);
}
};
struct Geometry{
char id;
vector<Line> v;
}G[maxn];
void Print(char r, vector<char> ID){
if(ID.empty()){
cout << r << " has no intersections\n";
//printf("%c has no intersections\n", r);
}
else if((int)(ID.size()) == 1){
cout << r << " intersects with " << ID[0] << '\n';
//printf("%c intersects with %c\n", r, ID[0]);
}
else if((int)(ID.size()) == 2){
cout << r << " intersects with " << ID[0] << " and " << ID[1] << '\n';
//printf("%c intersects with %c and %c\n", r, ID[0], ID[1]);
}
else{
int siz = ID.size();
//printf("%c intersects with ", r);
cout << r << " intersects with ";
for(int i = 0; i < siz - 1; i++){
cout << ID[i] << ", ";
//printf("%c, ", ID[i]);
}
cout << "and " << ID[siz - 1] << '\n';
//printf("and %c\n", ID[siz - 1]);
}
}
void Polygon(vector<Point> &A, vector<Line> &X){
int siz = A.size();
for(int i = 0; i < siz - 1; i++){
X.push_back(Line(A[i], A[i + 1]));
}
X.push_back(Line(A[siz - 1], A[0]));
}
void Solve(string str, Geometry &X){
X.id = str[0];
vector<Point> P;
int len = str.length();
for(int i = 0; i < len; i++){
if(str[i] == '('){
int nx, ny, pos = i, f = 1, val = 0;
//Calc NX
while(pos < len && str[pos] < '0' || str[pos] > '9'){
if(str[pos] == '-') f = -1;
pos++;
}
while(pos < len && str[pos] >= '0' && str[pos] <= '9'){
val = val * 10 + (str[pos] - '0');
pos++;
}
nx = val * f;
//Calc NY
f = 1, val = 0;
while(pos < len && str[pos] < '0' || str[pos] > '9'){
if(str[pos] == '-') f = -1;
pos++;
}
while(pos < len && str[pos] >= '0' && str[pos] <= '9'){
val = val * 10 + (str[pos] - '0');
pos++;
}
ny = val * f;
//Push Point
P.push_back(Point(nx, ny));
}
}
if(str.find("square") != string::npos){
double cx = P[0].x, cy = P[0].y, tx = P[1].x, ty = P[1].y;
double nx1 = (cx + tx + cy - ty) / 2, ny1 = (cy + ty - cx + tx) / 2;
double nx2 = (cx + tx - cy + ty) / 2, ny2 = (cy + ty + cx - tx) / 2;
P.push_back(Point(nx1, ny1)); P.push_back(Point(nx2, ny2)); swap(P[1], P[2]);
Polygon(P, X.v);
}
else if(str.find("rectangle") != string::npos){
Point D = P[0] + (P[2] - P[1]); P.push_back(D);
Polygon(P, X.v);
}
else{
Polygon(P, X.v);
}
}
bool check(vector<Line> v1, vector<Line> v2){
for(int i = 0; i < v1.size(); i++)
for(int j = 0; j < v2.size(); j++){
//v1[i].print();
//v2[j].print();
if(v1[i].segcrossseg(v2[j])){
return true;
}
}
return false;
}
bool cmp(const Geometry &A, const Geometry &B){
return A.id < B.id;
}
int main(void)
{
//std::ios::sync_with_stdio(false);
string str;
while(getline(cin, str) && str != "."){
for(int i = 1; i <= 30; i++) G[i].v.clear();
int cnt = 0;
do{
Solve(str, G[++cnt]);
} while(getline(cin, str) && str != "-");
sort(G + 1, G + cnt + 1, cmp);
for(int i = 1; i <= cnt; i++){
vector<char> cpI;
for(int j = 1; j <= cnt; j++){
if(i == j) continue;
if(check(G[i].v, G[j].v)) cpI.push_back(G[j].id);
}
Print(G[i].id, cpI);
}
cout << '\n';
}
return 0;
}