in reply to Re: Checking if squares intersect
in thread Checking if squares intersect

Actually i came up with the following:

I done this by negation and using the values for the upper left lower right respectively for both sqares.

square #1: upper left= x1,y1 ; lower right= x2,y2 square #2: upper left= x3,y3 ; lower right= x4,y4 return !(x2 < x3 || x1 > x2 || y2 > y3 || y1 < y4)
Hope my logic is right...can someone confirm?

Thanks,
Total Newbie

Replies are listed 'Best First'.
Re^3: Checking if squares intersect
by ikegami (Patriarch) on Oct 14, 2009 at 20:36 UTC

    x4 is absent, so there's surely an error

    Adjacent squares don't intersect, so
    < should be <=
    > should be >=

    You didn't indicate whether lower "y" values are located higher (that's what you used) or lower (cartesian), so I can't verify the if you are comparing the "y"s properly.

      But wouldn't the squares intersect if I changed to < to <= ?

      Example, if x2 <= x3 then the statement is true if x2 intersects with x3 since x2 is the lower right corner and x3 is the upper left corner.

      And yes, the x4 is missing, it was a typo.
        No. Adjacent squares don't intersect.
        x1 x2=x3 x4 +---------+ y1 | +---------+ y3 | | | | | | | | | +---------+ | y2 +---------+ y4
Re^3: Checking if squares intersect
by JavaFan (Canon) on Oct 20, 2009 at 09:38 UTC
    Your logic only holds with the additional property that the squares have their edges parallel to the x and y axis. If that isn't the case, your logic fails.