HDU 3694 Fermat Point in Quadrangle (计算几何- 四边形的费马点)

Fermat Point in Quadrangle


Problem Description
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
 

Source
 

Recommend
axubiao


解题思路:对于凸四边形,费马点一定在对角线的连线上,对于凹多边形,费马点在那个凹点上,证明的话,画张图很好证明。



View Code

Problem : 3694 ( Fermat Point in Quadrangle )     Judge Status : Accepted
RunId : 9336690    Language : C++    Author : a1061747415
Code Render Status : Rendered By HDOJ C++ Code Render Version 0.01 Beta
#include <iostream>
#include <cmath>
#include <cstdio>
#include <algorithm>
using namespace std;

struct point{
    double x,y;
    friend bool operator < (point p1,point p2){
        if(p1.y!=p2.y) return p1.y<p2.y;
        else return p1.x<p2.x;
    }
    double getdis(point p0){
        return sqrt((x-p0.x)*(x-p0.x)+(y-p0.y)*(y-p0.y));
    }
    friend istream& operator >> (istream &input,point &p0){
        input>>p0.x>>p0.y;
        return input;
    }
};
point p[10],pp[10];
int n=4,top;

double getans(int k){
    double dis=0;
    for(int i=0;i<4;i++){
        dis+=pp[i].getdis(pp[k]);
        //cout<<p[i].x<<" "<<p[i].y<<" "<<p[k].x<<" "<<p[k].y<<" "<<dis<<endl;
    }
    return dis;
}

double xchen(point a,point b,point c){
    return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
}

double dchen(point a,point b,point c){
    return (b.x-a.x)*(c.x-a.x)+(c.y-a.y)*(b.y-a.y);
}

bool cmp(point a,point b){
    if(fabs(xchen(p[0],a,b))<1e-7) return a.getdis(p[0])<b.getdis(p[0]);
    else return xchen(p[0],a,b)>0;
}

bool deal(){
    top=1;
    sort(p,p+n);
    sort(p+1,p+n,cmp);
    for(int i=2;i<n;i++){
        while(top>0 && xchen(p[top-1],p[top],p[i])<=0) top--;
        p[++top]=p[i];
    }
    if(top>=3) return true;
    else return false; 
}

void computing(){
    //for(int i=0;i<4;i++) cout<<p[i].x<<" "<<p[i].y<<endl;
    double ans=1e9,tmp;
    if(deal()){
        tmp=p[0].getdis(p[2])+p[1].getdis(p[3]);
        if(tmp<ans) ans=tmp;
    }else{
        for(int i=0;i<4;i++){
            tmp=getans(i);
            if(tmp<ans) ans=tmp;
        }
    }
    printf("%.4f\n",ans);
}

int main(){
    while(cin>>p[0]>>p[1]>>p[2]>>p[3]){
        bool flag=true;
        for(int i=0;i<4;i++){
            pp[i]=p[i];
            if(fabs(p[i].x+1)>1e-9) flag=false;
            if(fabs(p[i].y+1)>1e-9) flag=false;
        }
        if(flag) break;
        computing();
    }
    return 0;
}



转载于:https://www.cnblogs.com/toyking/p/3797413.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值