关键在于对问题的思考,要画图找出几何关系不要用解析几何方法做 (精度不够) 计算几何的微妙之处也在于几何位置关系和角度而不是用解析几何算
画个图 就可以了
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
const int maxn = 505;
const double eps = 1e-8;
double x,y;
struct node {
double x,y,r;
double ang;
}p[maxn];
struct Interval {
double l,r;
}res[maxn];
double dist (double x1,double y1,double x2,double y2) {
return sqrt ((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
}
bool cmp (const Interval a,const Interval b) {
return a.l < b.l;
}
int main () {
int n;
while (scanf ("%d",&n) && n) {
scanf ("%lf%lf",&x,&y);
for (int i = 1;i <= n; ++ i) {
scanf ("%lf%lf%lf",&p[i].x,&p[i].y,&p[i].r);
}
int cnt = 0;
for (int i = 1;i <= n; ++ i) {
double dis = dist (x,y,p[i].x,p[i].y);
double anga = asin (p[i].r / dis);
double angb = asin((x - p[i].x) / dis);
double d1 = tan (anga + angb) * y;
double d2 = tan (angb - anga) * y;
res[cnt].l = x - d1;
res[cnt].r = x - d2;
cnt ++;
}
sort (res,res + cnt,cmp);
int endcnt = 0;
// for (int i = 0;i < cnt; ++ i) {
// printf ("%.2f %.2f\n",res[i].l,res[i].r);
// }
// printf("\n");
for (int i = 0;i < cnt; ++ i) {
res[endcnt].l = res[i].l;
if (i == cnt - 1) {
res[endcnt].l = res[i].l;
res[endcnt ++].r = res[i].r;
break;
}
for (int j = i + 1;j < cnt; ++ j) {
if (res[j].l < res[i].r) {
if (res[j].r > res[i].r) {
res[i].r = res[j].r;
}
}
else {
res[endcnt ++].r = res[i].r;
i = j - 1;
break;
}
if (j == cnt - 1) {
res[endcnt ++].r = res[i].r;
i = j + 1;
break;
}
}
}
for (int i = 0;i < endcnt; ++ i) {
printf ("%.2f %.2f\n",res[i].l,res[i].r);
}
printf("\n");
}
return 0;
}