The Olympic Games in Bercouver are in full swing now. Here everyone has their own objectives: sportsmen compete for medals, and sport commentators compete for more convenient positions to give a running commentary. Today the main sport events take place at three round stadiums, and the commentator's objective is to choose the best point of observation, that is to say the point from where all the three stadiums can be observed. As all the sport competitions are of the same importance, the stadiums should be observed at the same angle. If the number of points meeting the conditions is more than one, the point with the maximum angle of observation is prefered.
Would you, please, help the famous Berland commentator G. Berniev to find the best point of observation. It should be noted, that the stadiums do not hide each other, the commentator can easily see one stadium through the other.
The input data consists of three lines, each of them describes the position of one stadium. The lines have the format x, y, r, where (x, y) are the coordinates of the stadium's center ( - 103 ≤ x, y ≤ 103), and r(1 ≤ r ≤ 103) is its radius. All the numbers in the input data are integer, stadiums do not have common points, and their centers are not on the same line.
Print the coordinates of the required point with five digits after the decimal point. If there is no answer meeting the conditions, the program shouldn't print anything. The output data should be left blank.
0 0 10 60 0 10 30 30 10
30.00000 0.00000
题意:
给出三个圆的坐标以及半径,让你找到一个点,使得这个点到三个圆的视角相等(视角:从一点引圆的切线,两条切线的夹角),求这个点的坐标
题解:
如果用数学方法解几何问题,是可以做出来的,但是会很麻烦,看到别人写的模拟退火算法,学习了
感觉就是一种暴力枚举的方法,只是在枚举中,每次都向最优解进行搜索,但是这个算法是概率算法,有一定的失败概率,成功与否取决于枚举的策略
/*
http://blog.youkuaiyun.com/liuke19950717
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const double eps=1e-5;
struct point
{
double x,y;
double r;
}p[3];
double dist(double x,double y,point tp)
{
return sqrt((x-tp.x)*(x-tp.x)+(y-tp.y)*(y-tp.y));
}
double cost(double x,double y)
{
double ang[3];//当前解对应的三个视角
for(int i=0;i<3;++i)
{
ang[i]=dist(x,y,p[i])/p[i].r;
}
double d[3];//视角的差值
for(int i=0;i<3;++i)
{
d[i]=ang[i]-ang[(i+1)%3];
}
double ans=0;
for(int i=0;i<3;++i)
{
ans+=d[i]*d[i];//找到一个定量来表示误差
}
return ans;
}
int main()
{
while(~scanf("%lf%lf%lf",&p[0].x,&p[0].y,&p[0].r))
{
for(int i=1;i<3;++i)
{
scanf("%lf%lf%lf",&p[i].x,&p[i].y,&p[i].r);
}
double x=0,y=0;
for(int i=0;i<3;++i)
{
x+=p[i].x/3;y+=p[i].y/3;//先大约确定最优解可能的范围,大约是重心附近
}
double t=1.0;
while(t>eps)
{
int kase=0;
double tp=cost(x,y);//当前的误差
if(cost(x+t,y)<tp)//四个方向进行尝试枚举
{
x+=t;kase=1;
}
else if(cost(x-t,y)<tp)
{
x-=t;kase=1;
}
else if(cost(x,y+t)<tp)
{
y+=t;kase=1;
}
else if(cost(x,y-t)<tp)
{
y-=t;kase=1;
}
if(!kase)//如果没有更优解,缩小尝试的范围
{
t/=2;
}
}
if(fabs(cost(x,y)<eps))//查看是否足够精确
{
printf("%lf %lf\n",x,y);
}
}
return 0;
}