Results 1 to 13 of 13

Thread: [2005] overlapping rectangles

  1. #1

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Question [2005] overlapping rectangles

    how can i test for overlapping rectangles?

    for example:

    vb.net Code:
    1. rectangle1 = new rectangle(168,111,10,36)
    2. rectangle2 = new rectangle(160,108,18,18)

    these 2 rectangles overlap. any ideas?

    the rectangles are actually a textbox + a picturebox

  2. #2
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Anchorage, Alaska
    Posts
    545

    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

  3. #3

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: [2005] overlapping rectangles

    that doesn't work unless at least 1 corner of r2 is in r1

  4. #4
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Anchorage, Alaska
    Posts
    545

    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

  5. #5

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: [2005] overlapping rectangles

    ok thanks

  6. #6
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: [2005] overlapping rectangles

    Quote Originally Posted by .paul.
    how can i test for overlapping rectangles?

    for example:

    vb.net Code:
    1. rectangle1 = new rectangle(168,111,10,36)
    2. 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.
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  7. #7
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,051

    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

  8. #8
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Anchorage, Alaska
    Posts
    545

    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

  9. #9
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,051

    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

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] overlapping rectangles

    Quote 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Anchorage, Alaska
    Posts
    545

    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

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] overlapping rectangles

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    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
  •  



Click Here to Expand Forum to Full Width