检测与圆角的矩形碰撞

检测与圆角的矩形碰撞,第1张

检测与圆角的矩形碰撞

这是我在评论中所描述的内容,并进行了更改,以正确处理迈克尔·安德森在评论中指出的较大矩形内的圆形情况:

import mathdef collision(rleft, rtop, width, height,   # rectangle definition   center_x, center_y, radius):  # circle definition    """ Detect collision between a rectangle and circle. """    # complete boundbox of the rectangle    rright, rbottom = rleft + width/2, rtop + height/2    # bounding box of the circle    cleft, ctop     = center_x-radius, center_y-radius    cright, cbottom = center_x+radius, center_y+radius    # trivial reject if bounding boxes do not intersect    if rright < cleft or rleft > cright or rbottom < ctop or rtop > cbottom:        return False  # no collision possible    # check whether any point of rectangle is inside circle's radius    for x in (rleft, rleft+width):        for y in (rtop, rtop+height): # compare distance between circle's center point and each point of # the rectangle with the circle's radius if math.hypot(x-center_x, y-center_y) <= radius:     return True  # collision detected    # check if center of circle is inside rectangle    if rleft <= center_x <= rright and rtop <= center_y <= rbottom:        return True  # overlaid    return False  # no collision detected


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5663737.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存