Circle-Rectangle collision detection (intersection)

前端 未结 24 1696
无人共我
无人共我 2020-11-22 02:55

How can I tell whether a circle and a rectangle intersect in 2D Euclidean space? (i.e. classic 2D geometry)

24条回答
  •  轮回少年
    2020-11-22 03:10

    worked for me (only work when angle of rectangle is 180)

    function intersects(circle, rect) {
      let left = rect.x + rect.width > circle.x - circle.radius;
      let right = rect.x < circle.x + circle.radius;
      let top = rect.y < circle.y + circle.radius;
      let bottom = rect.y + rect.height > circle.y - circle.radius;
      return left && right && bottom && top;
    }
    

提交回复
热议问题