栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

检测与圆角的矩形碰撞

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

检测与圆角的矩形碰撞

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

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/661781.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号