1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| import math
def ellipse_eq(x, y): return 4*x*x + y*y - 100
def normalize(v): norm = math.hypot(v[0], v[1]) return (v[0]/norm, v[1]/norm)
def reflect(v, x, y): n = (4*x, y) n = normalize(n)
dot = v[0]*n[0] + v[1]*n[1] v_ref = ( v[0] - 2*dot*n[0], v[1] - 2*dot*n[1] ) return normalize(v_ref)
def next_intersection(x0, y0, dx, dy): A = 4*dx*dx + dy*dy B = 8*x0*dx + 2*y0*dy C = 4*x0*x0 + y0*y0 - 100
delta = B*B - 4*A*C if delta < 0: return None
sqrt_delta = math.sqrt(delta) t1 = (-B + sqrt_delta) / (2*A) t2 = (-B - sqrt_delta) / (2*A)
t = min(t for t in [t1, t2] if t > 1e-6)
return (x0 + dx*t, y0 + dy*t)
x, y = 1.4, -9.6 dx, dy = normalize((1.4 - 0.0, -9.6 - 10.1))
hit_count = 0
while True: if abs(x) <= 0.01 and y > 0: break
dx, dy = reflect((dx, dy), x, y)
x, y = next_intersection(x, y, dx, dy)
hit_count += 1
print("激光在白腔内击中椭圆的次数:", hit_count)
|