Guest User

Untitled

a guest
Oct 16th, 2018
4,038
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. import cairo
  2. import math
  3.  
  4. FRAMECOUNT = 100
  5.  
  6. #############################
  7.  
  8. class Shade():
  9. def __init__(self, radius, centerpoint):
  10. self.xCord, self.yCord = centerpoint
  11. self.radius = radius
  12. self.center = centerpoint
  13.  
  14. def updatePos(self, newX, newY):
  15. self.xCord = newX
  16. self.yCord = newY
  17.  
  18. def receiveCord(self, cords):
  19. x, y = cords
  20. self.updatePos(x, y)
  21.  
  22. def drawSelf(self, context, fill):
  23. context.set_source_rgb(1,1,1)
  24. context.arc(self.xCord, self.yCord, self.radius, 0, 2*math.pi)
  25. if fill == True:
  26. context.fill()
  27. else:
  28. context.stroke()
  29.  
  30.  
  31.  
  32.  
  33. class Positions():
  34. def __init__(self, frameCount, radius, center, phase):
  35.  
  36. # get radians of slices depending on frame count
  37. self.radian = 2*math.pi/frameCount
  38. self.slices = []
  39. for x in range(frameCount):
  40. currentSlice = self.radian * x
  41. xVal = math.cos(currentSlice)
  42. yVal = math.sin(currentSlice)
  43. self.slices.append((xVal,yVal))
  44. continue
  45. # scale those coordinates by the radius
  46. self.storedCords = []
  47. self.centerpoint = center
  48. for each in self.slices:
  49. xVal = (each[0]*radius)
  50. yVal = (each[1]*radius)
  51. self.storedCords.append((xVal,yVal))
  52. continue
  53. # translate to the correct portion
  54. xCent, yCent = center
  55. for i in range(len(self.storedCords)):
  56. tempX = self.storedCords[i][0] + xCent
  57. tempY = self.storedCords[i][1] + yCent
  58. self.storedCords[i] = (tempX, tempY)
  59. continue
  60. # start orbit in whichever phase
  61. self.finalCords = self.storedCords[phase:]
  62. for x in self.storedCords[:phase]:
  63. self.finalCords.append(x)
  64.  
  65.  
  66. ###################################################
  67.  
  68. surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 1000, 1000)
  69. ctx = cairo.Context (surface)
  70.  
  71. frames = FRAMECOUNT
  72.  
  73. # initial positions dont really matter except for you
  74. blackhole = Shade(125, (500,500))
  75. planet = Shade(25, (800, 500))
  76. moon = Shade(5, (750,500))
  77.  
  78. #find planet's orbit
  79. planetPos = Positions(FRAMECOUNT, 300, (500, 500), 0)
  80.  
  81.  
  82.  
  83. #animation
  84. for x in range(frames):
  85. # Blank screen
  86. ctx.set_source_rgb(0, 0, 0)
  87. ctx.rectangle(0, 0, 1000, 1000)
  88. ctx.fill()
  89.  
  90. #update planet position
  91. planet.receiveCord(planetPos.finalCords[x])
  92. moonPos = Positions(FRAMECOUNT, 50, planetPos.finalCords[x], x)
  93. moon.receiveCord(moonPos.finalCords[x])
  94.  
  95.  
  96. #draw planets
  97. blackhole.drawSelf(ctx, False)
  98. planet.drawSelf(ctx, True)
  99. moon.drawSelf(ctx, False)
  100.  
  101.  
  102. #draw
  103. ctx.stroke()
  104. print ('Creating Frame')
  105. surface.write_to_png('Frame' + str(x)+'.png')
  106.  
  107. print ('Finished')
Advertisement
Add Comment
Please, Sign In to add comment