首页 > ACM题库 > HDU-杭电 > hdu 2056 Rectangles-计算几何-[解题报告]C++
2013
12-26

hdu 2056 Rectangles-计算几何-[解题报告]C++

Rectangles

问题描述 :

Given two rectangles and the coordinates of two points on the diagonals of each rectangle,you have to calculate the area of the intersected part of two rectangles. its sides are parallel to OX and OY .

输入:

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).

输出:

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).

样例输入:

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

样例输出:

1.00
56.25

题解:

  简单数学题,但是细节就太重要了,注意面积为零的情况,最最该注意的是题中给出的对角线上的点是主对角线或副对角线上的,我就是因为一直认为它给出的都是主对角线上的点错了好多次。基本步骤是先将给出的点转换成主对角线上的点,即用主对角线的两个点表示矩形,再确定重叠部分(假设存在)矩形的主对角线的两个点坐(x1,y1)(x2,y2)如果x2-x1<0 或 y2-y1<0,那么假设不成了,重叠部分为零(输出时应输出0.00)。

代码如下:

#include <stdio.h>

void change(double *x1, double *y1, double *x2, double *y2)
{//将题中给出的矩形,用主对角线的顶点表示
	double t;
	if (*x1 > *x2)
	{
		t = *x1;
		*x1 = *x2;
		*x2 = t;
	}
	if (*y1 > *y2)
	{
		t = *y1;
		*y1 = *y2;
		*y2 = t;
	}
}

int main()
{
	double A[8], x1, y1, x2, y2;
	while(scanf("%lf%lf%lf%lf%lf%lf%lf%lf",	&A[0], &A[1], &A[2], &A[3],	&A[4], &A[5], &A[6], &A[7]) != EOF)
	{
		change (&A[0], &A[1], &A[2], &A[3]);
		change (&A[4], &A[5], &A[6], &A[7]);
		x1 = A[0] > A[4] ? A[0] : A[4];//
		y1 = A[1] > A[5] ? A[1] : A[5];//
		x2 = A[2] < A[6] ? A[2] : A[6];//
		y2 = A[3] < A[7] ? A[3] : A[7];//求出重叠部分(假定重叠)的主对角线顶点
		if( (x2 - x1) < 0 || (y2 - y1) < 0)
			printf("0.00\n");
		else
			printf("%.2lf\n", (x2-x1)*(y2-y1));
	}
	return 0;
}

 

解题转自:http://blog.csdn.net/suifengdream/article/details/8537224


  1. Good task for the group. Hold it up for every yeara??s winner. This is a excellent oppotunity for a lot more enhancement. Indeed, obtaining far better and much better is constantly the crucial. Just like my pal suggests on the truth about ab muscles, he just keeps obtaining much better.