python circle(x、10)_python – 使用Circle检测Rectangle碰撞

我有一个带有中心点的圆(Center_X,Center_Y),我正在检测矩形是否落入其半径(半径).我怎么能够完成这项任务?我试过用

if (X - Center_X)^2 + (Y - Center_Y)^2 < Radius^2:

print(1)

然后我尝试绘制一个圆圈以适应这个区域:

Circle = pygame.draw.circle(Window, Blue, (Center_X, Center_Y), Radius, 0)

但它似乎没有排队.有什么我做错了吗?

解决方法:

以下是我在评论中所描述的内容,以及对Michael Anderson在评论中指出的更大矩形内正确处理圆形情况的更改:

import math

def 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

标签:python,pygame,geometry,collision-detection

来源: https://codeday.me/bug/20191007/1865293.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值