In geometry the Fermat point of a triangle, also called Torricelli point, is a point such that the total distance from the three vertices of the triangle to the point is the minimum. It is so named because this problem is first raised by Fermat in a private letter. In the following picture, P 0 is the Fermat point. You may have already known the property that:
Alice and Bob are learning geometry. Recently they are studying about the Fermat Point.
Alice: I wonder whether there is a similar point for quadrangle.
Bob: I think there must exist one.
Alice: Then how to know where it is? How to prove?
Bob: I don’t know. Wait… the point may hold the similar property as the case in triangle.
Alice: It sounds reasonable. Why not use our computer to solve the problem? Find the Fermat point, and then verify your assumption.
Bob: A good idea.
So they ask you, the best programmer, to solve it. Find the Fermat point for a quadrangle, i.e. find a point such that the total distance from the four vertices of the quadrangle to that point is the minimum.
Input
The input contains no more than 1000 test cases.
Each test case is a single line which contains eight float numbers, and it is formatted as below:
x 1 y 1 x 2 y 2 x 3 y 3 x 4 y 4
x i, y i are the x- and y-coordinates of the ith vertices of a quadrangle. They are float numbers and satisfy 0 ≤ x i ≤ 1000 and 0 ≤ y i ≤ 1000 (i = 1, …, 4).
The input is ended by eight -1.
Output
For each test case, find the Fermat point, and output the total distance from the four vertices to that point. The result should be rounded to four digits after the decimal point.
Sample Input
0 0 1 1 1 0 0 1
1 1 1 1 1 1 1 1
-1 -1 -1 -1 -1 -1 -1 -1
Sample Output
2.8284
0.0000
四边形费马点:到四边形四个顶点的距离之和最小的点
(1)在凸四边形中,费马点为两对角线交点。
(2)在凹四边形中,费马点为凹顶点。
计算几何不会,玄学(模拟退火)试试
<mark>模拟退火注意精度</mark>
#include<cstdio>
#include<cmath>
using namespace std;
const double eps=1e-8;
struct point{
double x,y;
point(){};
point(double xx,double yy){
x=xx;
y=yy;
}
}p[10],now,temp;
double dis(point a,point b){
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int t[4][2]={0,1,0,-1,1,0,-1,0};
double f(point a){
double sum=0;
for(int i=0;i<4;i++){
sum+=dis(a,p[i]);
}
return sum;
}
int main(){
while(scanf("%lf%lf",&p[0].x,&p[0].y)){
for(int i=1;i<4;i++)
scanf("%lf%lf",&p[i].x,&p[i].y);
if(p[0].x==-1) break;
now=point(0,0);
double step=100,ans=f(now);
while(step>eps){
bool flag=true;
while(flag){
flag=false;
for(int i=0;i<4;i++){
temp=point(now.x+t[i][0]*step,now.y+t[i][1]*step);
double cnt=f(temp);
if(cnt<ans){
ans=cnt;
now=temp;
flag=true;
}
}
}
step=step/2.0;
}
printf("%.4lf\n",ans);
}
return 0;
}