给定一对撞球在撞击前的二维位置和速度,计算出完全弹性碰撞后的速度。假定这些球是具有相同半径,相同质量,均匀密度且无摩擦的理想球体(或等效地:圆形)。
输入由8个数字:p0x,p0y,v0x,v0y,p1x,p1y,v1x,v1y
其中p0x,p0y
是所述第一球,中心v0x,v0y
其速度,并且类似地p1x,p1y,v1x,v1y
,第二球。可以接受以任何顺序输入,并且在任何方便的方式结构化,例如,作为一个2x2x2的阵列,或者一个2×2阵列p
和两个长度为2的数组为v0
和v1
。也可以使用复数(如果您的语言支持)而不是xy对。但是,您不应在非直角坐标系的坐标系中进行输入,即不允许使用极坐标。
请注意,撞球的半径是p0x,p0y
和之间的距离的一半p1x,p1y
,因此并未将其作为输入的明确部分给出。
编写一个程序或函数,以任何方便的笛卡尔表示形式输出或返回4个数字:的碰撞后值v0x,v0y,v1x,v1y
。
可能的算法是:
找到穿过两个中心的法线
找到穿过两个中心之间的中点并垂直于法线的切线
改变坐标系和分解
v0x,v0y
,并v1x,v1y
纳入其切线和正常组件v0t,v0n
和v1t,v1n
交换的正常组成部分
v0
和v1
,维护他们的切向分量改回原始坐标系
测试(结果四舍五入到小数点后五位):
p0x p0y v0x v0y p1x p1y v1x v1y -> v0x' v0y' v1x' v1y'
[-34.5,-81.8, 34.7,-76.1, 96.2,-25.2, 59.2,-93.3] [ 49.05873, -69.88191, 44.84127, -99.51809]
[ 36.9, 77.7,-13.6,-80.8, -7.4, 34.4, 15.1,-71.8] [ 5.57641, -62.05647, -4.07641, -90.54353]
[-51.0, 17.6, 46.1,-80.1, 68.6, 54.0,-35.1,-73.9] [ -26.48927,-102.19239, 37.48927, -51.80761]
[-21.1,-52.6,-77.7, 91.5, 46.0, 94.1, 83.8, 93.7] [ -48.92598, 154.40834, 55.02598, 30.79166]
[ 91.3, -5.3, 72.6, 89.0, 97.8, 50.5, 36.2, 85.7] [ 71.73343, 81.56080, 37.06657, 93.13920]
[-79.9, 54.9, 92.5,-40.7,-20.8,-46.9,-16.4, -0.9] [ 47.76727, 36.35232, 28.33273, -77.95232]
[ 29.1, 80.7, 76.9,-85.1,-29.3,-49.5,-29.0,-13.0] [ 86.08581, -64.62067, -38.18581, -33.47933]
[ 97.7,-89.0, 72.5, 12.4, 77.8,-88.2, 31.5,-34.0] [ 33.42847, 13.97071, 70.57153, -35.57071]
[-22.2, 22.6,-61.3, 87.1, 67.0, 57.6,-15.3,-23.1] [ -58.90816, 88.03850, -17.69184, -24.03850]
[-95.4, 15.0, 5.3, 39.5,-54.7,-28.5, -0.7, 0.8] [ 21.80656, 21.85786, -17.20656, 18.44214]
[ 84.0,-26.8,-98.6,-85.6,-90.1, 30.9,-48.1, 37.2] [ -89.76828, -88.52700, -56.93172, 40.12700]
[ 57.8, 90.4, 53.2,-74.1, 76.4,-94.4,-68.1,-69.3] [ 51.50525, -57.26181, -66.40525, -86.13819]
[ 92.9, 69.8,-31.3, 72.6,-49.1,-78.8,-62.3,-81.6] [-123.11680, -23.48435, 29.51680, 14.48435]
[-10.3,-84.5,-93.5,-95.6, 35.0, 22.6, 44.8, 75.5] [ -11.12485, 99.15449, -37.57515,-119.25449]
[ -3.9, 55.8,-83.3, 9.1, -2.7,-95.6, 37.7,-47.8] [ -82.84144, -48.75541, 37.24144, 10.05541]
[-76.5,-88.4,-76.7,-49.9, 84.5, 38.0, 4.2, 18.4] [ 6.52461, 15.43907, -79.02461, -46.93907]
[ 64.2,-19.3, 67.2, 45.4,-27.1,-28.7, 64.7, -4.3] [ 59.66292, 44.62400, 72.23708, -3.52400]
[ 9.8, 70.7,-66.2, 63.0,-58.7, 59.5, 83.7,-10.6] [ 68.07646, 84.95469, -50.57646, -32.55469]
[ 62.9, 46.4, 85.0, 87.4, 36.3,-29.0,-63.0,-56.3] [ 23.53487, -86.82822, -1.53487, 117.92822]
[ -5.5, 35.6, 17.6,-54.3, -2.2, 66.8,-15.2, 11.8] [ 24.15112, 7.63786, -21.75112, -50.13786]
最短的胜利。没有漏洞。
感谢@Anush 帮助修复图表的背景色
r=p-q
为p-=q
并进一步p
代替使用r
,例如Neil的js答案