程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> Ä£°å ÇóËıßÐεķÑÂíµã

Ä£°å ÇóËıßÐεķÑÂíµã

編輯:C++入門知識

Ä£°å ÇóËıßÐεķÑÂíµã


Fermat Point in Quadrangle

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2039 Accepted Submission(s): 375


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, P0 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 fZ†·Ÿ"http://www.Bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vciBhIHF1YWRyYW5nbGUsIGkuZS4gZmluZCBhIHBvaW50IHN1Y2ggdGhhdCB0aGUgdG90YWwgZGlzdGFuY2UgZnJvbSB0aGUgZm91ciB2ZXJ0aWNlcyBvZiB0aGUgcXVhZHJhbmdsZSB0byB0aGF0IHBvaW50IGlzIHRoZSBtaW5pbXVtLgogCjxicj4KSW5wdXQKVGhlIGlucHV0IGNvbnRhaW5zIG5vIG1vcmUgdGhhbiAxMDAwIHRlc3QgY2FzZXMuPGJyPgo8YnI+CkVhY2ggdGVzdCBjYXNlIGlzIGEgc2luZ2xlIGxpbmUgd2hpY2ggY29udGFpbnMgZWlnaHQgZmxvYXQgbnVtYmVycywgYW5kIGl0IGlzIGZvcm1hdHRlZCBhcyBiZWxvdzo8YnI+Cjxicj4KeDxzdWI+MTwvc3ViPiB5PHN1Yj4xPC9zdWI+IHg8c3ViPjI8L3N1Yj4geTxzdWI+Mjwvc3ViPiB4PHN1Yj4zPC9zdWI+IHk8c3ViPjM8L3N1Yj4geDxzdWI+NDwvc3ViPiB5PHN1Yj40PC9zdWI+PGJyPgo8YnI+Cng8c3ViPmk8L3N1Yj4sIHk8c3ViPmk8L3N1Yj4gYXJlIHRoZSB4LSBhbmQgeS1jb29yZGluYXRlcyBvZiB0aGUgaXRoIHZlcnRpY2VzIG9mIGEgcXVhZHJhbmdsZS4gVGhleSBhcmUgZmxvYXQgbnVtYmVycyBhbmQgc2F0aXNmeSAwIKHcIHg8c3ViPmk8L3N1Yj4godwgMTAwMCBhbmQgMCCh3CB5PHN1Yj5pPC9zdWI+IKHcIDEwMDAgKGkgPSAxLCChrSwgNCkuPGJyPgo8YnI+ClRoZSBpbnB1dCBpcyBlbmRlZCBieSBlaWdodCAtMS4KIAo8YnI+Ck91dHB1dApGb3IgZWFjaCB0ZXN0IGNhc2UsIGZpbmQgdGhlIEZlcm1hdCBwb2ludCwgYW5kIG91dHB1dCB0aGUgdG90YWwgZGlzdGFuY2UgZnJvbSB0aGUgZm91ciB2ZXJ0aWNlcyB0byB0aGF0IHBvaW50LiBUaGUgcmVzdWx0IHNob3VsZCBiZSByb3VuZGVkIHRvIGZvdXIgZGlnaXRzIGFmdGVyIHRoZSBkZWNpbWFsIHBvaW50LgogCjxicj4KU2FtcGxlIElucHV0Cgo8cHJlIGNsYXNzPQ=="brush:java;">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
///·ÑÂíµã
#include
#include
#include
const double esp=1e-8;
struct point
{
    double x,y;
} a[10];
struct line
{
    double a,b,c;
};
double min(double a,double b)
{
    return a0)?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%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(a[1],a[2],a[3])*cross(a[1],a[2],a[4])<0)
        {
            lineform(a[1].x,a[1].y,a[2].x,a[2].y,temp1);
            lineform(a[3].x,a[3].y,a[4].x,a[4].y,temp2);
            lineintersect(temp1,temp2,fx,fy);
        }
        else if(cross(a[1],a[3],a[4])*cross(a[1],a[3],a[2])<0)
        {
            lineform(a[1].x,a[1].y,a[3].x,a[3].y,temp1);
            lineform(a[2].x,a[2].y,a[4].x,a[4].y,temp2);
            lineintersect(temp1,temp2,fx,fy);
        }
        else if(cross(a[1],a[4],a[3])*cross(a[1],a[4],a[2])<0)
        {
            lineform(a[1].x,a[1].y,a[4].x,a[4].y,temp1);
            lineform(a[2].x,a[2].y,a[3].x,a[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(min(dit[1],dit[2]),min(dit[3],dit[4]));
        double ret=min(tans,ans);
        printf("%.4lf\n",ret);
    }
    return 0;
}


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved