我正在写C程序来计算矩形的交点,但是我无法获得预期的输出。canocalise函数,max和min函数不起作用。请帮助我。
#include <stdio.h>
#include <stdlib.h>
//I've provided "min" and "max" functions in
//case they are useful to you
int min (int a, int b) {
if (a < b) {
return a;
}
return b;
}
int max (int a, int b) {
if (a > b) {
return a;
}
return b;
}
//Declare your rectangle structure here!
typedef struct re{
int x,y,width,height;
}rectangle;
rectangle canonicalize(rectangle r) {
//WRITE THIS FUNCTION
if(r.width<0)
{
r.width=-r.width;
r.x=r.x-r.width;
}
else if(r.height<0)
{
r.height=-r.height;
r.y=r.y-r.height;
}
return r;
}
rectangle intersection(rectangle r1, rectangle r2) {
//WRITE THIS FUNCTION
r1.x=max(r1.x,r2.x);
r1.y=max(r1.y,r2.y);
r1.width=min(r1.width,r2.width);
r1.height=min(r1.height,r2.height);
if(r1.x>r1.width || r1.y>r1.height)
{
}
return r1;
}
//You should not need to modify any code below this line
void printRectangle(rectangle r) {
r = canonicalize(r);
if (r.width == 0 && r.height == 0) {
printf("<empty>\n");
}
else {
printf("(%d,%d) to (%d,%d)\n", r.x,r.y,r.x + r.width,r.y + r.height);
}
}
int main (void) {
rectangle r1;
rectangle r2;
rectangle r3;
rectangle r4;
r1.x = 2;
r1.y = 3;
r1.width = 5;
r1.height = 6;
printf("r1 is ");
printRectangle(r1);
r2.x = 4;
r2.y = 5;
r2.width = -5;
r2.height = -7;
printf("r2 is ");
printRectangle(r2);
r3.x = -2;
r3.y = 7;
r3.width = 7;
r3.height = -10;
printf("r3 is ");
printRectangle(r3);
r4.x = 0;
r4.y = 7;
r4.width = -4;
r4.height = 2;
printf("r4 is ");
printRectangle(r4);
//test everything with r1
rectangle i = intersection(r1,r1);
printf("intersection(r1,r1): ");
printRectangle(i);
i = intersection(r1,r2);
printf("intersection(r1,r2): ");
printRectangle(i);
i = intersection(r1,r3);
printf("intersection(r1,r3): ");
printRectangle(i);
i = intersection(r1,r4);
printf("intersection(r1,r4): ");
printRectangle(i);
//test everything with r2
i = intersection(r2,r1);
printf("intersection(r2,r1): ");
printRectangle(i);
i = intersection(r2,r2);
printf("intersection(r2,r2): ");
printRectangle(i);
i = intersection(r2,r3);
printf("intersection(r2,r3): ");
printRectangle(i);
i = intersection(r2,r4);
printf("intersection(r2,r4): ");
printRectangle(i);
//test everything with r3
i = intersection(r3,r1);
printf("intersection(r3,r1): ");
printRectangle(i);
i = intersection(r3,r2);
printf("intersection(r3,r2): ");
printRectangle(i);
i = intersection(r3,r3);
printf("intersection(r3,r3): ");
printRectangle(i);
i = intersection(r3,r4);
printf("intersection(r3,r4): ");
printRectangle(i);
//test everything with r4
i = intersection(r4,r1);
printf("intersection(r4,r1): ");
printRectangle(i);
i = intersection(r4,r2);
printf("intersection(r4,r2): ");
printRectangle(i);
i = intersection(r4,r3);
printf("intersection(r4,r3): ");
printRectangle(i);
i = intersection(r4,r4);
printf("intersection(r4,r4): ");
printRectangle(i);
return EXIT_SUCCESS;
}
当前输出:
r1为(2,3)至(7,9)
r2为(-1,5)至(4,-2)
r3为(-2,-3)至(5,7)
r4为(-4,7)至(0,9)
交点(r1,r1):(2,3)至(7,9)
交点(r1,r2):(-1,5)至(4,-2)
交点(r1,r3):(2,-3)至(7,7)
交点(r1,r4):(-2,7)至(2,9)
交点(r2,r1):(-1,5)至(4,-2)
交点(r2,r2):(-1,5)至(4,-2)
交点(r2,r3):(-1,7)至(4,-3)
交点(r2,r4):(-1,7)至(4,0)
交点(r3,r1):(2,-3)至(7,7)
交点(r3,r2):(-1,7)至(4,-3)
交点(r3,r3):(-2,-3)至(5,7)
交点(r3,r4):(-4,7)至(0,-3)
交点(r4,r1):(-2,7)至(2,9)
交点(r4,r2):(-1,7)至(4,0)
交点(r4,r3):(-4,7)至(0,-3)
交点(r4,r4):(-4,7)至(0,9)
请注意,max,min和canocalise无法正常工作。
答案 0 :(得分:0)
不确定您尝试过在交点函数中执行的操作,因此假设它对您而言正确运行。
if(r.width<0)
{
r.width=-r.width;
r.x=r.x-r.width;
}
if(r.height<0) // to handle when height and width both are less than zero (r2)
{
r.height=-r.height;
r.y=r.y-r.height;
}