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>




下属思路做对比,判断已有代码是否正确 一、公式提取 1. 坡面平面方程 设坡面法向量为\(\boldsymbol{n}=(\sin\alpha, 0, \cos\alpha)\)(位于\(xoz\)平面),且坡面过原点\((0,0,0)\),则坡面所在平面的一般方程为: \[ x\sin\alpha + z\cos\alpha = 0 \tag{8} \] 2. 测线方向向量 测线方向在\(xoy\)平面投影\(x\)轴夹角为\(\beta\),则测线方向向量为: \[ \boldsymbol{t}=(\cos\beta, \sin\beta, 0) \] 3. 测量船坐标 船距海域中心点距离为\(d_c\),中心点水深120 m,故测量船坐标为: \[ (x_0, y_0, z_0) = \left(d_c\cos\beta, d_c\sin\beta, 120\right) \] 4. 声波边界直线的方向向量 波束左右两侧声波边界直线\(l_1, l_2\)的方向向量(测线垂直、\(z\)轴夹角\(60^\circ\)): \[ \boldsymbol{l_1} = \left(-\sin\beta, \cos\beta, -\frac{\sqrt{3}}{3}\right), \quad \boldsymbol{l_2} = \left(\sin\beta, -\cos\beta, -\frac{\sqrt{3}}{3}\right) \] 5. 直线的点向式方程 - \(l_1\)的点向式方程: \[ \frac{x - d_c\cos\beta}{-\sin\beta} = \frac{y - d_c\sin\beta}{\cos\beta} = \frac{z - 120}{-\frac{\sqrt{3}}{3}} \tag{9} \] - \(l_2\)的点向式方程: \[ \frac{x - d_c\cos\beta}{\sin\beta} = \frac{y - d_c\sin\beta}{-\cos\beta} = \frac{z - 120}{-\frac{\sqrt{3}}{3}} \tag{10} \] 6. 直线的参数方程 - \(l_1\)的参数方程(\(t_1\)为参数): \[ \begin{cases} x = d_c\cos\beta - \sin\beta \cdot t_1 \\ y = d_c\sin\beta + \cos\beta \cdot t_1 \\ z = 120 - \frac{\sqrt{3}}{3} \cdot t_1 \end{cases} \tag{11} \] - \(l_2\)的参数方程(\(t_2\)为参数): \[ \begin{cases} x = d_c\cos\beta + \sin\beta \cdot t_2 \\ y = d_c\sin\beta - \cos\beta \cdot t_2 \\ z = 120 - \frac{\sqrt{3}}{3} \cdot t_2 \end{cases} \tag{12} \] 7. 直线坡面交点的参数解 将参数方程代入坡面平面方程\(x\sin\alpha + z\cos\alpha = 0\),解得参数\(t_1, t_2\): \[ t_1 = \frac{d_c\sin\alpha \cos\beta + 120\cos\alpha}{\frac{\sqrt{3}}{3}\cos\alpha + \sin\alpha \sin\beta}, \quad t_2 = \frac{d_c\sin\alpha \cos\beta + 120\cos\alpha}{\frac{\sqrt{3}}{3}\cos\alpha - \sin\alpha \sin\beta} \] 8. 覆盖宽度公式 两交点间的空间距离(覆盖宽度\(W\)): \[ W = \sqrt{(x_1 - x_2)^2 + (y_1 - y_2)^2 + (z_1 - z_2)^2} = \sqrt{\left(\sin^2\beta + \frac{1}{3}\right)(t_1 - t_2)^2 + \cos^2\beta (t_1 + t_2)^2} \tag{13} \] 二、步骤提取 1. 坐标系构建: 以矩形待测海域中心点对应的海底处为原点,海底坡面法向量在原点水平面的投影为\(x\)轴,水平面坡面的交线为\(y\)轴,竖直方向为\(z\)轴,建立三维直角坐标系。 2. 坡面几何特征建模: - 设坡面法向量\(\boldsymbol{n}=(\sin\alpha, 0, \cos\alpha)\)(位于\(xoz\)平面),推导坡面平面方程\(x\sin\alpha + z\cos\alpha = 0\)。 - 定义测线方向向量\(\boldsymbol{t}=(\cos\beta, \sin\beta, 0)\)(由测线坡面法向量投影的夹角\(\beta\)确定)。 3. 测量船声波边界直线建模: - 由船距中心点距离\(d_c\)和水深120 m,确定测量船坐标\((d_c\cos\beta, d_c\sin\beta, 120)\)。 - 由波束张角\(120^\circ\)、边界直线测线垂直且\(z\)轴夹角\(60^\circ\),推导边界直线\(l_1, l_2\)的方向向量。 4. 直线方程交点求解: - 写出\(l_1, l_2\)的点向式方程,转化为参数方程(含参数\(t_1, t_2\))。 - 将参数方程代入坡面平面方程,解出参数\(t_1, t_2\),进而得到直线坡面的交点坐标。 5. 覆盖宽度计算: 利用空间两点距离公式,结合交点坐标,推导覆盖宽度\(W\)的表达式。 以上为图示内容中公式推导步骤的完整提取。 以上内容均由AI搜集总结并生成,仅供参考
08-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值