链接:




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;
}










sgu 1348 Goat in the Garden 2【点到线段的距离】的更多相关文章

  1. poj1584(判断凸包+求点到线段的距离)

    题目链接:https://vjudge.net/problem/POJ-1584 题意:首先要判断凸包,然后判断圆是否在多边形中. 思路: 判断凸包利用叉积,判断圆在多边形首先要判断圆心是否在多边形中 ...

  2. ural 1348 Goat in the Garden 2

    http://acm.timus.ru/problem.aspx?space=1&num=1348 #include <cstdio> #include <cstring&g ...

  3. POJ 1584 A Round Peg in a Ground Hole(判断凸多边形,点到线段距离,点在多边形内)

    A Round Peg in a Ground Hole Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4438   Acc ...

  4. (点到线段的最短距离)51nod1298 圆与三角形

    1298 圆与三角形 给出圆的圆心和半径,以及三角形的三个顶点,问圆同三角形是否相交.相交输出"Yes",否则输出"No".(三角形的面积大于0).   收起 ...

  5. POJ - 1584 A Round Peg in a Ground Hole(判断凸多边形,点到线段距离,点在多边形内)

    http://poj.org/problem?id=1584 题意 按照顺时针或逆时针方向输入一个n边形的顶点坐标集,先判断这个n边形是否为凸包. 再给定一个圆形(圆心坐标和半径),判断这个圆是否完全 ...

  6. POJ 1584 A Round Peg in a Ground Hole 判断凸多边形 点到线段距离 点在多边形内

    首先判断是不是凸多边形 然后判断圆是否在凸多边形内 不知道给出的点是顺时针还是逆时针,所以用判断是否在多边形内的模板,不用是否在凸多边形内的模板 POJ 1584 A Round Peg in a G ...

  7. POJ1584 判断多边形是否为凸多边形,并判断点到直线的距离

    求点到直线的距离: double dis(point p1,point p2){   if(fabs(p1.x-p2.x)<exp)//相等的  {    return fabs(p2.x-pe ...

  8. ArcGIS 点到直线的距离

    /****点到直线的距离*** * 过点(x1,y1)和点(x2,y2)的直线方程为:KX -Y + (x2y1 - x1y2)/(x2-x1) = 0 * 设直线斜率为K = (y2-y1)/(x2 ...

  9. csuoj 1503: 点到圆弧的距离

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1503 1503: 点到圆弧的距离 时间限制: 1 Sec  内存限制: 128 MB  Speci ...

随机推荐

  1. JSP Response Set Status

    JSP Response Set Status In this tutorial you will learn about how to set the HTTP status code in JSP ...

  2. Hive 性能调优

    避免执行MR select * or select field1,field2 limit 10 where语句中只有分区字段或该表的本地字段 使用本地set hive.exec.mode.local ...

  3. Elasticsearch教程(五) elasticsearch Mapping的创建

    一.Mapping介绍 在Elasticsearch中,Mapping是什么? mapping在Elasticsearch中的作用就是约束. 1.数据类型声明 它类似于静态语言中的数据类型声明,比如声 ...

  4. 【MVC4】依頼注入

    ※本文参照<ASP.NET MVC 4高级编程>

  5. JavaScript匿名函数与托付

    <1> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <!-- C#匿名函数--& ...

  6. BigDecimal舍入模式使用及建议

    1. 八种舍入模式 此节内容参考于 https://my.oschina.net/sunchp/blog/670909. JDK1.5发布的枚举 RoundingMode 对 BigDecimal 的 ...

  7. oc 把view添加到rootcontrollerview控制的view

    在当前活跃的window 添加一个view [[[[[UIApplication sharedApplication] keyWindow] rootViewController] view] add ...

  8. layout_gravity和gravity的区别

    受不了了,用一遍查一遍...根本记不住,来这里记录一下 layout_gravity是子view相对于父view的位置,比如说,在button中设置了layout_gravity="cent ...

  9. 自己动手开发IOC容器

    前两天写简历.写了一句:精通Spring IoC容器.怎么个精通法?还是自己动手写个IOC容器吧. 什么是IoC(Inversion of Control)?什么是DI(Dependency Inje ...

  10. ISE封装IP

    1.综合成ngc文件,然后再黑盒调用,再写一个端口文件(写个空壳文件,就是定义输入输出,在工程里面调用这个文件就行,把ngc放到工程目录下).