Jump to content

Check rectangle overlap given bottom left corner, width, and height

Zalosath
Go to solution Solved by Zalosath,

Ugh, solved. 
 

    public bool Overlaps(Room secondRoom)
    {
        bool condition1 = X2 - 1 > secondRoom.X1;
        bool condition2 = X1 < secondRoom.X2 - 1;
        bool condition3 = Y2 - 1 > secondRoom.Y1;
        bool condition4 = Y1 < secondRoom.Y2 - 1;

        return condition1 && condition2 && condition3 && condition4;
    }

 

Hi,

Can't figure this out for the life of me, everything on StackOverflow and what ChatGPT is trying to tell me doesn't work, always fails one of the conditions and I cannot work out what I'm doing wrong.

 

According to the following:

    public int X1 { get { return x; } }
    public int X2 { get { return x + width; } }
    public int Y1 { get { return y; } }
    public int Y2 { get { return y + height; } }
    
    public bool Overlaps(Room secondRoom)
    {
        if (X2 < secondRoom.X1 || secondRoom.X2 < X1 || Y2 < secondRoom.Y1 || secondRoom.Y2 < Y1)
        {
            return true;
        }
        return false;
    }

These rectangles overlap:

image.thumb.png.e1040250fe6f55d171c4c22a007ca026.png

 

And I can assure you, they do not!

 

Debug output:

Blue Rect
X1 Y1    X2  Y2
8, 18 -> 16, 28

Pink Rect
X1 Y1    X2  Y2
7, 6  -> 19, 13

 

Any help greatly appreciated; I feel that this should be simpler than it is being. 

Main PC [ CPU AMD Ryzen 9 7900X3D with H150i ELITE CAPPELIX  GPU Nvidia 3090 FE  MBD ASUS ROG STRIX X670E-A  RAM Corsair Dominator Platinum 64GB@5600MHz  PSU HX1000i  Case Lian Li PC-O11 Dynamic  Monitor LG UltraGear 1440p 32" Nano IPS@180Hz  Keyboard Keychron Q6 with Kailh Box Switch Jade  Mouse Logitech G Pro Superlight  Microphone Shure SM7B with Cloudlifter & GoXLR ]

 

Server [ CPU AMD Ryzen 5 5600G  GPU Intel ARC A380  RAM Corsair VEGEANCE LPX 64GB  Storage 16TB EXOS ]

 

Phone [ Google Pixel 8 Pro, 256GB, Snow ]

Link to comment
Share on other sites

Link to post
Share on other sites

Ugh, solved. 
 

    public bool Overlaps(Room secondRoom)
    {
        bool condition1 = X2 - 1 > secondRoom.X1;
        bool condition2 = X1 < secondRoom.X2 - 1;
        bool condition3 = Y2 - 1 > secondRoom.Y1;
        bool condition4 = Y1 < secondRoom.Y2 - 1;

        return condition1 && condition2 && condition3 && condition4;
    }

 

Main PC [ CPU AMD Ryzen 9 7900X3D with H150i ELITE CAPPELIX  GPU Nvidia 3090 FE  MBD ASUS ROG STRIX X670E-A  RAM Corsair Dominator Platinum 64GB@5600MHz  PSU HX1000i  Case Lian Li PC-O11 Dynamic  Monitor LG UltraGear 1440p 32" Nano IPS@180Hz  Keyboard Keychron Q6 with Kailh Box Switch Jade  Mouse Logitech G Pro Superlight  Microphone Shure SM7B with Cloudlifter & GoXLR ]

 

Server [ CPU AMD Ryzen 5 5600G  GPU Intel ARC A380  RAM Corsair VEGEANCE LPX 64GB  Storage 16TB EXOS ]

 

Phone [ Google Pixel 8 Pro, 256GB, Snow ]

Link to comment
Share on other sites

Link to post
Share on other sites

20 minutes ago, Zalosath said:

what ChatGPT is trying to tell me doesn't work

chatgpt doesn't know what it's talking about, it's a coin toss whether anything it gives you is going to work.

 

It seems to me you have your if statement the wrong way around.

if (X2 < secondRoom.X1 || secondRoom.X2 < X1 || Y2 < secondRoom.Y1 || secondRoom.Y2 < Y1)
{
	return false; //<-- RETURN FALSE HERE
}
return true; //<-- RETURN TRUE HERE

 

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×