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
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):