2

Good day for everyone! So, I have a case like this one. I need to write a method, that says if the rectangles overlap each other. Inputs are the following: height, width, x-pos and y-pos and rectangles are parallel to the x and the y axis. I used the solution from the questio, to which I gave a link, but it doesn't work properly. It tells that rectangles overlap even if they don't! Am I missing something important?

Code itself:

    public static bool AreIntersected(Rectangle r1, Rectangle r2)
    {
    return (!(r1.Left > r2.Left + r2.Width) || !(r1.Left + r1.Width < r2.Left) || !(r1.Top < r2.Top - r2.Height)|| !(r1.Top - r1.Height > r2.Top));
    }    

Screen of the error

And here it works just fine

Many thanks for your help!

Community
  • 1
  • 1
Olga Zhukova
  • 369
  • 1
  • 5
  • 13
  • Is this one of the answers provided to that question? If not, why did you make up your own version when there are simple, straightforward, highly upvoted solutions you could have used? – adv12 Sep 15 '16 at 20:31
  • There is already a [Rectangle.Intersect](https://msdn.microsoft.com/en-us/library/y10fyck0(v=vs.110).aspx) method – Matias Cicero Sep 15 '16 at 20:37
  • It is one of the answers to that question, and it didn't work. And I can't use already existing method, it's part of a task, to make my own method. (so I probably should change a question a little bit) – Olga Zhukova Sep 15 '16 at 20:40
  • If it's part of a task to make your own method, why are you asking others to make it for you? – adv12 Sep 15 '16 at 20:41
  • Because I do not understand, what I'm doing wrong, and have no opportunity to ask an expert for help in person – Olga Zhukova Sep 15 '16 at 20:44
  • Have you tried [debugging your small program](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)? – Hermann Döppes Sep 15 '16 at 20:53
  • What is the difference, as you see it, between using an "already existing method" and copy-pasting from Stack Overflow? – adv12 Sep 15 '16 at 20:54
  • I don't just copy-pasting it. I try to understand the logic behind it with the help of people's answers, as I don't get it by myself – Olga Zhukova Sep 16 '16 at 05:06

3 Answers3

4

The answer is on the page you linked. The only change to your code would be to replace rx.Right with rx.Left + rx.Width and rx.Bottom with rx.Top + rx.Height.

return !(r1.Left > r2.Left + r2.Width) &&
       !(r1.Left + r1.Width < r2.Left) &&
       !(r1.Top > r2.Top + r2.Height) &&
       !(r1.Top + r1.Height < r2.Top);

Then again, I'm assuming that you have your own Rectangle class you are using. If you are using the .NET Rectangle struct, that object has the Right and Bottom properties built in, so no code substitution is necessary.

return !(r1.Left > r2.Right) &&
       !(r1.Right < r2.Left) &&
       !(r1.Top > r2.Bottom) &&
       !(r1.Bottom < r2.Top);

Of course, you can also just as easily use the static Rectangle.Intersect method.

return !Rectangle.Intersect(r1, r2).IsEmpty;
Abion47
  • 22,211
  • 4
  • 65
  • 88
  • Addiing r.Height the r.Top instead of subtraction helped! I just don't understand, why we should add it? We are given the coordinates of upper left corner, and by adding height we're moving up the y axis, but rectangle's side must be under the upper left point. – Olga Zhukova Sep 16 '16 at 05:04
  • In computer screen space, the point (0,0) is in the top left corner of the window, and going up in the Y direction means going down the window. So when you have a rectangle's X and Y coordinate, that is the Left and Top side, so Left + Width is Right and Top + Height is Bottom. – Abion47 Sep 16 '16 at 07:24
3

Use Rectangle.Intersect:

public static bool AreIntersected(Rectangle r1, Rectangle r2)
{
    return !Rectangle.Intersect(r1, r2).IsEmpty;
}

If you can't use Rectangle.Intersect:

public static bool AreIntersected(Rectangle r1, Rectangle r2)
{
    int x1 = Math.Max(r1.X, r2.X);
    int x2 = Math.Min(r1.X + r1.Width, r2.X + r2.Width); 
    int y1 = Math.Max(r1.Y, r2.Y);
    int y2 = Math.Min(r1.Y + r1.Height, r2.Y + r2.Height); 

    return (x2 >= x1 && y2 >= y1);
}

Another approach:

public static bool AreIntersected(Rectangle r1, Rectangle r2)
{
    return(r2.X < r1.X + r1.Width) &&
        (r1.X < (r2.X + r2.Width)) && 
        (r2.Y < r1.Y + r1.Height) &&
        (r1.Y < r2.Y + r2.Height); 
}
Matias Cicero
  • 25,439
  • 13
  • 82
  • 154
1

These 2 approaches are equivalent - you mixed the || and &&

public static bool AreIntersected(Rectangle r1, Rectangle r2)
{
    bool test1 = ((r1.Left > r2.Left + r2.Width) || (r1.Left + r1.Width < r2.Left) || (r1.Top < r2.Top - r2.Height)|| (r1.Top - r1.Height > r2.Top));
    bool test2 = (!(r1.Left > r2.Left + r2.Width) && !(r1.Left + r1.Width < r2.Left) && !(r1.Top < r2.Top - r2.Height) && !(r1.Top - r1.Height > r2.Top));
    return test1; // or test2 as they are logically equivalent
}           
John D
  • 1,627
  • 1
  • 11
  • 10