in reply to Checking if squares intersect

[ This posts assumes the squares exist on the same plane. ]

Start by listing all the different circumstances where the two squares intersect:

Then further refine those conditions to use definite articles:

Then express them in terms of functions or X and Y.

On second thought, it might be easier to to check if two squares *don't* intersect, then negate the result

Start by listing all the different circumstances where the two squares don't intersect:

Then further refine those conditions to use definite articles:

Then express them in terms of functions or X and Y.

Replies are listed 'Best First'.
Re^2: Checking if squares intersect
by Anonymous Monk on Oct 14, 2009 at 20:24 UTC
    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

      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.
      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.