|
-
Nov 24th, 2007, 06:13 PM
#1
[2005] overlapping rectangles
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
-
Nov 24th, 2007, 06:29 PM
#2
Fanatic Member
Re: [2005] overlapping rectangles
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.
Please RATE posts, click the RATE button to the left under the Users Name.
Once your thread has been answered, Please use the Thread Tools and select RESOLVED so everyone knows your question has been answered.
"As I look past the light, I see the world I wished tonight, never the less, sleep has come, and death shall soon follow..." © 1998 Jeremy J Swartwood
-
Nov 24th, 2007, 06:32 PM
#3
Re: [2005] overlapping rectangles
that doesn't work unless at least 1 corner of r2 is in r1
-
Nov 24th, 2007, 06:38 PM
#4
Fanatic Member
Re: [2005] overlapping rectangles
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.
Please RATE posts, click the RATE button to the left under the Users Name.
Once your thread has been answered, Please use the Thread Tools and select RESOLVED so everyone knows your question has been answered.
"As I look past the light, I see the world I wished tonight, never the less, sleep has come, and death shall soon follow..." © 1998 Jeremy J Swartwood
-
Nov 24th, 2007, 06:43 PM
#5
Re: [2005] overlapping rectangles
-
Nov 24th, 2007, 06:50 PM
#6
Re: [2005] overlapping rectangles
 Originally Posted by .paul.
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
Hi,
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:
Code:
rectangle1 = new rectangle(168,111,10,36)
rectangle2 = new rectangle(168,122,18,18)
That they will be situated under each other with there own size.
Or you can have the same size and then you do this:
Code:
rectangle1 = new rectangle(168,111,10,36)
rectangle2 = new rectangle(168,122,10,36)
Hope it helps,
sparrow1
Last edited by sparrow1; Nov 24th, 2007 at 06:55 PM.
-
Nov 24th, 2007, 06:53 PM
#7
Frenzied Member
Re: [2005] overlapping rectangles
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
-
Nov 24th, 2007, 06:57 PM
#8
Fanatic Member
Re: [2005] overlapping rectangles
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 =)
Please RATE posts, click the RATE button to the left under the Users Name.
Once your thread has been answered, Please use the Thread Tools and select RESOLVED so everyone knows your question has been answered.
"As I look past the light, I see the world I wished tonight, never the less, sleep has come, and death shall soon follow..." © 1998 Jeremy J Swartwood
-
Nov 24th, 2007, 07:12 PM
#9
Frenzied Member
Re: [2005] overlapping rectangles
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
-
Nov 24th, 2007, 08:44 PM
#10
Re: [2005] overlapping rectangles
 Originally Posted by rack
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 =)
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.
-
Nov 24th, 2007, 09:18 PM
#11
Fanatic Member
Re: [2005] overlapping rectangles
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.
Please RATE posts, click the RATE button to the left under the Users Name.
Once your thread has been answered, Please use the Thread Tools and select RESOLVED so everyone knows your question has been answered.
"As I look past the light, I see the world I wished tonight, never the less, sleep has come, and death shall soon follow..." © 1998 Jeremy J Swartwood
-
Nov 24th, 2007, 09:37 PM
#12
Re: [2005] overlapping rectangles
 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.
-
Nov 25th, 2007, 03:38 AM
#13
Re: [2005] overlapping rectangles
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|