deadlyapp

Diamond Member
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.
 

deadlyapp

Diamond Member
Apr 25, 2004
6,652
734
126
Well I figured out what would make it work and had to add some code to make it work for me. Messy but at this point I don't care.
 

Gamingphreek

Lifer
Mar 31, 2003
11,679
0
81
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)))

I'm glad you found your answer; however, if I was an employer or a professor looking at your code, I would drop you to a D if you ever wrote compound if statement like that.

-Kevin
 

deadlyapp

Diamond Member
Apr 25, 2004
6,652
734
126
Originally posted by: Gamingphreek
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)))

I'm glad you found your answer; however, if I was an employer or a professor looking at your code, I would drop you to a D if you ever wrote compound if statement like that.

-Kevin

Do you have any constructive tips then? I'm relatively limited on what I know at this point. We have only done if/else statements and just recently looped statements but I don't see how a loop would be helpful at all.

I could have broken it down into multiple steps but my prototypes have to stay the same.
 

Atheus

Diamond Member
Jun 7, 2005
7,313
2
0
Originally posted by: deadlyapp
Originally posted by: Gamingphreek
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)))

I'm glad you found your answer; however, if I was an employer or a professor looking at your code, I would drop you to a D if you ever wrote compound if statement like that.

-Kevin

Do you have any constructive tips then? I'm relatively limited on what I know at this point. We have only done if/else statements and just recently looped statements but I don't see how a loop would be helpful at all.

I could have broken it down into multiple steps but my prototypes have to stay the same.

If you only just learned what a loop is, your code is just fine, or impressive even. Your goal at this point is to write statements which WORK, not ones which look nice and fit on a post-it note.
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
Originally posted by: Atheus
Originally posted by: deadlyapp
Originally posted by: Gamingphreek
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)))

I'm glad you found your answer; however, if I was an employer or a professor looking at your code, I would drop you to a D if you ever wrote compound if statement like that.

-Kevin

Do you have any constructive tips then? I'm relatively limited on what I know at this point. We have only done if/else statements and just recently looped statements but I don't see how a loop would be helpful at all.

I could have broken it down into multiple steps but my prototypes have to stay the same.

If you only just learned what a loop is, your code is just fine, or impressive even. Your goal at this point is to write statements which WORK, not ones which look nice and fit on a post-it note.

Honestly the only thing I would change would be the formatting of the if statement, but Fusetalk likes to chew on whitespace here so attempting to format it better would be pointless.

 

squatchman

Member
Apr 1, 2009
50
0
0
Seems like the point of the assignment is more of an exercise in translating math to software than to write some monolithic conditional. Trying to solve the problem without using math works, but not in an elegant way.

Test it extensively if you're going to go with this, and expect that the person with the grading pen will find a condition that causes a failure.
 

Gamingphreek

Lifer
Mar 31, 2003
11,679
0
81
Originally posted by: deadlyapp
Originally posted by: Gamingphreek
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)))

I'm glad you found your answer; however, if I was an employer or a professor looking at your code, I would drop you to a D if you ever wrote compound if statement like that.

-Kevin

Do you have any constructive tips then? I'm relatively limited on what I know at this point. We have only done if/else statements and just recently looped statements but I don't see how a loop would be helpful at all.

I could have broken it down into multiple steps but my prototypes have to stay the same.

Sure! I'd be more than happy to help

I'll assume that you haven't done any compiler theory or anything, so I'll try and give you some insight to where I am coming from.

Consider this 'if' statement:
if( rect1.fll.x >= rect2.fll.x )
{
// Code
}

When you compile this code, you get something like this:
...
cmpl %eax, %edx
jge .L51
...

Pretty simple code (eax and edx are registers). I assume both of those values are in registers, compare them, and then jump to another portion of code if the value in the first register is greater than or equal to the second.

Now consider this:
if()
{
if()
{
if()
{
}
}
}

and this:
if( a>b || a=b || x>a || ...)
{
}

Both of these will compile to the exact same assembly code (Something similar to - the memory addresses and registers are arbitrary):
...
cmpl %eax, %edx
jg .L51
cmpl 0xbee, 0xcee
je .L51
...

The difference is that one is much more readable than the other.
In short, I am suggesting that your code does something like this:
if( ( rect1.fll.x >= rect2.fill.x && rect1.fll.x <=rect2.bur.x ) || ( rect2.fll.x >= rect1.fll.x && rect2.fll.x <= rect1.bur.x ) )
{
if( ( rect1.fll.y >= rect2.fll.y && rect1.fll.y <= rect2.bur.y ) || ( rect2.fll.y >= rect1.fll.y && rect2.fll.y <= rect1.bur.y ) )
{
if( ( 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;
}
}
}
return 0;

-Kevin

Edit:
If you only just learned what a loop is, your code is just fine, or impressive even. Your goal at this point is to write statements which WORK, not ones which look nice and fit on a post-it note.

Absolutely - if this is your first loop, well done! I just assumed that you knew a loop given that you were readily accessing members within what appears to be a struck/union.
 

iCyborg

Golden Member
Aug 8, 2008
1,342
59
91
I'm also of the opinion that formatting the big 'if' more nicely would achieve the same effect as your multiple 'if's, readibility wise. Perhaps he already did that, but the forum made it messy...
 
Sep 29, 2004
18,656
67
91
Originally posted by: iCyborg
I'm also of the opinion that formatting the big 'if' more nicely would achieve the same effect as your multiple 'if's, readibility wise. Perhaps he already did that, but the forum made it messy...

As long as the logic repeats like the OPs does, I'd say that this is best practice.

Ive had some messes in the past and found that another good solution is temp variables granted it runs slower.
 

Gamingphreek

Lifer
Mar 31, 2003
11,679
0
81
Ive had some messes in the past and found that another good solution is temp variables granted it runs slower.

Not necessarily. Unless you create more variables than registers, allocate memory for each variable (needlessly), or if the variables are used in recursion that can't be optimized out, there should be no performance penalty. Just because you declare a variable, doesn't mean it is slower - often times it can even be faster.

-Kevin
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |