现在的位置: 首页 > 综合 > 正文

sgu 1348 Goat in the Garden 2【点到线段的距离】

2012年11月09日 ⁄ 综合 ⁄ 共 2449字 ⁄ 字号 评论关闭

链接:

1348. Goat in the Garden 2

Time limit: 1.0 second
Memory limit: 64 MB
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 L meters 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 output
8 -6 8 6
0 0 7
1.00
3.00

/*********************************************************
Accepted	132 KB	15 ms	Visual C++ 2010	1630 B
题意:有一条线段 AB
      有一条牛在点 C , 牛上有根绳子长 L 【绳子可以拉伸】
      问点 C 到线段 AB 的最近与最远距离要拉伸绳子多长。
思路:求出点 C 到线段 AB 的最近距离
       和最远距离【最远距离直接与端点比较就好了】
       然后减去 L 的长度,
       如果 < 0则证明在绳长范围内,不用拉伸,输出 0
       否则直接输出所求结果
**********************************************************/
#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;

struct Point{
    double x,y;
    Point() {}
    Point(double _x, double _y){
        x = _x;
        y = _y;
    }
    Point operator - (const Point &B) const
    {
        return Point(x-B.x, y-B.y);
    }
};

double eps = 1e-5;
int dcmp(double x){ /** 精度问题*/
    if(fabs(x) < eps) return 0;
    else return x < 0 ? -1 : 1;
}

bool operator ==(const Point &a, const Point &b)
{
    return dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y) == 0;
}
/** 向量的距离*/
double Length(Point A)
{
    return sqrt(A.x*A.x + A.y*A.y);
}
/** 向量叉积*/
double Cross(Point A, Point B)
{
    return A.x*B.y - A.y*B.x;
}
/** 向量点积*/
double Dot(Point A, Point B)
{
    return A.x*B.x+A.y*B.y;
}

double dist(Point A, Point B)
{
    return sqrt((A.x-B.x)*(A.x-B.x) + (A.y-B.y)*(A.y-B.y));
}

/** 点 p 到线段 AB 距离*/
double DistanceToSegment(Point p, Point A, Point B)
{
    if(A == B) return Length(p-A);
    Point v1 = B-A;
    Point v2 = p-A;
    Point v3 = p-B;
    if(dcmp(Dot(v1, v2)) < 0) return Length(v2);
    else if(dcmp(Dot(v1, v3)) > 0) return Length(v3);
    else return fabs(Cross(v1, v2))/Length(v1);
}

int main()
{
    Point a,b,c;
    double L;
    while(scanf("%lf%lf%lf%lf", &a.x,&a.y,&b.x,&b.y) != EOF)
    {
        scanf("%lf%lf%lf", &c.x,&c.y,&L);
        double ans1 = DistanceToSegment(c, a, b);
        ans1 = ans1-L;

        double ans2 = max(dist(a,c), dist(b,c));
        ans2 = ans2-L;

        if(ans1 < 0) ans1 = 0;
        if(ans2 < 0) ans2 = 0;
        printf("%.2lf\n%.2lf\n", ans1, ans2);
    }
    return 0;
}

抱歉!评论已关闭.