You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
1.7 KiB

  1. from RigidMass import RigidMass
  2. from PIL import Image
  3. from PIL import ImageFont
  4. from PIL import ImageDraw
  5. import random
  6. import math
  7. outImg = Image.new("RGB", [1000, 1000])
  8. def storePixels(name, things):
  9. global outImg
  10. #outImg = Image.new("RGB", [1000, 1000])
  11. w, h = 1000, 1000
  12. for thing in things:
  13. if thing.x > 0 and thing.y > 0 and thing.x < 1000 and thing.y < 1000:
  14. outImg.putpixel((int(thing.x), int(thing.y)), 16581370*255)
  15. #outImg.save(name)
  16. #sun = RigidMass( 500000000000000, 0, -5, 500, 500, 0.05)
  17. #earth = RigidMass(500000000000000, 0, 5, 250, 500, 0.05)
  18. tick_time = 0.05
  19. num_stars = 1000
  20. stars = []
  21. for i in range(num_stars):
  22. #give each star some angular velocity
  23. #did some cool algebra on paper
  24. x = random.randint(375, 625)
  25. y = random.randint(375, 625)
  26. dx = x - 500
  27. dy = y - 500
  28. if abs(dy) <= abs(dx):
  29. vy = 3 * random.random()
  30. vx = -vy * dy / dx
  31. else:
  32. vx = 3 * random.random()
  33. vy = -vx * dx / dy
  34. #this makes the circle rotate together
  35. #without it, one half rotates in the opposite direction
  36. #the two halves are separated by the y=x line
  37. #I have no idea why that happens, but this fixes it.
  38. if y < x:
  39. vx = -vx
  40. vy = -vy
  41. stars.append(RigidMass(50000000000, vx, vy, x, y, tick_time))
  42. for i in range(0, 50000):
  43. for a in range(0, num_stars):
  44. for b in range(0, num_stars):
  45. if not a == b and abs(stars[a].x - stars[b].x) > 0.5 and abs(stars[a].y - stars[b].y) > 0.5:
  46. stars[a].gravity(stars[b])
  47. for a in range(0, num_stars):
  48. stars[a].process_tick()
  49. storePixels(str(i) + ".png", stars)
  50. if i % 10 == 0:
  51. draw = ImageDraw.Draw(outImg)
  52. draw.text((0, 0),"t=" + str(i * tick_time) + "s", (255,255,255))
  53. outImg.save("img/" + str(i // 10) + ".png")
  54. outImg = Image.new("RGB", [1000, 1000])
  55. outImg.save("final.png")