Guest User

Untitled

a guest
Oct 16th, 2018
966
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.34 KB | None | 0 0
  1. import cairo
  2. import math
  3.  
  4. FRAMECOUNT = 10
  5.  
  6. #############################
  7.  
  8.  
  9. #The particle or planets or whatever are called shades in the code.
  10. class Shade():
  11.     #Initialize shade with a radius and centerpoint. Tuple expected by arg centerpoint.
  12.     #Centerpoint is unused in this code, but I left it in because it could be used later.
  13.     def __init__(self, radius, centerpoint):
  14.         self.xCord, self.yCord = centerpoint
  15.         self.radius = radius
  16.         self.center = centerpoint
  17.  
  18.     #This lets shade change its own position.
  19.     def updatePos(self, newX, newY):
  20.         self.xCord = newX
  21.         self.yCord = newY
  22.  
  23.     #This lets shade receive a tuple, unpack it, and update its position with it.
  24.     def receiveCord(self, cords):
  25.         x, y = cords
  26.         self.updatePos(x, y)
  27.  
  28.     #This lets shade draw itself to the context. We're assuming
  29.     #here that all shades are white, and want to be circles.
  30.     #But further upgrades could change that. True for filled, False for outline.
  31.     def drawSelf(self, context, fill):
  32.         context.set_source_rgb(1,1,1)
  33.         context.arc(self.xCord, self.yCord, self.radius, 0, 2*math.pi)
  34.         if fill == True:
  35.             context.fill()
  36.         else:
  37.             context.stroke()
  38.  
  39.  
  40.        
  41.  
  42. class Orbit():
  43.     def __init__(self, frameCount, radius, center, phase):
  44.         #Initializes Positions with the number of frames, a radius of
  45.         #the orbit, a center of the orbit, and a phase. The phase is where
  46.         #the planet starts in its orbit. At phase 0, the planet starts
  47.         #at the far right and moves clockwise. Currently no ability to
  48.         #make counter-clockwise.
  49.         #
  50.         #Takes the frame count and slices a circle into that many wedges,
  51.         #then finds the x and y coordinate along a circle a unit circle and adds it
  52.         #to a list.
  53.         self.radian = 2*math.pi/frameCount
  54.         self.slices = []
  55.         for x in range(frameCount):
  56.             currentSlice = self.radian * x
  57.             xVal = math.cos(currentSlice)
  58.             yVal = math.sin(currentSlice)
  59.             self.slices.append((xVal,yVal))
  60.         #Scales those coordinates by the radius. Creates a new list filled with
  61.         #tuples of the correctly scaled x, y coordinates.
  62.         self.storedCords = []
  63.         for each in self.slices:
  64.             xVal = (each[0]*radius)
  65.             yVal = (each[1]*radius)
  66.             self.storedCords.append((xVal,yVal))
  67.         #Translates those coordinates to the correct centerpoint inside the image.
  68.         #Goes through each coordinate and makes a new tuple with correct translation.
  69.         self.centerpoint = center
  70.         xCent, yCent = center
  71.         for i in range(len(self.storedCords)):
  72.             tempX = self.storedCords[i][0] + xCent
  73.             tempY = self.storedCords[i][1] + yCent
  74.             self.storedCords[i] = (tempX, tempY)
  75.         # A phase of 0 means the planet starts on the right side.
  76.         # A phase of half your framecount starts it on the left side.
  77.         # Simple slice and dice.
  78.         self.finalCords = self.storedCords[phase:]
  79.         for x in self.storedCords[:phase]:
  80.             self.finalCords.append(x)
  81.        
  82.          
  83. ###################################################
  84.    
  85. surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 1000, 1000)
  86. ctx = cairo.Context (surface)
  87.  
  88. frames = FRAMECOUNT
  89.  
  90. # Initial positions of Shades don't matter to the program.
  91. blackhole = Shade(125, (500,500))
  92. planet = Shade(25, (800, 500))
  93. moon = Shade(5, (750,500))
  94.  
  95. # Find the planet's orbit around 500, 500, or our black hole.
  96. #And assign it to planetPos
  97. planetPos = Orbit(FRAMECOUNT, 300, (500, 500), 0)
  98.  
  99.  
  100.  
  101. #animation
  102. for x in range(frames):
  103.     # Blank the screen.
  104.     ctx.set_source_rgb(0, 0, 0)
  105.     ctx.rectangle(0, 0, 1000, 1000)
  106.     ctx.fill()
  107.  
  108.     # Update the positions. Recursion!
  109.     planet.receiveCord(planetPos.finalCords[x])
  110.     moonPos = Orbit(FRAMECOUNT, 50, planetPos.finalCords[x], x)
  111.     moon.receiveCord(moonPos.finalCords[x])
  112.    
  113.  
  114.     # Tell the planets to draw themselves.
  115.     blackhole.drawSelf(ctx, False)
  116.     planet.drawSelf(ctx, True)
  117.     moon.drawSelf(ctx, False)
  118.    
  119.  
  120.     # Draw the thing to a png, and iterate over however many frames.
  121.     ctx.stroke()
  122.     print ('Creating Frame')
  123.     surface.write_to_png('Frame' + str(x)+'.png')
  124.  
  125. print ('Finished')
Advertisement
Add Comment
Please, Sign In to add comment