【点到线段距离】URAL - 1348 Goat in the Garden 2

URAL - 1348 Goat in the Garden 2

http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=22321

问题描述:点到线段的最短和最长距离

【最长距离】到两个端点的最大值,好想

【最短距离】首先判断垂足落不落在线段上,可以连成三角形判断底角为锐角,然后点到直线距离;否则,取到两个端点的最小值

思路

点到线段的距离

注意2点,一是点到直线距离时若使用叉乘除以底边的方法要判断底边长度是否为0,否则就除0了!
二者,最后判断minlen 与 maxlen与L的大小关系再确定输出呦~


参考代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<vector>
#include<algorithm>
#include<set>
#include<sstream>
#define eps 1e-9
using namespace std;

typedef long long ll;

const int _max = 2e3 + 10;

int dcmp(double x){
  if(fabs(x)<eps) return 0;else return x < 0?-1:1;
}

double l;

struct point{
  double x,y;
}c,s,t;//源、汇

double len(point a,point b){//返回两点之间的距离
  return hypot(b.x-a.x,b.y-a.y);
}

double dot(point s1,point t1,point s2,point t2){//(t1-s1)点乘(t2-s2)
  double x1 = t1.x - s1.x,y1 = t1.y - s1.y;
  double x2 = t2.x - s2.x,y2 = t2.y - s2.y;
  return x1*x2+y1*y2;
}

double cross(point s1,point t1,point s2,point t2){//(t1-s1)叉乘(t2-s2)
  double x1 = t1.x - s1.x,y1 = t1.y - s1.y;
  double x2 = t2.x - s2.x,y2 = t2.y - s2.y;
  return x1*y2-x2*y1;
}
double dispointtoseg(){//点到线段距离
  if(dcmp(dot(s,c,s,t))<0||dcmp(dot(t,s,t,c))<0) return min(len(c,s),len(c,t));
  //返回点到直线的距离
  if(dcmp(len(s,t)) == 0) return len(s,c);//特判除0的情况
  return fabs(cross(s,c,s,t))/len(s,t);
}

int main(){
 #ifndef ONLINE_JUDGE
 freopen("input.txt","r",stdin);
 #endif // ONLINE_JUDGE
 while(scanf("%lf%lf%lf%lf%lf%lf%lf",&s.x,&s.y,&t.x,&t.y,&c.x,&c.y,&l) == 7){
    double d1 = dispointtoseg();
    double d2 = max(len(c,s),len(c,t));
    if(dcmp(d1-l)<=0) puts("0.00"); else printf("%.2f\n",d1-l);
    if(dcmp(d2-l)<=0) puts("0.00"); else printf("%.2f\n",d2-l);
    //printf("%.2lf\n%.2lf\n",d1-l,d2-l);
 }
 return 0;
}
  • 加粗 Ctrl + B
  • 斜体 Ctrl + I
  • 引用 Ctrl + Q
  • 插入链接 Ctrl + L
  • 插入代码 Ctrl + K
  • 插入图片 Ctrl + G
  • 提升标题 Ctrl + H
  • 有序列表 Ctrl + O
  • 无序列表 Ctrl + U
  • 横线 Ctrl + R
  • 撤销 Ctrl + Z
  • 重做 Ctrl + Y
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
用代码解决这个问题The program committee of the school programming contests, which are often held at the Ural State University, is a big, joyful, and united team. In fact, they are so united that the time spent together at the university is not enough for them, so they often visit each other at their homes. In addition, they are quite athletic and like walking. Once the guardian of the traditions of the sports programming at the Ural State University decided that the members of the program committee spent too much time walking from home to home. They could have spent that time inventing and preparing new problems instead. To prove that, he wanted to calculate the average distance that the members of the program committee walked when they visited each other. The guardian took a map of Yekaterinburg, marked the houses of all the members of the program committee there, and wrote down their coordinates. However, there were so many coordinates that he wasn't able to solve that problem and asked for your help. The city of Yekaterinburg is a rectangle with the sides parallel to the coordinate axes. All the streets stretch from east to west or from north to south through the whole city, from one end to the other. The house of each member of the program committee is located strictly at the intersection of two orthogonal streets. It is known that all the members of the program committee walk only along the streets, because it is more pleasant to walk on sidewalks than on small courtyard paths. Of course, when walking from one house to another, they always choose the shortest way. All the members of the program committee visit each other equally often. Input The first line contains the number n of members of the program committee (2 ≤ n ≤ 105). The i-th of the following n lines contains space-separated coordinates xi, yi of the house of the i-th member of the program committee (1 ≤ xi, yi ≤ 106). All coordinates are integers. Output Output the average distance, rounded down to an integer, that a member of the program committee walks from his house to the house of his colleague.
最新发布
05-26

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值