POJ 1375 求圆外一点引圆的切线

关键在于对问题的思考,要画图找出几何关系不要用解析几何方法做 (精度不够) 计算几何的微妙之处也在于几何位置关系和角度而不是用解析几何算
画个图 就可以了

#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;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值