Question: four points are given to form a quadrilateral (not given in order), and The ferma distance is obtained.
1. Because there are only four points, when these four points can form a convex Quadrilateral (): the intersection point formed by the diagonal line is the horse point. It is point 1. it can be proved that the two sides of the triangle are greater than the third side.
2. When a convex polygon cannot be formed, that is, the concave point is a Fermat point (): Likewise, it can be proved.
The Code is as follows: // a bit ugly TT
[Cpp]
# Include <cstdio>
# Include <iostream>
# Include <math. h>
# Define eps 1e-8
Using namespace std;
Const int maxn = 4;
Struct point {double x, y;} points [maxn];
Int n = 4;
Double dis (point p1, point p2)
{
Return sqrt (p1.x-p2.x) * (p1.x-p2.x) + (p1.y-p2.y) * (p1.y-p2.y ));
}
Double alldis (point tmp)
{
Double sum = 0;
Int I;
For (I = 0; I <n; I ++)
{
Sum + = dis (tmp, points [I]);
}
Return sum;
}
Double xmult (point p1, point p2, point p0)
{
Return (p1.x-Snapshot X) * (p2.y-Snapshot y)-(p2.x-Snapshot X) * (p1.y-Snapshot y );
}
Point intersection (point p1, point p2, point p3, point p4)
{
Point ret = p1;
Double t = (p1.x-p3.x) * (p3.y-p4.y)-(p1.y-p3.y) * (p3.x-p4.x ))
/(P1.x-p2.x) * (p3.y-p4.y)-(p1.y-p2.y) * (p3.x-p4.x ));
Ret. x + = (p2.x-p1.x) * t;
Ret. y + = (p2.y-p1.y) * t;
Return ret;
}
Double min (double a, double B)
{
Return a> B? B:;
}
Int main ()
{
While (1)
{
Int I;
For (I = 0; I <4; I ++)
Scanf ("% lf", & points [I]. x, & points [I]. y );
If (points [0]. x =-1 & points [1]. x =-1 & points [2]. x =-1 & points [3]. x =-1 &&
Points [0]. y =-1 & points [1]. y =-1 & points [2]. y =-1 & points [3]. y =-1) break;
Double res = 0;
Double minn = 1000000000;
For (I = 0; I <n; I ++)
{
Res = alldis (points [I]);
Minn = min (minn, res );
}
If (xmult (points [0], points [1], points [3]) * xmult (points [2], points [1], points [3]) <0)
{
Point ans = intersection (points [0], points [2], points [1], points [3]);
Res = alldis (ans );
Minn = min (minn, res );
}
If (xmult (points [1], points [2], points [3]) * xmult (points [0], points [2], points [3]) <0)
{
Point ans = intersection (points [1], points [0], points [2], points [3]);
Res = alldis (ans );
Minn = min (minn, res );
}
If (xmult (points [2], points [0], points [3]) * xmult (points [1], points [0], points [3]) <0)
{
Point ans = intersection (points [2], points [1], points [0], points [3]);
Res = alldis (ans); www.2cto.com
Minn = min (minn, res );
}
Printf ("%. 4lf \ n", minn );
}
Return 0;
}
Author: ssslpk