sgu 1348 Goat in the Garden 2【点到线段的距离】
链接:
1348. Goat in the Garden 2
Memory limit: 64 MB
a bed of pineapples that he loves very much. The bed is a line segment with the ends A and B.
Input
The numbers are separated with spaces or line feeds.
Output
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 |
1.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【点到线段的距离】的更多相关文章
- poj1584(判断凸包+求点到线段的距离)
题目链接:https://vjudge.net/problem/POJ-1584 题意:首先要判断凸包,然后判断圆是否在多边形中. 思路: 判断凸包利用叉积,判断圆在多边形首先要判断圆心是否在多边形中 ...
- ural 1348 Goat in the Garden 2
http://acm.timus.ru/problem.aspx?space=1&num=1348 #include <cstdio> #include <cstring&g ...
- 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 ...
- (点到线段的最短距离)51nod1298 圆与三角形
1298 圆与三角形 给出圆的圆心和半径,以及三角形的三个顶点,问圆同三角形是否相交.相交输出"Yes",否则输出"No".(三角形的面积大于0). 收起 ...
- POJ - 1584 A Round Peg in a Ground Hole(判断凸多边形,点到线段距离,点在多边形内)
http://poj.org/problem?id=1584 题意 按照顺时针或逆时针方向输入一个n边形的顶点坐标集,先判断这个n边形是否为凸包. 再给定一个圆形(圆心坐标和半径),判断这个圆是否完全 ...
- POJ 1584 A Round Peg in a Ground Hole 判断凸多边形 点到线段距离 点在多边形内
首先判断是不是凸多边形 然后判断圆是否在凸多边形内 不知道给出的点是顺时针还是逆时针,所以用判断是否在多边形内的模板,不用是否在凸多边形内的模板 POJ 1584 A Round Peg in a G ...
- POJ1584 判断多边形是否为凸多边形,并判断点到直线的距离
求点到直线的距离: double dis(point p1,point p2){ if(fabs(p1.x-p2.x)<exp)//相等的 { return fabs(p2.x-pe ...
- ArcGIS 点到直线的距离
/****点到直线的距离*** * 过点(x1,y1)和点(x2,y2)的直线方程为:KX -Y + (x2y1 - x1y2)/(x2-x1) = 0 * 设直线斜率为K = (y2-y1)/(x2 ...
- csuoj 1503: 点到圆弧的距离
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1503 1503: 点到圆弧的距离 时间限制: 1 Sec 内存限制: 128 MB Speci ...
随机推荐
- 【Zookeeper】Zookeeper 和他的小伙伴们
ZK实际应用场景.实例:
- Keepalived高可用集群应用
Keepalived高可用集群应用 1.keepalived服务说明 1.1.keepalived介绍 Keepalived是一个用C语言编写的路由软件.该项目的主要目标是为Linux系统和基于Lin ...
- Tomcat 启动或者发布项目时提示Publishing failed:Resource /xxxx does not exist
解决方法: 刷新一下项目,有可能是磁盘文件和Eclipse项目中文件不一致造成的. 重新启动eclipse 删除tomcat server 重新发布下即可
- Android模块编译过程中的错误no rules to make target
今天花了不少时间在纠正一个编译错误: make: *** No rule to make target `out/target/common/obj/JAVA_LIBRARIES/sqlite-jdb ...
- 蓝的成长记——追逐DBA(5):不谈技术谈业务,恼人的应用系统
***************************************声明*************************************** 个人在oracle路上的成长记录,当中 ...
- 使用theHarvester 进行邮箱和子域名的收集
下载地址:https://github.com/laramies/theHarvester 先要安装python的 reqeusts 库 安装pip install reqeustsmail -d b ...
- Python 实现指定目录下 删除指定大小的文件
import os, sys from stat import * BIG_FILE_THRESHOLD = 6000L #1000000L dict1 = {} # dict2 = {} # def ...
- svn解决与优化帮助
1.问题的出现 解决方案: 最后一行不能子目录. 启动的时候也是要根目录的.svnserve -d -r /home/svn/repos [不能是svnserve -d -r /home/svn/re ...
- Maven学习小结
简介:一款服务于Java的自动化构建工具 1 安装 必须已经安装了jdk且配置了环境变量,注意查看当前maven版本支持的jdk版本 配置Maven的环境变量 MAVEN_HOME PATH 使用mv ...
- [腾讯 TMQ] 零基础学习 Fiddler 抓包改包
本文转载于https://testerhome.com/topics/7159 一.Fiddler1.1.简介Fiddler是一款HTTP协议调试代理工具,它能够抓取记录本机所有HTTP(S)请求,通 ...