链接:




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. JS方面重点摘要(一)

    1.获取样式(1)style只能获取到行间样式的属性(2)火狐getComputedStyle(obj,null)[attr],IE:obj.currentStyle[attr] 2.ready.on ...

  2. linux中grep命令

    grep 是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来. grep常用用法 [root@www ~]# grep [-acinv] [--color=auto] '搜寻字 ...

  3. ECSHOP热门搜索关键词随机显示

    实现ECSHOP热门搜索关键词随机显示,需要修改ECSHOP模板和ECShOP程序,按照步骤修改即可. 一.打开 include/lib_main.php 文件,找到下面这段代码,删除之(大概在165 ...

  4. 在ecshop中添加页面,并且实现后台管理

    后台一共需要修改下面的四个文件 admin/template.php admin/includes/lib_template.php languages/zh_cn/admin/template.ph ...

  5. [Algorithms] Divide and Recurse Over an Array with Merge Sort in JavaScript

    Merge sort is a recursive sorting algorithm. If you don't understand recursion, I recommend finding ...

  6. Win7系统管理员设置了系统策略,禁止进行此安装,怎么办

    系统管理员设置了系统策略,禁止进行此安装,怎么办 最佳答案 尝试方法一:   windows开始菜单,运行里面输入gpedit.msc打开组策略,   在"计算机配置"→管理模板→ ...

  7. Linux 常见安全检查方法

    Linux 常见安全检查方法进行概要说明: 一.检查系统密码文件,查看文件修改日期 # ls -l /etc/passwd 二.查看 passwd 文件中有哪些特权用户 # awk -F: '$3= ...

  8. Asp.net Mvc使用PagedList分页

    git:https://github.com/troygoode/PagedList 1. Nuget 安装package watermark/2/text/aHR0cDovL2Jsb2cuY3Nkb ...

  9. Python的Django框架中的Cookie相关处理

    Python的Django框架中的Cookie相关处理 浏览器的开发人员在非常早的时候就已经意识到. HTTP's 的无状态会对Web开发人员带来非常大的问题,于是(cookies)应运而生. coo ...

  10. android Activity的onPause()与onResume()

    官方文档地址:http://www.android-doc.com/training/basics/activity-lifecycle/pausing.html#Resume Pause和Resum ...