UVA 478 Points in Figures: Rectangles, Circles, Triangles

本來用 OOP 的方法去寫的, 不過因為不太熟 c++ 的 OOP (平日多用 java, python, nodejs), 所以還是用不美的方法解決了.

不過之後還是有再複習一下 c++ 的 OOP, 再寫一次應該不成問題.

uva478.cpp
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
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
using namespace std;

#define PI 3.14159265

typedef struct pt {
double x, y;
pt() {}
pt(double a, double b): x(a), y(b) {}
} Point;

struct Shape {
Point lt, rb;
Point center;
double radius;
Point vertices[3];
};

double distance_p(const Point &a, const Point &b) {
return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
}

// ao bo
int cross(const Point &a, const Point &b, const Point &o) {
double v = (a.x-o.x)*(b.y-o.y) - (a.y-o.y)*(b.x-o.x);
if(v == 0) return 0;
if(v > 0) return 1;
if(v < 0) return -1;
}

bool isInsideR(const Shape &s, const Point &a) {
bool xok = s.lt.x < a.x && s.rb.x > a.x;
bool yok = s.lt.y > a.y && s.rb.y < a.y;
return xok && yok;
}

bool isInsideT(const Shape &s, const Point &a) {
int signA = cross(a, s.vertices[0], s.vertices[1]);
int signB = cross(a, s.vertices[1], s.vertices[2]);
int signC = cross(a, s.vertices[2], s.vertices[0]);
return signA == signB && signB == signC;
}

bool isInsideC(const Shape &s, const Point &a) {
return distance_p(s.center, a) < s.radius;
}

vector<Shape> v;
vector<char> types;

void read_shape() {
v.clear();
types.clear();

char type[4];
double a, b, c, d, e, f;

while(scanf("%s\n", type) != EOF && type[0] != '*') {
types.push_back(type[0]);

if(type[0] == 'r') {
Shape r;
scanf("%lf%lf%lf%lf", &r.lt.x, &r.lt.y, &r.rb.x, &r.rb.y);
v.push_back(r);
} else if(type[0] == 'c') {
Shape c;
scanf("%lf%lf%lf", &c.center.x, &c.center.y, &c.radius);
v.push_back(c);
} else if(type[0] == 't') {
Shape t;
scanf("%lf%lf%lf%lf%lf%lf", &t.vertices[0].x, &t.vertices[0].y,
&t.vertices[1].x, &t.vertices[1].y,
&t.vertices[2].x, &t.vertices[2].y);
v.push_back(t);
}
}
}


int main() {
// freopen("input.txt", "r", stdin);
Point cur;
bool ans;
int ptcounter = 1;
bool isinany;

read_shape();
while(scanf("%lf%lf", &cur.x, &cur.y) != EOF && (cur.x != 9999.9 && cur.y != 9999.9)) {

isinany = false;

for(int i = 0; i < v.size(); ++i) {
if(types[i] == 'r') {
ans = isInsideR(v[i], cur);
} else if(types[i] == 'c')
ans = isInsideC(v[i], cur);
else
ans = isInsideT(v[i], cur);

if(ans) {
printf("Point %d is contained in figure %d\n", ptcounter, i + 1);
isinany = true;
}
}
if(!isinany)
printf("Point %d is not contained in any figure\n", ptcounter);
ptcounter++;
}

return 0;
}