Rectangles

Input
Input The first line of input is 8 positive numbers which indicate the coordinates of four points that must be on each diagonal.The 8 numbers are x1,y1,x2,y2,x3,y3,x4,y4.That means the two points on the first rectangle are(x1,y1),(x2,y2);the other two points on the second rectangle are (x3,y3),(x4,y4).
 
Output
Output For each case output the area of their intersected part in a single line.accurate up to 2 decimal places.
 
Sample Input
1.00 1.00 3.00 3.00 2.00 2.00 4.00 4.00 5.00 5.00 13.00 13.00 4.00 4.00 12.50 12.50
 
Sample Output
1.00 56.25
 
 
 

#include<stdio.h>

float deal(float k,float xy[])
{
    if( xy[0] > xy[1] )
    {
        float t = xy[0];
        xy[0] = xy[1];
        xy[1] = t;
    }
    if( xy[2] > xy[3] )
    {
        float t = xy[2];
        xy[2] = xy[3];
        xy[3] = t;
    }
    if( xy[1] <= xy[3] )
    {
        if( xy[1] <= xy[2] )
        {
            return 0;
        }
        else
        {
            if( xy[0] > xy[2] )
            {
                return xy[1] - xy[0];
            }
            else
            {
                return xy[1] - xy[2];
            }
        }
    }
    else
    {
        if( xy[3] <= xy[0] )
        {
            return 0;
        }
        else
        {
            if( xy[2] >= xy[0] )
            {
                return xy[3] - xy[2];
            }
            else
            {
                return xy[3] - xy[0];
            }
        }
    }  
}


int main()
{
    float x[4],y[4];
    while(scanf("%f%f",&x[0],&y[0]) == 2 )
    {
        int i;
        for( i = 1; i < 4; i++ )
        {
            scanf( "%f%f", &x[i], &y[i] );
        }
        float len = deal( len, x );
        float wid = deal( wid, y );
        printf( "%.2f\n", len*wid );
    }
    return 0;
}
        

原文地址:https://www.cnblogs.com/zsj576637357/p/2290075.html