首页 > 代码库 > POJ 1375

POJ 1375

可以通过顶角角度来计算切线与坐标轴的坐标

开始还以为有公式可以算,谁知道原来是解二元一次方程,靠。。

#include <iostream>#include <cstdio>#include <algorithm>#include <cmath>using namespace std;const int MAX=1050;struct point{	double x;	int flag;	point(){ flag=0;}}p[MAX];double dist(double x1,double y1,double x2,double y2){	return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}bool cmp(point A, point B){	if(A.x<B.x)return true;	else if(A.x==B.x){		if(A.flag<B.flag) return true;	}	return false;}int main(){	double lx,ly; int n; double x,y,r;	while(scanf("%d",&n)!=EOF){		if(n==0) break;		scanf("%lf%lf",&lx,&ly);		for(int i=0;i<n;i++){			scanf("%lf%lf%lf",&x,&y,&r);			double dis=dist(lx,ly,x,y);			double Agle=asin(r/dis); double Bgle=asin((lx-x)/dis);			double bx=lx-ly*tan(Agle+Bgle); 			double ex=lx-ly*tan(Bgle-Agle);			p[2*i].x=bx; p[2*i].flag=-1;			p[2*i+1].x=ex; p[2*i+1].flag=1;		}		sort(p,p+2*n,cmp);		int now=0; double L;		for(int i=0;i<2*n;i++){			if(now==0){			L=p[i].x;			}			now+=p[i].flag;			if(now==0)			printf("%0.2f %0.2f\n",L,p[i].x);		}		printf("\n");	}	return 0;}