Distance
For given two segments s1 and s2, print the distance between them.
s1 is formed by end points p0 and p1, and s2 is formed by end points p2 and p3.
Input
The entire input looks like:
q (the number of queries) 1st query 2nd query ... qth query
Each query consists of integer coordinates of end points of s1 and s2 in the following format:
xp0 yp0 xp1 yp1 xp2 yp2 xp3 yp3
Output
For each query, print the distance. The output values should be in a decimal fraction with an error less than 0.00000001.
Constraints
- 1 ≤ q ≤ 1000
- -10000 ≤ xpi, ypi ≤ 10000
- p0 ≠ p1 and p2 ≠ p3.
Sample Input
3 0 0 1 0 0 1 1 1 0 0 1 0 2 1 1 2 -1 0 1 0 0 1 0 -1
Sample Output
1.0000000000 1.4142135624 0.0000000000
题目大意:求解两条线段之间的最短距离
题解:
对于两条线段来讲,之间的最短距离只有两种情况:
(1)两线段相交:此时它们之间的最短距离就是0.
(2)两线段不相交: 此时最短距离就是两线段的四个端点到与其不相交的线段的最短距离。(我们需要计算四种组合(每条线段的两个端点分别与另一条线段)之间的距离再求min)
代码如下
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
const double eps = 1e-10;
int sign(double x)
{
if (fabs(x) <= eps) return 0;
if (x > 0) return 1;
return -1;
}
struct Point
{
double x, y;
Point(double x = 0, double y = 0) :x(x), y(y) { }
Point operator - (Point& t) { return Point(x - t.x, y - t.y); }
Point operator + (Point& t) { return Point(x + t.x, y + t.y); }
Point operator * (double & t) { return Point(x * t, y * t); }
bool operator ==(Point& t)
{
return !sign(x - t.x) && !sign(y - t.y);
}
};
typedef Point Vector;
struct Line
{
Point p;
Vector v;
Line(Point p, Vector v) : p(p), v(v) { }
};
double cross(Vector a, Vector b)
{
return a.x * b.y - b.x * a.y;
}
double dot(Vector a, Vector b)
{
return a.x * b.x + a.y * b.y;
}
double norm(Vector a)
{
return sqrt(dot(a, a));
}
double get_length(Vector a)
{
return sqrt(dot(a, a));
}
double Distance_point_to_line(Point P, Point A, Point B)
{
Vector v1 = B - A, v2 = P - A;
return fabs(cross(v1, v2) / get_length(v1));
}
//计算点到线段的距离
//垂线距离 或者PA,PB的距离
double Distance_point_to_segment(Point P, Point A, Point B)
{
if (A == B) return get_length(P - A);
Vector v1 = B - A, v2 = P - A, v3 = P - B;
if (sign(dot(v1, v2)) < 0) return get_length(v2);
if (sign(dot(v1, v3)) > 0) return get_length(v3);
return Distance_point_to_line(P, A, B);
}
double Distance_segment_to_segment(Point A, Point B, Point C, Point D)
{
Vector v1 = B - A, v2 = D - C;
double ans = min(min(Distance_point_to_segment(A, C, D), Distance_point_to_segment(B, C, D)), min(Distance_point_to_segment(C, A, B), Distance_point_to_segment(D, A, B)));
return ans;
}
bool OnSegment(Point p, Point a1, Point a2)
{
return sign(cross(a1 - p, a2 - p)) == 0 && sign(dot(a1 - p, a2 - p)) <= 0;
}
bool Segment_proper_intersection(Point a1, Point a2, Point b1, Point b2) {
double c1 = cross(a2 - a1, b1 - a1), c2 = cross(a2 - a1, b2 - a1);
double c3 = cross(b2 - b1, a1 - b1), c4 = cross(b2 - b1, a2 - b1);
//if判断控制是否允许线段在端点处相交,根据需要添加
if (!sign(c1) || !sign(c2) || !sign(c3) || !sign(c4)) {
bool f1 = OnSegment(b1, a1, a2);
bool f2 = OnSegment(b2, a1, a2);
bool f3 = OnSegment(a1, b1, b2);
bool f4 = OnSegment(a2, b1, b2);
bool f = (f1 | f2 | f3 | f4);
return f;
}
return (sign(c1) * sign(c2) < 0 && sign(c3) * sign(c4) < 0);
}
int main()
{
int m;
cin >> m;
while (m--)
{
Point A, B, C, D;
cin >> A.x >> A.y >> B.x >> B.y >> C.x >> C.y >> D.x >> D.y;
Distance_segment_to_segment(A, B, C, D);
if (Segment_proper_intersection(A, B, C, D)) printf("%.10lf\n", 0.0000000000);
else printf("%.10lf\n", Distance_segment_to_segment(A, B, C, D));
}
return 0;
}