URAL 1348. Goat in the Garden 2[求点到线段的距离]

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1348

题目的意思是:求一个点到线段的最短距离和最长距离。。

最长距离比较容易,就是求点到线段两个端点较长的那个距离就是ans。

最短距离就比较有意思了。。。

可能的情况就是点到线段的垂线的垂足在线段内,还有就是垂足在线段外。。。

在线段内的话,那么应用叉积求面积+底面长度可以求得垂线长度也就是最短距离。。

如果在线段外的话,最短距离就是点到线段的两个端点的最小值。。

那么问题就来了。。怎么判断垂足在线段内还是在线段外的呢??

详细见代码。 - - 。。。

Code:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;

const double eps = 1e-8;
const double pi = acos(-1);
//点
struct POINT
{
    double x, y;
    POINT(){ }
    POINT(double a, double b){
        x = a;
        y = b;
    }
};
//线段
struct Seg
{
    POINT a, b;
    Seg() { }
    Seg(POINT x, POINT y){
        a = x;
        b = y;
    }
};

//直线
struct Line
{
    POINT a, b;
    Line() {}
    Line(POINT x, POINT y){
        a = x;
        b = y;
    }
};

//叉乘
double cross(POINT o, POINT a, POINT b)
{
    return (a.x - o.x) * (b.y - o.y) - (b.x - o.x) * (a.y - o.y);
}

//求两点间的距离
double dis(POINT a, POINT b)
{
    return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
Seg s;
POINT p;
double L;
//点到直线的距离..
double PointToLine(POINT p, Line l)
{
    return fabs(cross(p, l.a, l.b)) / dis(l.a, l.b);
}
//线段到直线的距离..
double PointToSeg(POINT p, Seg s)
{
    POINT tmp = p;
    tmp.x += s.a.y - s.b.y;
    tmp.y += s.b.x - s.a.x;
    if(cross(s.a, p, tmp) * cross(s.b, p, tmp) >= 0){
        return min(dis(p, s.a), dis(p, s.b));
    }
    return PointToLine(p, Line(s.a, s.b));
}

void solve()
{
    double ans1 = PointToSeg(p, s), ans2 = max(dis(p, s.a), dis(p, s.b));
    printf("%.2lf\n%.2lf\n", ans1 > L ? ans1 - L : 0, ans2 > L ? ans2 - L : 0);
    return ;
}

int main()
{
//    freopen("11.txt", "r", stdin);
    while(~scanf("%lf %lf %lf %lf", &s.a.x, &s.a.y, &s.b.x, &s.b.y)){
        scanf("%lf %lf %lf", &p.x, &p.y, &L);
        solve();
    }
    return 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、付费专栏及课程。

余额充值