CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2011
    Posts
    4

    C# How to check if pictureBox1 touches pictureBox2

    What I basically have at the moment is a pictureBox1, you can move it by clicking anywhere on the form and it slowly moves at that coordinate + 1 at a time with the timer every 10 milliseconds.

    I created a pictureBox2, on the form which I want to act as a barrier.

    Does anyone know the code to check if pictureBox1 touches pictureBox2, i used a code before but it only seemed to work if the pictureBox1 approached pictureBox2 from bottom probably because the x, y was on the top left hand corner, and it also worked with pic approaching from the right because of the pics top left hand corner touching the pic2.

    But, what I need is a code that if pic1 touches any part of pic2 then pic1 stops in its position of my choice

  2. #2
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: C# How to check if pictureBox1 touches pictureBox2

    For two squares (pictureboxes) to be intersecting, you must consider all line-segment intersections, in particular:

    (a) at least one of the horzional edges of square1 must intersect a vertical edge of square2, or
    (b) at least one of the vertical edges of square1 must intersect a horizonal edge of square2

    This means you must check for 8 potential intersections. First calculate the positions of all of the corners of the square (you can do this with properties Left, Top, Size.X and Size.Y). Call these corners TL, TR, BL, BR for top-left, top-right, bottom-left and bottom-right, respectively.

    You can check, for example, if the left vertical edge of square2 intersects the top horizonal edge of square1 by checking (this is pseudocode to illustrate the idea, not direct code you can copy/paste):

    Code:
    bool edgeIntersects = (square1.TL.Y <= square2.BL.Y && square1.TL.Y >= square2.TL.Y &&
                           square2.TL.X >= square1.TL.X && square2.TL.X <= square1.TR.X);
    You can puzzle out the other 7 intersections. I suggest drawing a picture.

    Good luck!
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    May 2010
    Posts
    10

    Re: C# How to check if pictureBox1 touches pictureBox2

    Another, simpler approach:

    Code:
    public bool DoPicturesIntersect(PictureBox square1, PictureBox square2)
    {
          return square1.DisplayRectangle.IntersectsWith(square2.DisplayRectangle);
    }

  4. #4
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: C# How to check if pictureBox1 touches pictureBox2

    Most impressive. Complexity is the price I pay, I guess, for not knowing the API as well as I should. :-)
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

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

Featured