Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
November 18th, 2012, 05:15 PM
 Xios
Registered User

Join Date: Nov 2012
Posts: 1
Time spent in forums: 39 m 31 sec
Reputation Power: 0
Maths questions:

Hi guys, I'm trying to develop an exercise my teacher give me last week that is about drawing the rectangle that intersects between 2 rectangles:
___________
__|_ |
|_ |T| |
|__________|

Im already drawing everything because I fond some code but i dont understand it mathematically. Is this:

if(squarePosX > bkSquarePosX){
innerSquarePosX = squarePosX;
}
else{
innerSquarePosX = bkSquarePosX;
}
if(squarePosY > bkSquarePosY){
innerSquarePosY = squarePosY;
}
else{
innerSquarePosY = bkSquarePosY;
}
if((squarePosX + squareWidth) < (bkSquarePosX + bkSquareWidth )){
innerSquareWidth = squarePosX + squareWidth;
}
else{
innerSquareWidth = bkSquarePosX + bkSquareWidth;
}
if((squarePosY + squareHeight) < (bkSquarePosY + bkSquareHeight)){
innerSquareHeight = squarePosY+ squareHeight;
}

square is the little of 2, bkSquare is the big and innerSquare is the one created by the intersection of the other 2.

Can you guys explain me how this code(mathematically) does the calculations to have the innerSquare? Thx!

#2
November 18th, 2012, 11:20 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,159
Time spent in forums: 1 Month 3 Weeks 2 Days 9 h 8 m 16 sec
Reputation Power: 455
Please put code tags around the rectangles to preserve spacing--->spell code correctly...
[kode]text with preserved white space[/kode]
First find the variables in your code.
Code:
```   NB. http://www.jsoftware.com
A=:0 :0
if(squarePosX > bkSquarePosX){
innerSquarePosX = squarePosX;
}
else{
innerSquarePosX = bkSquarePosX;
}
if(squarePosY > bkSquarePosY){
innerSquarePosY = squarePosY;
}
else{
innerSquarePosY = bkSquarePosY;
}
if((squarePosX + squareWidth) < (bkSquarePosX + bkSquareWidth )){
innerSquareWidth = squarePosX + squareWidth;
}
else{
innerSquareWidth = bkSquarePosX + bkSquareWidth;
}
if((squarePosY + squareHeight) < (bkSquarePosY + bkSquareHeight)){
innerSquareHeight = squarePosY+ squareHeight;
}
)
>(~.;:A-.LF)-.;:')({};=+<>if else'
squarePosX
bkSquarePosX
innerSquarePosX
squarePosY
bkSquarePosY
innerSquarePosY
squareWidth
bkSquareWidth
innerSquareWidth
squareHeight
bkSquareHeight
innerSquareHeight
```
Next, observe that the "squares" can have differing height and width. They're rectangles, not necessarily squares. Confusing.
Code:
``` +------------------------W------------------(X+W,Y+H)
|                                               |
|                                               |
|                                               |
|                                               |
|                                               H
|                                               |
|                               (c+b,d+a)       |
|               +---------b-----(x+w,y+h)       |
|               |                   |           |
|               a                   |           |
|               |                   |           |
X,Y------------(c,d)-----------------|-------(X+W,Y)
|                   |
h                   |
|                   |
|                   |
|                   |
x,y--------w----------+```
The same logic will work for height and width.
c is the maximum of X and x. c = max(X,x)
c+b = min(X+W,x+w)
if c+b < c they rectangles don't intersect. (b < 0)
Warning, untested. My first and final drafts almost always differ.
__________________
[code]Code tags[/code] are essential for python code!

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Maths questions: