November 18th, 2012, 05:15 PM

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!
November 18th, 2012, 11:20 PM

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,yw+
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 and Makefiles!