1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
1
Rep 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. 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.