forked from sitz/UVa-Online-Judge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
10011.cpp
193 lines (174 loc) · 3.81 KB
/
10011.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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
#include <bits/stdc++.h>
using namespace std;
/**
1. test if the line through origin and house passes through the circle and has two
intersection points.
2. test if the segment connecting house and center of tree intersects with at least
one of the two tangent lines
3. test if the house is within the triangle formed by the origin and two tangent points
if all three tests pass, then pick the minimum among the distance from the house to
the tree and the distance from the house from two tangent lines
**/
class Point
{
public:
double x, y;
Point()
{
x = y = 0;
}
Point(double tx, double ty)
{
x = tx;
y = ty;
}
};
Point origin = Point(0, 0);
int fsig(double x)
{
if (fabs(x) < 1e-6)
{
return 0;
}
else if (x > 0)
{
return 1;
}
else
{
return -1;
}
}
int fcmp(double x, double y)
{
return fsig(x - y);
}
double sqr(double x)
{
return x * x;
}
double safe_sqrt(double x)
{
if (fsig(x) == 0)
{
return 0;
}
else
{
return sqrt(x);
}
}
double dis(double x1, double y1, double x2, double y2)
{
return safe_sqrt(sqr(x1 - x2) + sqr(y1 - y2));
}
double cross(double x1, double y1, double x2, double y2)
{
return x1 * y2 - x2 * y1;
}
double dis_ptl(double x, double y, double a, double b, double c)
{
return fabs(a * x + b * y + c) / sqrt(sqr(a) + sqr(b));
}
double line_circle_delta(double a, double b, Point center, double r)
{
double offset_x = center.x, offset_y = center.y;
double x1 = b, y1 = -a, x2 = b + b, y2 = -a - a;
x1 -= offset_x;
y1 -= offset_y;
x2 -= offset_x;
y2 -= offset_y;
double dx = x2 - x1, dy = y2 - y1;
double sqr_dr = sqr(dx) + sqr(dy);
double D = x1 * y2 - x2 * y1;
return sqr(r) * sqr_dr - sqr(D);
}
Point get_tangent(double a, double b, Point center, double r)
{
double offset_x = center.x, offset_y = center.y;
double x1 = b, y1 = -a, x2 = b + b, y2 = -a - a;
x1 -= offset_x;
y1 -= offset_y;
x2 -= offset_x;
y2 -= offset_y;
double dx = x2 - x1, dy = y2 - y1;
double sqr_dr = sqr(dx) + sqr(dy);
double D = x1 * y2 - x2 * y1;
double x = D * dy / sqr_dr;
double y = -D * dx / sqr_dr;
return Point(x + offset_x, y + offset_y);
}
int equal(Point a, Point b)
{
return fcmp(a.x, b.x) == 0 && fcmp(a.y, b.y) == 0;
}
double area_triangle(Point a, Point b, Point c)
{
return 0.5 * fabs(cross(c.x - a.x, c.y - a.y, c.x - b.x, c.y - b.y));
}
int point_in_triangle(Point p, Point a, Point b, Point c)
{
if (equal(a, b) || equal(a, c) || equal(b, c))
{
return 0;
}
return fcmp(area_triangle(a, b, c), area_triangle(p, a, b) + area_triangle(p, b, c) + area_triangle(p, a, c)) == 0;
}
double calc(double cx, double cy, double r, double hx, double hy)
{
Point center = Point(cx, cy);
Point house = Point(hx, hy);
double a1, b1, a2, b2;
if (fcmp(fabs(cy), r) == 0)
{
a1 = 0;
b1 = 1;
a2 = 2 * cx * cy;
b2 = sqr(r) - sqr(cx);
if (b2 > 0)
{
a2 = -a2;
b2 = -b2;
}
}
else
{
a1 = 1;
a2 = -1;
b1 = (-cx * cy - r * safe_sqrt(sqr(cy) + sqr(cx) - sqr(r))) / (sqr(cy) - sqr(r));
b2 = -(-cx * cy + r * safe_sqrt(sqr(cy) + sqr(cx) - sqr(r))) / (sqr(cy) - sqr(r));
}
double vx1 = b1, vy1 = -a1, vx2 = b2, vy2 = -a2;
/* Intersection test */
if (fsig(line_circle_delta(hy, -hx, center, r)) <= 0)
{
return 0;
}
/* cross test */
if (fsig(cross(vx1, vy1, cx, cy) * cross(vx1, vy1, hx, hy)) <= 0 || fsig(cross(vx2, vy2, cx, cy) * cross(vx2, vy2, hx, hy)) <= 0)
{
return 0;
}
Point t1 = get_tangent(a1, b1, center, r);
Point t2 = get_tangent(a2, b2, center, r);
if (point_in_triangle(house, origin, t1, t2))
{
return 0;
}
double ans = dis(cx, cy, hx, hy) - r;
ans = min(ans, dis_ptl(hx, hy, a1, b1, 0));
ans = min(ans, dis_ptl(hx, hy, a2, b2, 0));
return ans;
}
int main()
{
int times;
scanf("%d", ×);
while (times--)
{
double cx, cy, r, hx, hy;
scanf("%lf%lf%lf%lf%lf", &cx, &cy, &r, &hx, &hy);
printf("%.3lf\n", calc(cx, cy, r, hx, hy));
}
return 0;
}