python判断 射线是否与圆相交
# 以上是返回截距式方程的y=kx+b的k和b
def GeneralEquation(first_x,first_y,second_x,second_y):
A = second_y-first_y
B = first_x-second_x
C = second_x*first_y-first_x*second_y
k = -1 * A / B
b = -1 * C / B
return k, b
# 循环遍历矩形每个点 判断在不在直线上
def isCollsion1(x,y,w,h,first_x,first_y,second_x,second_y):
# 缩小矩形框增加精确度
centerx=int(x+w/2);
centery=int(y+h/2);
# 计算截距式方程
k,b=GeneralEquation(first_x,first_y,second_x,second_y)
# 遍历矩形内每个点 判断是不是在直线上
for w2 in range(centerx-3,centerx+3):
for h2 in range(centery-3,centery+3):
y=k*w2+b
if(y==h2):
return 1;
return 0;
iscol1=isCollsion1(0,1,2,2,1,1,2,2)
print(iscol1);