几何HDU 3103

注意精度。微笑

Shoring Up the Levees

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 307    Accepted Submission(s): 98


Problem Description
The tiny country of Waterlogged is protected by a series of levees that form a quadrilateral as shown below:




The quadrilateral is defined by four vertices. The levees partition the country into four quadrants. Each quadrant is identified by a pair of vertices representing the outside edge of that quadrant. For example, Quadrant 1 shown below is defined by the points (x1, y1) and (x2, y2) .




It happens very often that the country of Waterlogged becomes flooded, and the levees need to be reinforced, but their country is poor and they have limited resources. They would like to be able to reinforce those levees that encompass the largest area first, then the next largest second, then the next largest third, and the smallest area fourth.

Help Waterlogged identify which quadrants are the largest, and the length of the levees around them.
 

Input
here will be several sets of input. Each set will consist of eight real numbers, on a single line. Those numbers will represent, in order:


X1 Y1 X2 Y2 X3 Y3 X4 Y4


The four points are guaranteed to form a convex quadrilateral when taken in order -- that is, there will be no concavities, and no lines crossing. Every number will be in the range from -1000.0 to 1000.0 inclusive. No Quadrant will have an area or a perimeter smaller than 0.001. End of the input will be a line with eight 0.0's.
 

Output
For each input set, print a single line with eight floating point numbers. These represent the areas and perimeters of the four Quadrants, like this:


A1 P1 A2 P2 A3 P3 A4 P4


Print them in order from largest area to smallest -- so A1 is the largest area. If two Quadrants have the same area when rounded to 3 decimal places, output the one with the largest perimeter first. Print all values with 3 decimal places of precision (rounded). Print spaces between numbers. Do not print any blank lines between outputs.
 

Sample Input
  
  
1 2 1 5 5 2 2 0 3.5 2.2 4.8 -9.6 -1.2 -4.4 -8.9 12.4 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
 

Sample Output
  
  
5.100 11.459 3.400 9.045 0.900 6.659 0.600 4.876 44.548 38.972 21.982 25.997 20.342 38.374 10.038 19.043
 

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm>

using namespace std;
#define eps 1e-9

struct node
{
    double x,y;
};

struct ANSWER
{
    double area;
    double perimeter;
};
//线段的长度
double Distance(node u,node v)
{
    return sqrt( ( u.x - v.x ) *( u.x - v.x ) + ( u.y - v.y ) * ( u.y - v.y ) );
}
//两直线的交点
node intersection(node u1,node u2,node v1,node v2)
{
    node ret = u1;

    double t=((u1.x-v1.x)*(v1.y-v2.y)-(u1.y-v1.y)*(v1.x-v2.x))
             /((u1.x-u2.x)*(v1.y-v2.y)-(u1.y-u2.y)*(v1.x-v2.x));
    ret.x+=(u2.x-u1.x)*t;
    ret.y+=(u2.y-u1.y)*t;
    return ret;
}

//点到直线的最近距离
node ptoline(node p,node l1,node l2)
{
    node t=p;
    t.x+=l1.y-l2.y,t.y+=l2.x-l1.x;
    return intersection(p,t,l1,l2);
}

bool cmp(ANSWER a, ANSWER b)
{
    if(a.area == b.area)
        return a.perimeter >= b.perimeter;
    return a.area > b.area;
}

int main()
{
    node a,b,c,d;

    while(1)
    {
        scanf("%lf%lf",&a.x,&a.y);
        scanf("%lf%lf",&b.x,&b.y);
        scanf("%lf%lf",&c.x,&c.y);
        scanf("%lf%lf",&d.x,&d.y);

        if(a.x == 0.0 && a.y == 0 && b.x == 0 && b.y == 0 && c.x == 0 && c.y == 0 && d.x == 0 && d.y == 0)
            break;

        ANSWER ans[4];
        memset(ans,0,sizeof(ans));

        double t,tmp;

        node cnt1 = intersection(a,c,b,d);
        node cnt2;
        double l1,l2;

        cnt2 = ptoline(cnt1,a,b);
        l1 = Distance(cnt1,cnt2);
        l2 = Distance(a,b);
        ans[0].area = l1*l2/2.0;
        ans[0].perimeter = l2 + Distance(cnt1,a) + Distance(cnt1,b);

        cnt2 = ptoline(cnt1,b,c);
        l1 = Distance(cnt1,cnt2);
        l2 = Distance(b,c);
        ans[1].area = l1*l2/2.0;
        ans[1].perimeter = l2 + Distance(cnt1,c) + Distance(cnt1,b);

        cnt2 = ptoline(cnt1,c,d);
        l1 = Distance(cnt1,cnt2);
        l2 = Distance(c,d);
        ans[2].area = l1*l2/2.0;
        ans[2].perimeter = l2 + Distance(cnt1,c) + Distance(cnt1,d);

        cnt2 = ptoline(cnt1,a,d);
        l1 = Distance(cnt1,cnt2);
        l2 = Distance(a,d);
        ans[3].area = l1*l2/2.0;
        ans[3].perimeter = l2 + Distance(cnt1,a) + Distance(cnt1,d);

        for(int i=0;i<4;i++)
        {
            int x = (int)((ans[i].area +0.0005)* 1000);
            ans[i].area = x*1.0/1000.0;
        }

        sort(ans,ans+4,cmp);

        for(int i = 0; i<3;i++)
        {
            printf("%.3lf %.3lf",ans[i].area,ans[i].perimeter);
            printf(" ");
        }
        printf("%.3lf %.3lf\n",ans[3].area,ans[3].perimeter);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值