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 ...
随机推荐
- Hive设置参数的三种方法
Hive提供三种可以改变环境变量的方法,分别是:(1).修改${HIVE_HOME}/conf/hive-site.xml配置文件:(2).命令行参数:(3).在已经进入cli时进行参数声明.下面分别 ...
- HDFS源码分析之UnderReplicatedBlocks(一)
http://blog.csdn.net/lipeng_bigdata/article/details/51160359 UnderReplicatedBlocks是HDFS中关于块复制的一个重要数据 ...
- asp.net服务器数据源控件学习笔记
1.数据绑定控件的DataSource属性只能接受三种接口类型的数据 (IListSource,IEnumerable,IDataSource) 2.要手动在已经绑定数据的数据绑定控件上添加自定义的数 ...
- scrapy爬虫程序xpath中文编码报错
2017-03-23 问题描述: #选择出节点中“时间”二字 <h2>时间</h2> item["file_urls"]= response.xpath(& ...
- Node.js 网页爬虫再进阶,cheerio助力
任务还是读取博文标题. 读取app2.js // 内置http模块,提供了http服务器和客户端功能 var http=require("http"); // cheerio模块, ...
- webstrom 很卡 底下一直走进度条 scanning files to index
最近工作总会遇到一些问题 先说说webstrom 其中有一次仅仅开了两个项目 电脑风扇就各种轰鸣 各种重启 安装卸载webstrom 都没有作用 好吧 其实解决很简单 选择一个文件夹,右键, Mark ...
- AlamoFireDemo
// // ViewController.swift // AlamFireDemo // // import UIKit import Alamofire class ViewController: ...
- (九)Thymeleaf用法——Themeleaf注释
4. 注释 模板名称:comment.html 4.1 标准 HTML/XML注释 语法:<!-- --> 4.2 解析器级注释块(Parser-level ...
- Spring使用经验之StandardServletMultipartResolver实现文件上传的基本配置
Note:Spring使用版本是4.1.6.RELEASE 1. 在实现了AbstractAnnotationConfigDispatcherServletInitializer的类中重载custom ...
- 谈一谈Http Request 与Http Response
1.什么是HTTPRequest与HTTP Response? 我们平时打开浏览器,输入网址,点击Enter按键,然后我们想要的网页就呈现在我们的眼前,可是这个过程是怎么实现的呢? 简单来说是这样的: ...