fork download
  1. import matplotlib.pyplot as plt
  2.  
  3. # x 값을 직접 생성하는 함수 (numpy 없이)
  4. def generate_x(start, end, step):
  5. data = []
  6. v = start
  7. while v <= end:
  8. data.append(v)
  9. v += step
  10. return data
  11.  
  12. # 포물선 y = x^2
  13. x = generate_x(-2, 2, 0.01)
  14. y = [v**2 for v in x]
  15.  
  16. # 여러 광선을 시작할 x 좌표
  17. ray_starts = [-1.5, -1.0, -0.5, 0.5, 1.0, 1.5]
  18.  
  19. plt.figure(figsize=(6,6))
  20.  
  21. # 포물선 그리기
  22. plt.plot(x, y)
  23.  
  24. for xs in ray_starts:
  25. # 포물선과 만나는 점 (px, py)
  26. px = xs
  27. py = px**2
  28.  
  29. # 포물선의 접선 기울기 m = 2x
  30. m_tan = 2 * px
  31.  
  32. # 반사된 광선 기울기 (단순 반사용)
  33. m_ref = -m_tan
  34.  
  35. # 들어오는 광선: y = 1에서 포물선으로 내려오는 선
  36. plt.plot([xs, px], [1, py])
  37.  
  38. # 반사된 광선을 직접 생성 (x축 방향으로 1만큼)
  39. rx = generate_x(px, px + 1, 0.02)
  40. ry = [py + m_ref * (r - px) for r in rx]
  41.  
  42. plt.plot(rx, ry)
  43.  
  44. plt.xlim(-2, 2)
  45. plt.ylim(-0.2, 2)
  46. plt.xlabel("x")
  47. plt.ylabel("y")
  48. plt.title("포물선 반사 시뮬레이션")
  49. plt.show()
  50.  
Success #stdin #stdout #stderr 4.68s 67300KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Fontconfig error: No writable cache directories