Spirograph 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #万花尺程序,通过命令行输入R,r,a 三个数来产生最终效果。
  2. '''
  3. x(t) = (R+r)cos(t)-(r+a)cos((R+r)t/r)
  4. y(t) = (R+e)sin(t)-(r+a)sin((R+r)t/r)
  5. '''
  6. import sys
  7. import math
  8. from stdpackage import stddraw,stdarray
  9. def draw_spirograph(R,r,a):
  10. stddraw.setXscale(-40,40)
  11. stddraw.setYscale(-40,40)
  12. l = 1500
  13. x = stdarray.create1D(l,0)
  14. y = stdarray.create1D(l,0)
  15. for t in range(l):
  16. x[t] = (R+r)*math.cos(t) - (r+a) * math.cos((R+r)*t/r)
  17. y[t] = (R+r)*math.sin(t) - (r+a) * math.sin((R+r)*t/r)
  18. for t in range(l+1):
  19. if t >= l-1:
  20. pass
  21. else:
  22. if t ==0:
  23. stddraw.setPenColor(stddraw.RED)
  24. if t==l/3:
  25. stddraw.setPenColor(stddraw.DARK_BLUE)
  26. if t == 2*l/3:
  27. stddraw.setPenColor(stddraw.BOOK_BLUE)
  28. stddraw.line(x[t],y[t],x[t+1],y[t+1])
  29. stddraw.show(5)
  30. stddraw.show()
  31. def main():
  32. R = int(sys.argv[1])
  33. r = int(sys.argv[2])
  34. a = int(sys.argv[3])
  35. draw_spirograph(R,r,a)
  36. main()