-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13117.cpp
111 lines (95 loc) · 2.39 KB
/
13117.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
#include "bits/stdc++.h"
#define DEBUG cout <<
using namespace std;
struct point {
double x ,y;
point() {x = y = 0.0;}
point (double _x, double _y) : x(_x), y(_y) {}
};
struct vec {
double x, y;
vec (double _x, double _y) : x(_x), y(_y) {}
};
double hypot (double dx, double dy) {
return sqrt(dx* dx + dy*dy);
}
double dist (point p1, point p2) {
return hypot(p1.x - p2.x, p1.y - p2.y);
}
vec scale (vec v, double s) {
return vec(v.x * s, v.y * s);
}
point translate (point p, vec v) {
return point(p.x + v.x, p.y + v.y);
}
vec toVec(point a, point b) {
return vec(b.x - a.x, b.y - a.y);
}
double dot (vec a, vec b) {
return (a.x * b.x + a.y * b.y);
}
double norm_sq (vec v) {
return v.x * v.x + v.y * v.y;
}
double distToLine (point p, point a, point b, point &c) {
vec ap = toVec(a, p), ab = toVec(a, b);
double u = dot(ap,ab) / norm_sq(ab);
c = translate (a, scale(ab,u));
return dist(p, c);
}
double distToLineSegment (point p, point a, point b, point &c) {
vec ap = toVec(a, p), ab = toVec(a,b);
double u = dot(ap, ab) / norm_sq(ab);
if (u < 0.0) {
c = point(a.x, a.y);
return dist(p, a);
}
if (u > 1.0) {
c = point(b.x, b.y);
return dist(p, b);
}
return distToLine (p, a, b, c);
}
int main(int argc, char const *argv[]) {
double n;
while (true) {
cin >> n;
double xr, yr;
double minRadio = numeric_limits<double>::max();
if (cin.fail()) {
break;
}
double primx, primy;
cin >> xr >> yr;
point fabr (xr,yr);
double xi, yi, xprev, yprev;
for (int i = 0; i < n; ++i) {
if (i == 0) {
cin >> xprev >> yprev;
primx = xprev;
primy = yprev;
} else {
cin >> xi >> yi;
point tempA = point (xi, yi);
point tempB = point(xprev, yprev);
point closestPoint = point();
double rad = distToLineSegment (fabr, tempB, tempA, closestPoint);
minRadio = min(minRadio, rad);
xprev = xi;
yprev = yi;
}
}
point tempA = point (xi, yi);
point tempB = point(primx, primy);
point closestPoint = point();
double rad = distToLineSegment (fabr, tempA, tempB, closestPoint);
minRadio = min(minRadio, rad);
double roundNum = minRadio * 1000;
roundNum += 0.5555;
int trunk = int (roundNum);
double res = double (trunk) / 1000;
cout << res << endl;
printf("%.3f\n",minRadio);
}
return 0;
}