是否需要多个条件(如多个 if else 语句)才能正确打印相交矩形?
第三步:如果两个矩形有公共(public)区域,则它们相交如果两个矩形只是接触(公共(public)边或公共(public)角),则它们不重叠
两个矩形相交(如上所述)当且仅当,
i) max(xmin1, xmin2) < min(xmax1, xmax2) 和
ii) max(ymin1, ymin2) < min(ymax1, ymax2)
您的输出将被格式化。如下图所示,矩形的左下坐标(xmin,ymin)和右上角坐标(xmax,ymax)。其中坐标是笛卡尔平面中的坐标。
示例输出:
enter two rectangles:
1 1 4 4
2 2 5 5
rectangle 1: (1,1)(4,4)
rectangle 2: (2,2)(5,5)
intersection rectangle: (2,2)(4,4)
和
enter two rectangles:
1 1 4 4
5 5 10 10
rectangle 1: (1,1)(4,4)
rectangle 2: (5,5)(10,10)
these two rectangles do not intersect
代码:
#include <stdio.h>
#include <stdlib.h>
int readRect (int *w, int *x, int *y, int *z){
return scanf("%d%d%d%d",w,x,y,z);
}
int minInt(int x1, int x2){
return x1, x2;
}
int maxInt(int y1, int y2){
return y1, y2;
}
int main (void){
int a,b,c,d,e,f,g,h;
printf(">>enter two rectangles:\n");
readRect(&a,&b,&c,&d);
readRect(&e,&f,&g,&h);
printf("rectangle 1:(%d,%d)(%d,%d)\n",a,b,c,d);
printf("rectangle 2:(%d,%d)(%d,%d)\n",e,f,g,h);
if(maxInt(a,e) < minInt(c,g) && maxInt(b,f) < minInt(d,g)){
printf("intersection rectangle: (%d,%d)(%d,%d)\n",?,?,?,?);
}
else {
printf("these rectangles do not intersect\n");
}
return EXIT_SUCCESS;
}
最佳答案
第 1 步 - 罪魁祸首是 scanf 中的“\n”。如果你删除它就会起作用 如果您在第 2 步或第 3 步中需要任何具体帮助,请告诉我。
关于c - 这个程序用C怎么实现呢?第3.2-3.9部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19352614/