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

后端 未结 5 2178
孤城傲影
孤城傲影 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:41

    Update

    After a time I decided to redo a complete answer about detecting collision between 2 rotated rectangles: How to detect when rotated rectangles are colliding each other


    Original Answer

    If you want to check if 2 rotated rectangles collide you have to make projection of one rectangle corner on the axes of the other. If all projections of rectA hit the rectB, and rectB projections hit rectA then the two rectangles collide.

    Some projection don't collide here, the 2 rectangles are not collide. enter image description here

    The 4 projections hit the other rectangle, the 2 rectangles are collide. enter image description here

    I have made a presentation on this JSFiddle for more comprehension.

    You can check the function is_collide for more example

提交回复
热议问题