hdu 3694 10 福州 现场 E - Fermat Point in Quadrangle

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:

1 y 1 x 2 y 2 x 3 y 3 x 4 y 4

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

取四个点其中一个点或者四个点两两连线的交点

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;

const double eps=1e-10;

double add(double a,double b)
{
	if(abs(a+b)<eps*(abs(a)+abs(b))) return 0;
	return a+b;
}

struct point
{
    double x,y;
    point () {}
    point (double x,double y) : x(x),y(y){ }
    point operator + (point p)
    {
    	return point (add(x,p.x),add(y,p.y));
    }
    point operator - (point p)
    {
    	return point (add(x,-p.x),add(y,-p.y));
    }
    point operator * (double d)
    {
    	return point (x*d,y*d);
    }
    double dot(point p)
    {
    	return add(x*p.x,y*p.y);
    }
    double det(point p)
    {
    	return add(x*p.y,-y*p.x);
    }
};

bool on_seg(point p1,point p2,point q)
{
	return (p1-q).det(p2-q)==0&&(p1-q).dot(p2-q)<=0;
}

point intersection(point p1,point p2,point q1,point q2)
{
	return p1+(p2-p1)*((q2-q1).det(q1-p1)/(q2-q1).det(p2-p1));
}

bool cmp_x(const point&p,const point& q)
{
	if(p.x!=q.x) return p.x<q.x;
	return p.y<q.y;
}

vector<point> convex_hull(point*ps,int n)
{
	sort(ps,ps+n,cmp_x);
	//for(int i=0;i<n;i++) printf("x=%.f %.f")
	int k=0;
	vector<point> qs(n*2);
	for(int i=0;i<n;i++){
		while(k>1&&(qs[k-1]-qs[k-2]).det(ps[i]-qs[k-1])<=0) k--;
		qs[k++]=ps[i];
	}
	for(int i=n-2,t=k;i>=0;i--){
		while(k>t&&(qs[k-1]-qs[k-2]).det(ps[i]-qs[k-1])<=0) k--;
		qs[k++]=ps[i];
	}
	qs.resize(k-1);
	return qs;
}

double dis(point p1,point p2)
{
    return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
bool equ(point p1,point p2)
{
    if(fabs(p1.x-p2.x)<eps&&fabs(p1.y-p2.y)<eps)
        return true;
    return false;
}
int main()
{
    point p[10];
    for(int i=0;i<4;i++)
        scanf("%lf%lf",&p[i].x,&p[i].y);
    while(p[0].x!=-1&&p[0].y!=-1)
    {
        vector <point> m;
        double minn=100000000,d;
        m=convex_hull(p,4);
        if(m.size()==4)
            minn=dis(m[1],m[3])+dis(m[0],m[2]);
        for(int i=0;i<4;i++)
        {
            d=0;
            for(int j=0;j<4;j++)
                d+=dis(p[i],p[j]);
            minn=min(minn,d);
        }
        printf("%.4f\n",minn);
        for(int i=0;i<4;i++)
            scanf("%lf%lf",&p[i].x,&p[i].y);
    }
    return 0;
}

  

时间: 2024-10-08 20:36:04

hdu 3694 10 福州 现场 E - Fermat Point in Quadrangle的相关文章

hdu 3695 10 福州 现场 F - Computer Virus on Planet Pandora 暴力 ac自动机

F - Computer Virus on Planet Pandora Time Limit:2000MS     Memory Limit:128000KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 3695 Appoint description:  System Crawler  (2014-11-05) Description Aliens on planet Pandora also write comp

hdu 3699 10 福州 现场 J - A hard Aoshu Problem

Description Math Olympiad is called “Aoshu” in China. Aoshu is very popular in elementary schools. Nowadays, Aoshu is getting more and more difficult. Here is a classic Aoshu problem: ABBDE __ ABCCC = BDBDE In the equation above, a letter stands for

hdu 3697 10 福州 现场 H - Selecting courses

Description A new Semester is coming and students are troubling for selecting courses. Students select their course on the web course system. There are n courses, the ith course is available during the time interval (A i,B i). That means, if you want

hdu 3696 10 福州 现场 G - Farm Game

Description “Farm Game” is one of the most popular games in online community. In the community each player has a virtual farm. The farmer can decide to plant some kinds of crops like wheat or paddy, and buy the corresponding crop seeds. After they gr

hdu 3682 10 杭州 现场 C - To Be an Dream Architect 简单容斥

C - To Be an Dream Architect Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 3682 Appoint description:  System Crawler  (2014-11-09) Description The “dream architect” is the key role in a team o

hdu 3685 10 杭州 现场 F - Rotational Painting 重心

F - Rotational Painting Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 3685 Appoint description:  System Crawler  (2014-11-09) Description Josh Lyman is a gifted painter. One of his great works

hdu 3687 10 杭州 现场 H - National Day Parade 暴力水题

H - National Day Parade Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 3687 Appoint description:  System Crawler  (2014-11-08) Description There are n×n students preparing for the National Day

HDU 3694 Fermat Point in Quadrangle (费马定理求四边形的费马点)

题意:给你四个点,找出一个点到四个点的距离最小 四边形的费马点:凸边形是两对角线的交点,凹边形式凹点. PS: 三角形的费马点: 1.若三角形3个内角均小于120°,那么3条距离连线正好三等分费马点所在的周角,即该点所对三角形三边的张角相等,均为120°.所以三角形的费马点也称为三角形的等角中心. 2.若三角形有一内角大于等于120°,则此钝角的顶点就是距离和最小的点. #include<stdio.h> #include<string.h> #include<stdlib.

HDU 4815 2013长春现场赛C题

C - Little Tiger vs. Deep Monkey Time Limit:1000MS     Memory Limit:65535KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 4815 Description A crowd of little animals is visiting a mysterious laboratory ? The Deep Lab of SYSU. "Are y