Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validating Rectangular Regions on a form 2

Status
Not open for further replies.

barryna

Programmer
Feb 15, 2002
111
US
I have a puzzle all of you out there. I designed a program so the user can draw their own rectangular boundries by clicking, dragging and mouseup. I am able to grab the 4 coordinates of a rectangle that way and a draw the rectangle. However, my problem is not doing that, it is the complexity in validating to make sure no new rectangle drawn overlaps any existing rectangle. So far, I have not been able to come up with a simple function to check for that. I am storing the upper left coordinate (X1,Y1) and the lower right coordinate (X2,Y2) of the rectangles. The other 2 vertices can be determined in code very simply of course. Any ideas how to validate that? I could do it, but it will be a ton of code (there are just too many possiblities). Anything come to mind to make this validation simple? One problem I came across is that 2 rectangles can overlap without any of the 4 vertices of one rectangle being inside the other rectangle and vice versa. Also, one rectangle could be completely inside another rectangle, which poses another problem. There's got to be a mathmatical algorithm to do this or something.
 

Check out the RECT structure and search for strongm's code on collision detection and I belive you will have your answer.

Good Luck

 
You can use the RECT structure to store the coordinates of rectangles and determine their overlapping using IntersectRect and IsRectEmpty API functions. See the code fragment below.
___
[tt]
'declarations
Private Declare Function IntersectRect Lib "user32" (lpDestRect As RECT, lpSrc1Rect As RECT, lpSrc2Rect As RECT) As Long
Private Declare Function IsRectEmpty Lib "user32" (lpRect As RECT) As Long
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

...

'code
Dim Overlap As Boolean
Dim R1 As RECT, R2 As RECT, R As RECT

'the first rectangle
R1.Left = X11
R1.Top = Y11
R1.Right = X12
R1.Bottom = Y12

'the second rectange
R1.Left = X21
R1.Top = Y21
R1.Right = X22
R1.Bottom = Y22

IntersectRect R, R1, R2

Overlap = IsRectEmpty(R) = 0
'returns true if overlapping,
'false otherwise[/tt]
___

See thread222-626738 for another example. This thread also contains a pointer to the strongm's collision detection code using regions, mentioned by vb5prgrmr. This method has the advantage of dealing with non-rectangular objects as well.
 
Excellent Hypetia. Works great. Only had to fix the code you posted to use R2 for the second rectangle. Star for you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top