euler-elastica.py 786 B

1234567891011121314151617181920212223242526272829
  1. from math import *
  2. def plot_elastica(a, c):
  3. s = 500
  4. cmd = 'moveto'
  5. dx = .001
  6. x, y = 0, 0
  7. if c * c > 2 * a * a:
  8. g = sqrt(c * c - 2 * a * a)
  9. x = g + .01
  10. if c == 0:
  11. x = .001
  12. try:
  13. for i in range(1000):
  14. print 6 + s * x, 200 + s * y, cmd
  15. cmd = 'lineto'
  16. x += dx
  17. if 1 and c * c > 2 * a * a:
  18. print (c * c - x * x) * (x * x - g * g)
  19. dy = dx * (x * x - .5 * c * c - .5 * g * g) / sqrt((c * c - x * x) * (x * x - g * g))
  20. else:
  21. dy = dx * (a * a - c * c + x * x)/sqrt((c * c - x * x) * (2 * a * a - c * c + x * x))
  22. y += dy
  23. except ValueError, e:
  24. pass
  25. print 'stroke'
  26. plot_elastica(1, 0)
  27. print 'showpage'