The question is given at any time by four vertices, and the distance from the smallest vertex to the four vertices is obtained.
If the quadrilateral is convex, The Fermat point is the diagonal point, otherwise it is the concave point.
The proof is very simple. Connect the point to be proved with other vertices and take another point to prove that the distance from this point to the four vertices is longer than the original one.
View code
# Include <stdio. h>
# Include <math. h>
# Include < String . H>
Const Double ESP = 1E- 8 ;
Struct Point {
Double X, Y;
} [ 10 ];
Struct Line
{
Double A, B, C;
};
Double Min ( Double A, Double B)
{
Return A <B? A: B;
}
Double DIS (point a, point B)
{
Return SQRT (B. x-a.x) * (B. x-a.x) + (B. y-a.y) * (B. y-a.y ));
}
Int Dblcmp ( Double D)
{
If (FABS (d) <ESP)
Return 0 ;
Return (D> 0 )? 1 :- 1 ;
}
Double Cross (point a, point B, point C)
{
Return (B. x-a.x) * (C. y-a.y)-(B. y-a.y) * (C. x-a.x );
}
Void Lineintersect (line L1, line L2, Double & X, Double & Y)
{
Double D = l1.a * l2. B-l2.a * l1. B;
X = (l2.c * l1. B-l1.c * l2. B)/d;
Y = (l2.a * l1.c-l1.a * l2.c)/d;
}
Void Lineform ( Double X1, Double Y1,Double X2, Double Y2, line & temp) // Ax + by + c = 0;
{
Temp. A = y2-y1;
Temp. B = x1-x2;
Temp. c = x2 * y1-x1 * Y2;
}
Double Dit [ 5 ];
Int Main ()
{
Int I, J;
Double Ans;
Point Fermat;
While ( 1 )
{
Bool Flag = False ;
For (I = 1 ; I <= 4 ; I ++)
{
Scanf ( " % Lf " , & A [I]. X, & A [I]. y );
If (A [I]. X! =- 1 | A [I]. y! =- 1 )
Flag = True ;
}
If (! Flag)
Break ;
Memset (DIT, 0 , Sizeof (DIT ));
For (I = 1 ; I <=4 ; I ++)
{
For (J = 1 ; J <= 4 ; J ++)
{
Dit [I] + = DIS (A [I], a [J]);
}
}
Line temp1, temp2;
Double FX, FY;
If (Cross ([ 1 ], [ 2 ], [3 ]) * Cross ([ 1 ], [ 2 ], [ 4 ]) < 0 )
{
Lineform ([ 1 ]. X, [ 1 ]. Y, [ 2 ]. X, [ 2 ]. Y, temp1 );
Lineform ([ 3 ]. X, [ 3 ]. Y, [4 ]. X, [ 4 ]. Y, temp2 );
Lineintersect (temp1, temp2, FX, FY );
}
Else If (Cross ([ 1 ], [ 3 ], [ 4 ]) * Cross ([ 1 ], [ 3 ], [ 2 ]) < 0 )
{
Lineform ([ 1 ]. X, [ 1 ]. Y, [ 3 ]. X, [ 3 ]. Y, temp1 );
Lineform ([ 2 ]. X, [ 2 ]. Y, [ 4 ]. X, [ 4 ]. Y, temp2 );
Lineintersect (temp1, temp2, FX, FY );
}
Else If (Cross ([1 ], [ 4 ], [ 3 ]) * Cross ([ 1 ], [ 4 ], [ 2 ]) < 0 )
{
Lineform ([ 1 ]. X, [ 1 ]. Y, [ 4 ]. X, [ 4 ]. Y, temp1 );
Lineform ([2 ]. X, [ 2 ]. Y, [ 3 ]. X, [ 3 ]. Y, temp2 );
Lineintersect (temp1, temp2, FX, FY );
}
Ans = 0 ;
Fermat. x = FX; Fermat. Y = FY;
For (I = 1 ; I <= 4 ; I ++)
{
Ans + = DIS (Fermat, a [I]);
}
Double Tans = min (DIT [ 1 ], Dit [ 2 ]), Min (DIT [ 3 ], Dit [ 4 ]);
Double Ret = min (TANS, ANS );
Printf ( " %. 4lf \ n " , RET );
}
Return 0 ;
}