-
Notifications
You must be signed in to change notification settings - Fork 0
/
A1034162_UVA10263_20170426.cpp
59 lines (59 loc) · 1.17 KB
/
A1034162_UVA10263_20170426.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
#include<iostream>
#include<cmath>
#include<cstdio>
using namespace std;
struct pts
{
double x,y;
};
struct line
{
pts a,b;
};
double cross(pts a,pts b)
{
return a.x * b.y - a.y *b.x;
}
double length(pts a,pts b)
{
return sqrt(pow(a.x-b.x,2) + pow(a.y - b.y,2));
}
int main()
{
double ll,gl;
pts p,pl,pg,a,b;
line l1;
int i,n;
while(scanf("%lf %lf",&p.x,&p.y)!=EOF)
{
scanf("%d",&n);
scanf("%lf%lf",&a.x,&a.y);
for(i = 0;i < n;i++)
{
scanf("%lf%lf",&b.x,&b.y);
pts u,v,il;
u.x = b.x - a.x,u.y = b.y - a.y;
v.x = p.x -a.x,v.y = p.y - a.y;
ll = fabs(cross(u,v))/length(a,b);
il = u;
if(il.x*v.x + il.y*v.y < 0) il.x *= -1,il.y *= -1;
il.x /= length(a,b),il.y /= length(a,b);
double l1 = length(a,p),l2;
l2 = sqrt(pow(l1,2) - pow(ll,2));
pl.x = a.x + l2 * il.x,pl.y = a.y + l2 * il.y;
if((a.x - pl.x)*(b.x - pl.x) + (a.y - pl.y)*(b.y - pl.y) > 0)
{
if(length(a,p) < length(b,p)) pl = a,ll = length(a,p);
else pl = b,ll = length(b,p);
}
a = b;
if(ll < gl || i == 0)
{
pg = pl;
gl = ll;
}
}
printf("%.4lf\n%.4lf\n",pg.x,pg.y);
}
return 0;
}