bnuoj 1053 EASY Problem (计算几何)

http://www.bnuoj.com/bnuoj/problem_show.php?pid=1053

【题意】:基本上就是求直线与圆的交点坐标

【题解】:这种题我都比较喜欢用二分,三分做,果然可以完爆,哈哈,特有成就感的说。。。

【code】:

 #include <iostream>
#include <stdio.h>
#include <math.h>
#include <algorithm> using namespace std;
#define eps 1e-12 struct Point
{
double x,y;
Point(){}
Point(double dx,double dy)
{
x = dx;
y = dy;
}
}; double distance(double x1,double y1,double x2,double y2) //求距离
{
return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
} double distance2(double x1,double y1,double x2,double y2) //求距离的平方
{
return ((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
} double area2(double x1, double y1, double x2, double y2, double x3,double y3) //两倍三角形面积
{
double area;
area = fabs(x1*y2+x2*y3+x3*y1-x3*y2-x1*y3-x2*y1);
return area;
} int isEqual(double a,double b) //判断两浮点数是否相等
{
if(fabs(a-b)>1e-) return ;
return ;
} Point bs(double x1,double y1,double x2,double y2,double x3,double y3,double R) //二分查找交点
{
double l=,r=,mid,x,y;
while(l<=r)
{
mid = (l+r)/;
x = x1 + mid*(x2-x1);
y = y1 + mid*(y2-y1);
double temp =distance(x,y,x3,y3);
if(temp<R) //与半径的距离为二分点
{
l=mid+eps;
}
else
{
r=mid-eps;
}
}
return Point(x,y);
} int main()
{
double Cx,Cy,R;
double Px,Py;
double Qx,Qy;
scanf("%lf%lf%lf",&Cx,&Cy,&R);
scanf("%lf%lf",&Px,&Py);
scanf("%lf%lf",&Qx,&Qy);
double area = area2(Px,Py,Qx,Qy,Cx,Cy);
double disPQ = distance(Px,Py,Qx,Qy);
double dis = area/disPQ; //用面积除以底求得三角形的高,即点到直线的距离
if(dis>R||isEqual(dis,R))
{
puts("-1");
return ;
}
double x1,y1;
x1 = Px-Qx;
y1 = Py-Qy;
double x=,y=,k=,lx,rx,ry,ly;
if(isEqual(Px,Qx)) //如果px==qx,不存在斜率
{
x = Px;
y = (Cx-x)*x1/y1+Cy;
lx = x;
ly = Cy-R;
rx = x;
ry = Cy+R;
}
else if(isEqual(Py,Qy)) //存在斜率为1
{
y = Py;
x = (Cy-y)*y1/x1+Cx;
ly = Py;
lx = Px-R;
ry = Py;
rx = Px+R;
}
else //斜率在0-1之间
{
y = (Cy*y1/x1+Cx-Px+Py*(Qx-Px)/(Qy-Py))/(y1/x1+(Qx-Px)/(Qy-Py));
x = (Cy-y)*y1/x1+Cx;
lx = Cx - R;
ly = (Qy-Py)/(Qx-Px)*(lx-Px)+Py;
rx = Cx + R;
ry = (Qy-Py)/(Qx-Px)*(rx-Px)+Py;
}
Point p1 = bs(x,y,lx,ly,Cx,Cy,R);
Point p2 = bs(x,y,rx,ry,Cx,Cy,R);
double ans = distance2(p1.x,p1.y,Px,Py)+distance2(p2.x,p2.y,Px,Py);
printf("%.3lf\n",ans);
return ;
}
上一篇:POJ 2031 Building a Space Station (计算几何+最小生成树)


下一篇:HDU 5572 An Easy Physics Problem (计算几何+对称点模板)