【CodeForces 935C --- Fifa and Fafa】几何 || 圆
题目来源:点击进入【CodeForces 935C — Fifa and Fafa】
Description
Fifa and Fafa are sharing a flat. Fifa loves video games and wants to download a new soccer game. Unfortunately, Fafa heavily uses the internet which consumes the quota. Fifa can access the internet through his Wi-Fi access point. This access point can be accessed within a range of r meters (this range can be chosen by Fifa) from its position. Fifa must put the access point inside the flat which has a circular shape of radius R. Fifa wants to minimize the area that is not covered by the access point inside the flat without letting Fafa or anyone outside the flat to get access to the internet.
The world is represented as an infinite 2D plane. The flat is centered at (x1, y1) and has radius R and Fafa’s laptop is located at (x2, y2), not necessarily inside the flat. Find the position and the radius chosen by Fifa for his access point which minimizes the uncovered area.
Input
The single line of the input contains 5 space-separated integers R, x1, y1, x2, y2 (1 ≤ R ≤ 105, |x1|, |y1|, |x2|, |y2| ≤ 105).
Output
Print three space-separated numbers xap, yap, r where (xap, yap) is the position which Fifa chose for the access point and r is the radius of its range.
Your answer will be considered correct if the radius does not differ from optimal more than 10 - 6 absolutely or relatively, and also the radius you printed can be changed by no more than 10 - 6 (absolutely or relatively) in such a way that all points outside the flat and Fafa’s laptop position are outside circle of the access point range.
Sample Input
5 3 3 1 1
Sample Output
3.7677669529663684 3.7677669529663684 3.914213562373095
解题思路
由题意分析知以(x2,y2)点出发过(x1,y1)交于圆上的线段为答案所对应圆的直径。
我们只需求出(x,y)和半径就行。
我们很容易就可以得到r1=sqrt((x1-x2)(x1-x2)+(y1-y2)(y1-y2));
然后通过相似三角形我们可以得到r1/((R+r1)/2)=(x2-x1)/(x2-x)=(y2-y1)/(y2-y);
最后稍微化简就可以直接得到(x,y).半径的话就只需要(r1+R)/2就行了。
AC代码:
#include <iostream>
#include <cmath>
#include <cstdio>
using namespace std;
int main()
{
double R,x1,x2,y1,y2;
scanf("%lf%lf%lf%lf%lf",&R,&x1,&y1,&x2,&y2);
double r1=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
if(r1>R)
{
printf("%.10f %.10f %.10f\n",x1,y1,R);
return 0;
}
if(x1==x2 && y1==y2)
{
printf("%.10f %.10f %.10f\n",x1+0.5*R,y1,0.5*R);
return 0;
}
double x=(R+r1)/(r1*2);
printf("%.10f %.10f %.10f\n",x2-(x2-x1)*x,y2-(y2-y1)*x,(R+r1)/2);
return 0;
}