URAL 1348(几何)(解题报告)

本文详细解析了在'Goat in the Garden 2'问题中,一只羊为了吃到花园内的菠萝,需要如何调整绳子长度以实现目标。通过数学计算,探讨了最小和最大调整绳长的策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


Goat in the Garden 2

Description

A goat is tied to a peg (in a point C) in a garden with a strong rope of the length L (i.e. a goat may eat a grass that is not farther than Lmeters from the peg). There is a bed of pineapples that he loves very much. The bed is a line segment with the ends A and B.

Humph… We wonder, how much the goat is to stretch the roap in order to reach at least one pine apple? And all the pineapples?

Input

There are points’ A, B and C coordinates and a length of the rope L in the input. All the numbers are integer, L ≥ 0, all the coordinates don’t exceed 10000 by the absolute value. The numbers are separated with spaces or line feeds.

Output

The first line should contain the minimal length that the goat is to elongate the rope in order to reach the pineapples bed. The second line should contain the minimal length that the goat is to elongate the rope in order to eat all the pineapples from the bed. All the numbers are to be outputted within two digits after a decimal point.

Sample Input

input

output

8 -6 8 6

0 0 7

3.00

思路:求点到线段的最短和最长距离;:

有几个公式的应用:直线方程:Ax+By+C=0;点到直线的距离:d=|Ax+By+C|/(sqrt(A*A+B*B));

#

include<stdio.h>
#include<math.h>
#include<iostream>
using namespace std;
const double eps=1e-8;
int is_online(double x1,double y1,double x2,double y2,double x,double y)//判断(x,y)是否在线段上 
{
if(((x-x1)*(x-x2)+(y-y1)*(y-y2))<=eps)//(注意精度问题) 
return 1;
else
return 0;
}
int main()
{
    int i,j;
    double A,B,C1,C2,d1,d2,l,d;
    double a1,a2,b1,b2,c1,c2,x,y;
    while(scanf("%lf%lf%lf%lf",&a1,&a2,&b1,&b2)!=EOF)
    {scanf("%lf%lf%lf",&c1,&c2,&l);
    A=b2-a2;
    B=a1-b1;
    C1=a1*(a2-b2)-a2*(a1-b1);//Ax+By=C1表示直线L1方程 
    C2=A*c2-B*c1;//Bx-Ay+C2=0表示过圆心且垂直于直线L1的方程 
    //printf("%.2lf %.2lf %.2lf\n",A,B,C);
    if(A==0&&B==0)
    d1=d2=sqrt((a1-c1)*(a1-c1)+(a2-c2)*(a2-c2));//两点重合的情况 
    else
    { d=sqrt((a1-c1)*(a1-c1)+(a2-c2)*(a2-c2));
      d2=sqrt((b1-c1)*(b1-c1)+(b2-c2)*(b2-c2));
    y=(A*C2-B*C1)/(A*A+B*B);//(x,y)表示L1于L2的交点 
    x=(A*C1+B*C2)/(A*A+B*B)*(-1.0);
    if(is_online(a1,a2,b1,b2,x,y))
    d1=fabs(A*c1+B*c2+C1)/(sqrt(A*A+B*B));//点到直线的共识 
    else
    d1=(d<d2? d:d2);
    d2=(d2>d? d2:d);
    }
    //printf("%.2lf %.2lf",x,y);
    if(d2<=l)
    d2=0;
    else
    d2=d2-l; 
    if(d1<=l)
    d1=0;
    else
    d1=d1-l;
    printf("%.2lf\n%.2lf\n",d1,d2);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值