How do I see if two rectangles intersect in JavaScript or pseudocode?

后端 未结 5 2163
孤城傲影
孤城傲影 2021-02-19 04:53

I have two rectangles which I must return in a function whether they intersect or not.

They are represented by [ x0, y0, x1, y1 ] pairs that represent the t

5条回答
  •  温柔的废话
    2021-02-19 05:38

    Give rectangle 1 with points UL1 and LR1 and rectangle 2 with points UR2 and LR2 -

    Check if UL1 is in r2 or LR1 is in r2 (case 1 and case 2 in diagram). Finally check if one of UR2 / LR2 is in r1 (case 3 in diagram).

    You check if a point is in a rectangle by checking that x and y are between min and max of a rectangles' x and y range.

    Clear?

    enter image description here

    Blue is R1, Purple is R2

提交回复
热议问题