Circle-Rectangle collision detection (intersection)

前端 未结 24 1418
无人共我
无人共我 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

    def colision(rect, circle):
    dx = rect.x - circle.x
    dy = rect.y - circle.y
    distance = (dy**2 + dx**2)**0.5
    angle_to = (rect.angle + math.atan2(dx, dy)/3.1415*180.0) % 360
    if((angle_to>135 and angle_to<225) or (angle_to>0 and angle_to<45) or (angle_to>315 and angle_to<360)):
        if distance <= circle.rad/2.+((rect.height/2.0)*(1.+0.5*abs(math.sin(angle_to*math.pi/180.)))):
            return True
    else:
        if distance <= circle.rad/2.+((rect.width/2.0)*(1.+0.5*abs(math.cos(angle_to*math.pi/180.)))):
            return True
    return False
    

提交回复
热议问题