| 网站首页 | 业界新闻 | 小组 | 交易 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
 买学问 - 大牛一对一辅导，有问必答 买学问 - 专业的付费知识问答平台

已结贴   问题点数：100  回复次数：8
[没人气，抄道题] 矩形面积
```Find the total area covered by two rectilinear rectangles in a 2D plane.
Each rectangle is defined by its bottom left corner and top right corner as shown in the figure.
Assume that the total area is never beyond the maximum possible value of int.```

int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
// 代码
}

得分:2

得分:2

得分:2

得分:50

```#include <stdio.h>

#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))

int computeArea(int A, int B, int C, int D, int E, int F, int G, int H)
{
int left   = MAX(A, E);
int bottom = MAX(B, F);
int right  = MIN(C, G);
int top    = MIN(D, H);
return (C - A) * (D - B) + (G - E) * (H - F) - MAX(right - left, 0) * MAX(top - bottom, 0);
}

int main(void)
{
printf("%d\n", computeArea(-3, 0, 3, 4, 0, -1, 9, 2));
return 0;
}```

[ 本帖最后由 kenierlee 于 2015-8-7 11:43 编辑 ]
得分:2

得分:2

得分:20

```#include<stdio.h>
#include<math.h>
int computeArea(int a,int b,int c,int d,int e,int f,int g,int h)
{
int area,s1,s2,s3;
s1=(c-a)*(d-b);
s2=(g-e)*(h-f);
int max1,max2;
max1=(g>c?g:c)-(a<e?a:e);
max2=(h>d?h:d)-(f<b?f:b);
if(c-a+g-e>max1&&d-b+h-f>max2)//两矩形相交
{
s3=(c-a+g-e-max1)*(d-b+h-f-max2);//相交矩形面积
area=s1+s2-s3;
}
else
area=s1+s2;
return area;
}
int main()
{
int a,b,c,d,e,f,g,h;
int area;
scanf("%d%d%d%d%d%d%d%d",&a,&b,&c,&d,&e,&f,&g,&h);
area=computeArea(a,b,c,d,e,f,g,h);
printf("%d",area);
return 0;
}```

得分:20

```#include<stdio.h>

int abss(int a, int b){
if(a > b){
return a - b;
}
return b - a;
}

double computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
double totalArea, area1, area2;
area1 = (C - A) * (D - B);
area2 = (G - E) * (H - F);
printf("area1 = %f\narea2 = %f\n", area1, area2);
totalArea = area1 + area2;
//根据重心位置和长宽判断是否相交
int center_x = abss(A + C, E + G),
center_y = abss(B + D, F + H),
length_x = C - A + G - E,
length_y = D - B + H - F;
if(center_x < length_x && center_y < length_y){
double cross = (length_x - center_x) * (length_y - center_y) / 4;
printf("cross = %f\n", cross);
totalArea -= cross;
}
}

int main(){
printf("area = %f", computeArea(-3,0,3,4,0,-1,9,2));
return 0;
}```

• 9
• 1/1页
• 1