您的位置:首页 > 编程语言 > Go语言

URAL 1348 Goat in the Garden 2计算几何(解题报告)

2012-09-04 13:23 477 查看
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

inputoutput
8 -6 8 6
0 0 7

1.00
3.00

计算几何初级题,题目大意,羊前面有排成一线的菠萝,羊被绳子拴着,现在要求,增加多长的绳子能使羊迟到至少一个菠萝,增加多长的绳子能使羊迟到全部的菠萝。这个题是求点到线段的最短距离,用模版就好了,祥见代码:

#include<stdio.h>
#include<cmath>
#include<stdlib.h>
#include<string.h>
#define eps 1e-6
struct point
{
double x,y;
};
//以下是求两点到线段最短距离的模版,请见: http://www.shabiyuan.com/?p=234 double distance(point p1,point p2)
{
return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}

double xmult(point p1,point p2,point p0)
{
return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}

point intersection(point u1,point u2,point v1,point v2)
{
point ret = u1;
double t = ((u1.x-v1.x)*(v1.y-v2.y)-(u1.y-v1.y)*(v1.x-v2.x))
/((u1.x-u2.x)*(v1.y-v2.y)-(u1.y-u2.y)*(v1.x-v2.x));
ret.x+=(u2.x-u1.x)*t;
ret.y+=(u2.y-u1.y)*t;
return ret;
}

point ptoseg(point p,point l1,point l2)
{
point t=p;
t.x+=l1.y-l2.y;
t.y+=l2.x-l1.x;
if(xmult(l1,t,p)*xmult(l2,t,p)>eps)
{
return distance(p,l1)<distance(p,l2)?l1:l2;

}
return intersection(p,t,l1,l2);
}

double max(double a,double b)
{
return a>b?a:b;
}
int main()
{
point a,b,c;
double l;
double lena,lenb,lenc;
double minlen,maxlen;
scanf("%lf%lf%lf%lf%lf%lf%lf",&a.x,&a.y,&b.x,&b.y,&c.x,&c.y,&l);
minlen = distance(ptoseg(c,a,b),c)-l;
maxlen = max(distance(c,a),distance(c,b))-l;
if(a.x==b.x&&a.y==b.y)//判断线段两端点是不是重合
{
minlen = distance(c,a)-l;
maxlen = distance(c,a)-l;
}
if(minlen<0)//当距离小的时候,当然是不要加长了,变成0,下同。
{
minlen = 0;
}
if(maxlen<0)
{
maxlen = 0;
}
printf("%.2lf\n%.2lf\n",minlen,maxlen);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: