答案是X = 270 Y = 395
首先定義斜率V作爲DY/DX =(Y2-Y1)/(X2-X1)。在你的例子中:(35-20)/(30-20)= 1.5
線方程是 y = V *(x-x1)+ y1。你是在有意在水平位置x: Y = CH/2或y = H-CH/2 左右(不代碼,只是數學)
if (y2-y1)<0:
x=(CH/2 -y1)/V +x1 10 for your example. OR
if (y2-y1)>0:
x=(H-CH/2 -y1)/V +x1 270 for your example
else (that is: y2==y1)
the upper or lower lines were not hit.
if CH/2 <= x <= W-CH/2 the circle did hit the that upper or lower side: since V>0, we use x=270 and that is within CH/2 and W-CH/2.
所以回答你的問題爲y = H -CH/2 = 395,X = 270
對於側線是相似的:
(if (x2-x1)<0)
y=(CH/2 -x1)*V +y1
(if (x2-x1)>0)
y=(W-CH/2 -x1)*V +y1
else (that is: x2==x1)
the side lines were not hit.
if CH/2 <= y <= H-CH/2 the circle did hit that side at that y.
小心完全水平或垂直移動的簡單的情形,這樣你不除以零。計算V或1/V時。也處理圓圈根本不動的情況。
既然你現在問了,這裏的元代碼,你應該很容易能夠轉換爲一個真正的方法。它也處理特殊情況。輸入是您在示例中列出的所有變量。我在這裏只使用一個符號來表示圓的大小,因爲它是一個圓而不是橢圓。
method returning a pair of doubles getzy(x1,y1,W,H,CH){
if (y2!=y1){ // test for hitting upper or lower edges
Vinverse=(x2-x1)/(y2-y1)
if ((y2-y1)<0){
xout=(CH/2 -y1)*Vinverse +x1
if (CH/2 <= y <= H-CH/2) {
yout=CH/2
return xout,yout
}
}
if ((y2-y1)>0){
xout=(H-CH/2 -y1)*Vinverse +x1
if (CH/2 <= y <= H-CH/2) {
yout=H-CH/2
return xout,yout
}
}
}
// reaching here means upper or lower lines were not hit.
if (x2!=x1){ // test for hitting upper or lower edges
V=(y2-y1)/(x2-x1)
if ((x2-x1)<0){
yout=(CH/2 -x1)*V +y1
if (CH/2 <= x <= W-CH/2) {
xout=CH/2
return xout,yout
}
}
if ((x2-x1)>0){
yout=(H-CH/2 -x1)*V +y1
if (CH/2 <= x <= W-CH/2) {
xout=H-CH/2
return xout,yout
}
}
}
// if you reach here that means the circle does not move...
deal with using exceptions or some other way.
}
我不知道的手機,但在winforws應該是這樣的:如果它要正確會像'而(circle.Location.X <(this.Width - circle.Width)){/* DO STUFF * /};' –
不,您正試圖測試它是否已經到達最終位置。我試圖在達到它之前計算最終位置。 –
你知道兩個圓的半徑和它們中心的位置,所以使用直線方程:'y = m * x + b',沒有微積分,只有代數。 –