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

有一个光源,光源到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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值