Coverage 圆与直线的交点

Coverage

Time Limit: 1000ms
Memory Limit: 32768KB
64-bit integer IO format:  %I64d      Java class name:  Main
A cell phone user is travelling along a line segment with end points having integer coordinates. In order for the user to have cell phone coverage, it must be within the transmission radius of some transmission tower. As the user travels along the path, cell phone coverage may be gained (or lost) as the user moves inside the radius of some tower (or outside of the radii of all towers). Given the location of up to 100 towers and their transmission radii, you are to compute the percentage of cell phone coverage the user has
along the specified path. The (x,y) coordinates are integers between -100 and 100, inclusive, and the tower radii are integers between 1 and 100, inclusive.

Input

Your program will be given a sequence of configurations, one per line, of the form: N C0X C0Y C1X C1Y T1X T1Y T1R T2X T2Y T2R ... Here, N is the number of towers, (C0X,C0Y) is the start of path of the cell phone user,
(C1X,C1Y) is the end of the path, (TkX,TkY) is the position of the kth tower, and TkR is its transmission radius. The start and end points of the paths are distinct. The last problem is terminated by the line 0

Output

For each configuration, output one line containing the percentage of coverage the cell phone has, rounded to two decimal places.

Sample Input

3 0 0 100 0 0 0 10 5 0 10 15 0 10
1 0 0 100 0 40 10 50
0

Sample Output

25.00
88.99
分析:给出一条路径 再给出n个塔及每个塔发出的信号波长,求被信号覆盖的路所占总路程的百分比;

韦达定理 求线与圆的交点;

 PS:当直线斜率不存在的时候 将直角坐标系翻转90度 即可以在输入的过程中将点的横纵坐标对调 即可保证斜率不存在的情况下也能表示出斜率

<span style="font-size:18px;">#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;
struct gg
{
    double x,y,r,lx,rx;
} a[105];
bool cmp(gg a,gg b)
{
    return a.lx<b.lx;
}
int main()
{
    int n;
    while (scanf("%d",&n))
    {
        if (n==0)break;
        double sx,sy,ex,ey,flag=0,temp;
        scanf("%lf%lf%lf%lf",&sx,&sy,&ex,&ey);
        if (sx==ex)
        {
            temp=sx;
            sx=sy;
            sy=temp;
            temp=ex;
            ex=ey;
            ey=temp;
            flag=1;
        }
        double k,c;
        k=(ey-sy)/(ex-sx);
        c=ey-k*ex;
        for (int i=0; i<n; i++)
        {
            scanf("%lf%lf%lf",&a[i].x,&a[i].y,&a[i].r);
            if(flag)
            {
                temp=a[i].x;
                a[i].x=a[i].y;
                a[i].y=temp;
            }
            double A,B,C,T;
            A=1+k*k;
            B=-2*a[i].x+2*k*c-2*a[i].y*k;
            C=c*c+a[i].y*a[i].y+a[i].x*a[i].x-2*a[i].y*c-a[i].r*a[i].r;
            T=B*B-4*A*C;
            if(T>0)
            {
                a[i].lx=(-B-sqrt(T))/(2*A);
                a[i].rx=(-B+sqrt(T))/(2*A);
                if(a[i].lx>max(sx,ex)||a[i].rx<min(sx,ex))//不在范围
                a[i].lx=a[i].rx=-111;
                else
                {
                    if(a[i].lx<min(sx,ex))a[i].lx=min(sx,ex);
                    if(a[i].rx>max(sx,ex))a[i].rx=max(sx,ex);
                }
            }
            else
                a[i].lx=a[i].rx=-111;

        }
        sort(a,a+n,cmp);
        double sum=0,l=a[0].lx,r=a[0].rx;
        for (int i=0; i<n; i++)
        {
            if(a[i].lx<r)
            {
                r=r>a[i].rx?r:a[i].rx;
            }
            else
            {
                sum+=r-l;
                l=a[i].lx;
                r=a[i].rx;
            }
            if (i==n-1)
                sum+=r-l;
        }
        printf("%.2lf\n",sum/((ex-sx)>0?(ex-sx):(sx-ex))*100);
    }
    return 0;
}</span>




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值