how can i test for overlapping rectangles?
for example:
vb.net Code:
rectangle1 = new rectangle(168,111,10,36) rectangle2 = new rectangle(160,108,18,18)
these 2 rectangles overlap. any ideas?
the rectangles are actually a textbox + a picturebox
Printable View
how can i test for overlapping rectangles?
for example:
vb.net Code:
rectangle1 = new rectangle(168,111,10,36) rectangle2 = new rectangle(160,108,18,18)
these 2 rectangles overlap. any ideas?
the rectangles are actually a textbox + a picturebox
I am at work, and dont' have access to VB.
You would check if the points are anywhere in the other.
R1X1 R1Y1, R1X2, R1Y2 Info Rectangle 1
R2X1 R2Y1, R2X2, R2Y2 Info Rectangle 2
Checking if rect 2 is in 1
If R2x1 >R1x1 and R2x1 < R1x2 then
'Point R2X1 is inside the R1 box (rectangle 1)
End if
Do the same for the other 3 points of the Rectangle 2.
that doesn't work unless at least 1 corner of r2 is in r1
If you need to check if they are ontop of eachother, but not inside, do the =
>=
<=
it will check if its on the same X or inside.
ok thanks
Hi,Quote:
Originally Posted by .paul.
If you I see your code then they overlapping.
rectangle1 = new rectangle(168= x,111=y,10,36= the size of your rectangle)
rectangle2 = new rectangle(160=x,111= y,18,18= the size of the other rectangle)
In code you have then:
That they will be situated under each other with there own size.Code:rectangle1 = new rectangle(168,111,10,36)
rectangle2 = new rectangle(168,122,18,18)
Or you can have the same size and then you do this:
Hope it helps,Code:rectangle1 = new rectangle(168,111,10,36)
rectangle2 = new rectangle(168,122,10,36)
sparrow1
Code:Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim R1 As New Rectangle(0, 0, 40, 20)
Dim R2 As New Rectangle(0, 10, 40, 20)
Dim Bool As Boolean = R1.IntersectsWith(R2)
MessageBox.Show(CStr(Bool))
Dim R3 As New Rectangle(0, 0, 40, 20)
Dim R4 As New Rectangle(0, 20, 40, 20)
Dim Bool2 As Boolean = R3.IntersectsWith(R4)
MessageBox.Show(CStr(Bool2))
End Sub
Nice, they inputed a sub/function for that huh?
I always end up recoding something, be it the format for data, the decimal removing, or checking if 1 rect is in or on another rect, haha.
Oh well, need to start looking up the new functions now days =)
If you wanted to just check for a single point being inside a rectangle, you could use something like this:
Code:Imports System.Drawing.Drawing2D
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim R1 As New Rectangle(0, 0, 40, 20)
Dim P As New GraphicsPath
P.AddRectangle(R1)
Dim PointIsInRectangle As Boolean = P.IsVisible(New Point(2, 2))
MessageBox.Show(CStr(PointIsInRectangle))
End Sub
End Class
It's not a case of "looking up the new functions". It's a case of looking in the most obvious place first, EVERY time. You're working with two Rectangle objects. The first place you should have looked was the documentation for the Rectangle structure. If you're using a specific type and you want to know how to do something to or with that type then the first thing you do is read the MSDN library overview for that type. If that doesn't answer your question then you click the Members link and then read the member listing for the type. EVERY developer should be doing that EVERY time.Quote:
Originally Posted by rack
I was responding to his post while at work, giving him an answer that would work. I have not worked with the rectangle object before, I did not start the post jmcilhinney. Althought not the best answer, my answer would have worked. I was just commenting back stating that I didn't know about the rectangle object. Sorry.
I actually didn't notice who started the thread and who posted what. I just made the same statement to everyone in general that I have made many, many times before. If you want help then the Help should be the place you look first every time. If you're using a specific type then the documentation for that type should be the first place you look in the Help. To me, that's just common sense. It's worked for me ever since I started .NET development and even before. I used to consult the MSDN library first before .NET existed and I was using C++, and the .NET documentation is much better than anything that went before it.Quote:
Originally Posted by rack
ok thanks for all the help.
i thought it might be .intersects, but i was busy with something else and planning ahead with this thread.