您的位置:首页 > 其它

poj 1375 Intervals(圆外一点到圆的切点)

2013-09-05 12:50 281 查看
有一个光源,光源到x轴之间有n个圆,求x轴上的圆的阴影的区间。

先求出点到圆的切点,然后根据光源与切点求出直线方程,然后求出直线与x轴的交点,最后判断下线段在x轴上的重叠就行了。

#include<iostream>
#include<cstdio>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<queue>
#include<stack>
#include<map>
#include<vector>

#define maxn 505
using namespace std;

const int inf=0x7ffffff;
const double PI=acos(-1.0);
const double eps=1e-8;
const double e=2.7182818284590452354;

struct point
{
double x,y;
point () {}
point (double x,double y): x(x),y(y) {}
}p[maxn*4],ans[maxn*4];
struct Circle
{
point c;
double r;
Circle() {}
Circle( point c, double r ): c(c), r(r) {}
point getPoint( double theta )
{
return point( c.x + cos(theta)*r, c.y + sin(theta)*r );
}
}pc[maxn];
double cross(point a,point b,point op)
{
return (op.x-a.x)*(op.y-b.y)-(op.x-b.x)*(op.y-a.y);
}

double dist(point a,point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
void getTangentPoints( point p, Circle C, point &r1, point &r2)
{
double dis = dist(p,C.c);
double base = atan2( p.y - C.c.y, p.x - C.c.x );
double ang = acos( C.r / dis );
r1 = C.getPoint( base - ang );
r2 = C.getPoint( base + ang );
}
double getline(point x,point y)
{
double a,b,c;
a=y.y-x.y;
b=x.x-y.x;
c=y.x*(x.y-y.y)+y.y*(y.x-x.x);
return (-c/a);
}

bool cmp(point a,point b)
{
return a.x<b.x;
}
int main()
{
int n;
point p0;
double x0,y0;
while(~scanf("%d",&n),n)
{
scanf("%lf%lf",&x0,&y0);
p0=point(x0,y0);
for(int i=0;i<n;i++)
{
point r1,r2;
scanf("%lf%lf%lf",&pc[i].c.x,&pc[i].c.y,&pc[i].r);
getTangentPoints(p0,pc[i],r1,r2);
double u=getline(p0,r1);
double v=getline(p0,r2);
if(u>v) swap(u,v);
ans[i].x=u;
ans[i].y=v;
}
sort(ans,ans+n,cmp);
double pre1=ans[0].x;
double pre2=ans[0].y;
for(int i=1;i<n;i++)
{
if(ans[i].x>pre2)
{
printf("%.2lf %.2lf\n",pre1,pre2);
pre1=ans[i].x;
pre2=ans[i].y;
}
else pre2=max(ans[i].y,pre2);
}
printf("%.2lf %.2lf\n\n",pre1,pre2);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: