链接:




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. barrier and Fence

    barrier 管理的是commandbuffer里面 command之间 fence管理的是queue之间 queue和cpu之间的顺序 通过flag比如等待所有面片画完 ------------- ...

  2. 本地启动tomcat的时候报内存溢出错误:java.util.concurrent.ExecutionException: java.lang.OutOfMemoryError: PermGen space

    问题分析: PermGen space的全称是Permanent Generation space,是指内存的永久保存区域,这块内存主要是被JVM存放Class和Meta信息的,Class在被Load ...

  3. ElasticSearch 安装中文分词器

    1.安装中文分词器IK 下载地址:https://github.com/medcl/elasticsearch-analysis-ik 在线下载安装: elasticsearch-plugin.bat ...

  4. HDOJ 3359 Kind of a Blur

    用高斯消元对高斯模糊的图像还原.... Kind of a Blur Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/327 ...

  5. python 处理抓取网页乱码问题一招鲜

    FROM: http://my.oschina.net/012345678/blog/122355 相信用python的人一定在抓取网页时,被编码问题弄晕过一阵 前几天写了一个测试网页的小脚本,并查找 ...

  6. 一个对比各种开源库的网站 libhunt

    https://www.libhunt.com/ https://github.com/LibHunt/awesome-javascript

  7. USE [EPPM] [dbo].[REFRDEL_CLEANUP]

    USE [EPPM] GO /****** Object: StoredProcedure [dbo].[REFRDEL_CLEANUP] Script Date: 2016/4/2 16:32:29 ...

  8. Ubuntu下使用Sublime text 3阅读android源代码

    一.安装Sublime text 3 Sublime Text 是一款流行的文本编辑器软件,有点类似于TextMate,跨平台,可运行在Linux,Windows和Mac OS X.也是许多程序员喜欢 ...

  9. 【菜鸟学注入】之MySQL报错注入详解

    本文转自:http://bbs.blackbap.org/forum.php?mod=viewthread&tid=6483&highlight=mysql%2B报错注入 用SQL注入 ...

  10. Linux组件封装(一)中互斥锁MutexLock的封装

    本文对Linux中的pthread_mutex_t做一个简易的封装. 互斥锁主要用于互斥,互斥是一种竞争关系,主要是某一个系统资源或一段代码,一次做多被一个线程访问. 条件变量主要用于同步,用于协调线 ...