- Apr 25, 2004
- 6,652
- 734
- 126
Alright so heres my dilemma. I'm taking a C programming class for my ME curriculum and I'm having an issue with my current assignment.
My teacher expects us to write a C program that will take two sets of points for 3D rectangles and find the intersecting rectangle that results. It's far more complex than that as I have to validate things as I go.
I'm running into a problem deciding how to find the intersecting rectangle though. My function basically looks at the front lower left corner (fll) and back upper right corner(bur). It compares the values for the two rectangles to see if one rectangle has a point that falls within that range.
My code looks at the x, y, and z values independently and requires that all 3 values fall between one or the other rectangle. I use <= and >= operands though to incorporate overlaps but this causes the function to fail when a 2nd rectangle is passed that is touching the 1st rectangle.
Can anyone think of a set of code that will do this for me? I could probably redo it but it would turn into a huge amount of code if I had to define things so specifically.
int intersects(Rect3D rect1, Rect3D rect2)
{
/*
* We will check to see if the two rectangles that have been passed to us
* intersect. If they do return 1 for true and if not return 0 for false.
*/
if (((rect1.fll.x >= rect2.fll.x && rect1.fll.x <= rect2.bur.x) || (rect2.fll.x >= rect1.fll.x && rect2.fll.x <= rect1.bur.x)) && ((rect1.fll.y >= rect2.fll.y && rect1.fll.y <= rect2.bur.y) || (rect2.fll.y >= rect1.fll.y && rect2.fll.y <= rect1.bur.y)) && ((rect1.fll.z >= rect2.fll.z && rect1.fll.z <= rect2.bur.z) || (rect2.fll.z >= rect1.fll.z && rect2.fll.z <= rect1.bur.z)))
{
return(1);
}
else
{
return(0);
}
}
My god this code box sucks balls.
My teacher expects us to write a C program that will take two sets of points for 3D rectangles and find the intersecting rectangle that results. It's far more complex than that as I have to validate things as I go.
I'm running into a problem deciding how to find the intersecting rectangle though. My function basically looks at the front lower left corner (fll) and back upper right corner(bur). It compares the values for the two rectangles to see if one rectangle has a point that falls within that range.
My code looks at the x, y, and z values independently and requires that all 3 values fall between one or the other rectangle. I use <= and >= operands though to incorporate overlaps but this causes the function to fail when a 2nd rectangle is passed that is touching the 1st rectangle.
Can anyone think of a set of code that will do this for me? I could probably redo it but it would turn into a huge amount of code if I had to define things so specifically.
int intersects(Rect3D rect1, Rect3D rect2)
{
/*
* We will check to see if the two rectangles that have been passed to us
* intersect. If they do return 1 for true and if not return 0 for false.
*/
if (((rect1.fll.x >= rect2.fll.x && rect1.fll.x <= rect2.bur.x) || (rect2.fll.x >= rect1.fll.x && rect2.fll.x <= rect1.bur.x)) && ((rect1.fll.y >= rect2.fll.y && rect1.fll.y <= rect2.bur.y) || (rect2.fll.y >= rect1.fll.y && rect2.fll.y <= rect1.bur.y)) && ((rect1.fll.z >= rect2.fll.z && rect1.fll.z <= rect2.bur.z) || (rect2.fll.z >= rect1.fll.z && rect2.fll.z <= rect1.bur.z)))
{
return(1);
}
else
{
return(0);
}
}
My god this code box sucks balls.