0
我正在編寫一個python程序,其中用戶畫出的線條反彈了一個圓圈。有多個圈子從牆上彈開。對於每一個,都應計算從圓心和球的最短距離。我更喜歡這個代碼是非常有效的,因爲我目前的算法滯後於計算機。如果a點是起點,b點是終點,c點是中心點,r是半徑,我將如何計算球之間的最短距離?如果球的X座標超出AB段的x座標範圍,該算法也應該可以工作。python tkinter中的圓形和線條碰撞檢測
請發表python代碼
任何幫助將不勝感激!
這是我到目前爲止有:
lineList是4個值用戶繪製線條,它包含起點和終點座標列表
中心是球的中心
global lineList, numobjects
if not(0 in lineList):
beginCoord = [lineList[0],lineList[1]]
endCoord = [lineList[2]-500,lineList[3]-500]
center = [xCoordinate[i],yCoordinate[i]+15]
distance1 = math.sqrt((lineList[1] - center[1])**2 + (lineList[0] - center[0])**2)
slope1 = math.tan((lineList[1] - lineList[3])/(lineList[0] - lineList[2]))
try:
slope2 = math.tan((center[1] - beginCoord[1])/(center[0]-beginCoord[0]))
angle1 = slope2 + slope1
circleDistance = distance1 * math.sin(angle1)
except:
#If the circle is directly above beginCoord
circleDistance = center[1] - lineList[1]
global numbounces
if circleDistance < 2 and circleDistance > -2:
print(circleDistance)
b = False
b2=False
if xCoordinate[i] < 0:
xCoordinate[i] += 1
speed1[i] *= -1
b=True
elif xCoordinate[i] > 0:
xCoordinate[i] -= 1
speed1[i] *= -1
b=True
if yCoordinate[i] < 0:
yCoordinate[i] += 1
speed2[i] *= -1
b2=True
elif yCoordinate[i] > 0:
yCoordinate[i] -= 1
speed2[i] *= -1
b2=True
if b and b2:
#Only delete the line if the ball reversed directions
numbounces += 1
#Add a ball after 5 bounces
if numbounces % 5 == 0 and numbounces != 0:
numobjects = 1
getData(numobjects)
canvas.delete("line")
lineList = [0,0,0,0]
是的,這就是我的意思。謝謝 – user3129956