题目链接:点击打开链接
题意:给定一个圆位于(x1, y1),半径为R,再给定平面上任意一点(x2, y2),在圆内找一点画一个半径为r的圆,使得该圆将该点包含在园内(可以在边界)且使得大圆内不属于小圆部分面积最小。
思路:若(x2, y2)在圆外,则所求圆即大圆。若(x2, y2)在圆内,则所求圆圆心在(x2, y2)与大圆圆心连线处。采用二分法寻找小圆圆心与(x2, y2)的距离(即半径r),使得r最大。计算坐标使用向量的方法。
AC代码:
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <string>
#include <vector>
using namespace std;
#define FSIO ios::sync_with_stdio(0);cin.tie(0);
#define DEBUG(a) cout<<"DEBUG: "<<(a)<<endl;
double R, x1, y1, x2, y2;
double dx, dy, dd;
double r, x, y;
int judge(double rr)
{
if(R*R-(rr-dd)*(rr-dd)>=rr*rr)
{
if(rr>=dd&&(R+dd)>=rr*2.0) return 1;
else if(rr<dd&&rr<R) return 1;
else return 0;
}
else return 0;
}
int main()
{
FSIO;
while(cin>>R>>x1>>y1>>x2>>y2)
{
dx = x2 - x1;
dy = y2 - y1;
dd = sqrt(dx*dx + dy*dy);
if(dd>=R) {printf("%.8f %.8f %.8f\n",x1,y1,R);continue;}
double l = 0, ri = R, mid = 0;
for(int i=1;i<=1000000;++i)
{
mid = (l+ri)/2.0;
if(judge(mid)) l = mid;
else ri = mid;
}
r = mid;
x = x2 - dx*r/dd;
y = y2 - dy*r/dd;
if(fabs(dx)<=1e-6&&fabs(dy)<=1e-6) dx = 1.0, dd=1.0;
x = x2 - dx*r/dd;
y = y2 - dy*r/dd;
double a=x, b = y;
printf("%.8f %.8f %.8f\n",x,y,r);
//cout<<x<<" "<<y<<" "<<r<<endl;
}
return 0;
}