答えは 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 OR 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 を計算する場合。円がまったく動かなかった場合にも対処します。
あなたが尋ねたので、実際のメソッドに簡単に変換できるはずのメタコードを次に示します。特殊なケースも扱います。入力は、例にリストしたすべての変数です。円は楕円ではなく円であるため、ここでは円のサイズに 1 つの記号のみを使用します。
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.
}