Advertisement
besmao

Python Hypotrochoid Generator

Dec 3rd, 2012
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.18 KB | None | 0 0
  1. #Hypotrochoid generator
  2.  
  3. import Image, ImageDraw
  4. import math
  5.  
  6. #Size of canvas to draw on
  7. #Higher resolution means thinner lines, but may need higher resolution below
  8. canvasX = 5760
  9. canvasY = 3240
  10.  
  11. #Size of final image
  12. imageX = 1920
  13. imageY = 1080
  14.  
  15. #Background color
  16. bgcolor = "black"
  17.  
  18. #Coordinates of center of image
  19. centerX = canvasX / 2
  20. centerY = canvasY / 2
  21.  
  22. resolution = 6000        #Number of points calculated and drawn per half rotation
  23. revolutions = 350        #Number of times the disc goes around the rim
  24. rimRadius = 1620.0       #Radius of rim/outer circle
  25. rimRadians = 0           #Keeps track of the rotations around the rim
  26.  
  27. disc1Radius = 630.0      #Radius of disc with pen holes in it
  28. disc1HoleRadius = 600.0  #Dist of hole from disc center, <= discRadius
  29. disc1inside = -1         #-1 if disc1 is inside rim, 1 if outside
  30. disc1rotSpeed = 1.0      #1.0 for normal, lower->slower, higher->faster
  31. disc1Radians = 0.0       #Keeps track of disc 1 rotation
  32.  
  33. useSecondDisc = True     #enaTruebles a second disc
  34. disc2Radius = 300.0      #Radius of disc with pen holes in it
  35. disc2HoleRadius = 120.0  #Dist of hole from disc center, < discRadius/2
  36. disc2inside = 1          #-1 if disc2 is inside disc1, 1 if outside
  37. disc2rotSpeed = 0.1      #1.0 for normal, lower for slower, higher for faster
  38. disc2Radians = 0.0       #Keeps track of disc 2 rotation
  39.  
  40. #Used for finding the center of the discs
  41. ratio1 = disc1Radius/rimRadius
  42. ratio2 = disc2Radius/disc1Radius
  43.  
  44.  
  45. #Color options, see colorMethod function below
  46. rColor = 2
  47. gColor = 2
  48. bColor = 1
  49.  
  50. def colorMethod(methodNum, x, y):
  51.     if methodNum == 0: #No color added
  52.         return 0
  53.     elif methodNum == 1: #Full color added
  54.         return 255
  55.     elif methodNum == 2: #Based on distance from center
  56.         distance = math.sqrt(((x - centerX)*(x - centerX))+\
  57.                              ((y - centerY)*(y - centerY)))
  58.         return int((distance/rimRadius)*255)%255
  59.     elif methodNum == 3: #Based on distance from center (reversed)
  60.         distance = math.sqrt(((x - centerX)*(x - centerX))+\
  61.                              ((y - centerY)*(y - centerY)))
  62.         return int(255-((distance/rimRadius)*255))%255
  63.     elif methodNum == 4: #Based on polar coordinates of draw point
  64.         if x - centerX == 0:
  65.             return 0
  66.         else:
  67.             temp = (math.atan((y-centerY)/(x-centerX))) / (math.pi*2)
  68.             temp = int(16 * temp * 255) % 510
  69.             if temp <= 255:
  70.                 return temp
  71.             else:
  72.                 return 510 - temp
  73.     elif methodNum == 5: #Based on rimRadians
  74.         temp = rimRadians / (math.pi*2)
  75.         temp = int(temp) % 510
  76.         if temp <= 255:
  77.             return temp
  78.         else:
  79.             return 510 - temp
  80.     elif methodNum == 6: #Based on disc1Radians
  81.         temp = disc1Radians / (math.pi*2)
  82.         temp = int(temp) % 510
  83.         if temp <= 255:
  84.             return temp
  85.         else:
  86.             return 510 - temp
  87.  
  88. #Open the image and prepare to draw to it
  89. print "Creating image..."
  90. myImage = Image.new("RGB", (canvasX,canvasY), bgcolor)
  91. draw = ImageDraw.Draw(myImage)
  92.  
  93. #Note: sin and cos may be switched to shift rotation of entire image slightly
  94. while rimRadians < (math.pi * 2 * revolutions):
  95.     #Coordinates for center of disc. Finds point of contact between
  96.     #disc and rim, then subtracts radius of disc
  97.     disc1CenterX = (1+(disc1inside*ratio1)) * rimRadius*math.sin(rimRadians)
  98.     disc1CenterY = (1+(disc1inside*ratio1)) * rimRadius*math.cos(rimRadians)
  99.  
  100.     #Finds radians disc1 has rotated
  101.     #Or if second disc used, find radians disc2 has rotated around disc1
  102.     disc1Radians = disc1rotSpeed * (rimRadians * rimRadius / disc1Radius)
  103.  
  104.     if not useSecondDisc: #Using one disc, like a normal spirograph
  105.         #Calculate where the pen hole is
  106.         drawPointX = disc1CenterX + disc1HoleRadius*math.sin(-disc1Radians)
  107.         drawPointY = disc1CenterY + disc1HoleRadius*math.cos(-disc1Radians)
  108.  
  109.     else: #Using a second disc rotating around the first disc
  110.         #Find the center of the center disc
  111.         disc2CenterX = disc1CenterX + ((1+(disc2inside*ratio2)) * \
  112.                                        disc2Radius*math.sin(disc1Radians))
  113.         disc2CenterY = disc1CenterY + ((1+(disc2inside*ratio2)) * \
  114.                                        disc2Radius*math.cos(disc1Radians))
  115.  
  116.         #Find radians disc2 has rotated
  117.         disc2Radians = disc2rotSpeed * (rimRadians * rimRadius / disc2Radius)
  118.  
  119.         #Calculate where the pen hole is
  120.         drawPointX = centerX + disc2CenterX + disc2HoleRadius*math.sin(-disc2Radians)
  121.         drawPointY = centerY + disc2CenterY + disc2HoleRadius*math.cos(-disc2Radians)
  122.  
  123.     #Draw the point
  124.     colorTuple = (colorMethod(rColor, drawPointX, drawPointY),\
  125.                   colorMethod(gColor, drawPointX, drawPointY),\
  126.                   colorMethod(bColor, drawPointX, drawPointY))
  127.     draw.point((drawPointX, drawPointY), fill=colorTuple)
  128.  
  129.     #Rotate around the rim
  130.     rimRadians += (math.pi/resolution)
  131.  
  132. #Stop drawing and save the image
  133. del draw
  134. myImage = myImage.resize((imageX,imageY), Image.ANTIALIAS)
  135. myImage.save("hypotrochoid.png", )
  136. print "Completed!"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement