Advertisement
Guest User

Multiplication Curves

a guest
Oct 17th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.49 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. '''
  4. multiplication curves
  5.  
  6. inspired by:
  7. http://superdecade.blogspot.co.uk/2017/12/advent-calendar-for-geeks-21.html
  8. https://youtu.be/qhbuKbxJsk8
  9.  
  10. James Edmundson 12/2017
  11. james-edmundson.com
  12. '''
  13.  
  14. import turtle  as t
  15. import math
  16. import time
  17. import datetime
  18. import random
  19. import sys
  20.  
  21. numberOfPoints=300  #number of points
  22. radiusOfCircle=400   #radius of circle
  23.  
  24. def polToCart(radiusOfCircle,theta):                   #function to convert a polar coordinate to a
  25.     coord=[]
  26.     coord.append(radiusOfCircle*math.cos(math.radians(theta)))    #cartesian coordinate that turtle can follow
  27.     coord.append(radiusOfCircle*math.sin(math.radians(theta)))
  28.     return coord    #returns the coordinate in cartesian form
  29.  
  30. def loopMult(numberOfPoints,d,radiusOfCircle,multupto):
  31.     for i in range(multupto):
  32.         specific(numberOfPoints,d,radiusOfCircle,i) #draws each multiplication curve up to the value specified
  33.         time.sleep(0.7)
  34.         t.clear()
  35.  
  36. def specific(numberOfPoints,d,radiusOfCircle,specificMultiple):
  37.     t.tracer(0,0)               #for speeding up turtle graphics
  38.     for i in range(numberOfPoints):          #for each point on the circumference of the cirle
  39.         point=(specificMultiple*i)%numberOfPoints                  #determins which point the line should go to
  40.         t.goto(polToCart(radiusOfCircle,i*d))  #goes to the next point
  41.         t.pd()
  42.         t.goto(polToCart(radiusOfCircle,point*d))  #goes to the new point
  43.         t.pu()
  44.     t.update() #updates turtle window, so all lines are drawn
  45.  
  46. def screenCap():
  47.     ts = t.getscreen()
  48.     ts.getcanvas().postscript(file="{}.eps".format(datetime.datetime.now().strftime("%Y-%m-%d%H:%M"))) # saves the turtle screen as the date and time.eps
  49.  
  50. def init(radiusOfCircle): #changing turtle settings ready for drawing
  51.     t.bgcolor("Black")  #changing the background colour to black
  52.     t.color("Red")      #turtle pen colour to red
  53.     t.ht()              #hiding the turtle character itself
  54.     t.penup()           #pen up
  55.     t.goto(polToCart(radiusOfCircle, 0)) #going to the first point on the circle
  56.     t.pd()              #pen down
  57.  
  58. def main(numberOfPoints=numberOfPoints, radiusOfCircle=radiusOfCircle): #defaults to global variables, but can be set as otherwise
  59.     menuOption = input("> ")
  60.     if menuOption == "s": #specific curve drawn
  61.         try:
  62.             multiple = int(input("multiple: ")) #getting the multiple
  63.             init(radiusOfCircle)                #making turtle pretty
  64.             specific(numberOfPoints, 360/numberOfPoints, radiusOfCircle, multiple) #drawing the specific curve
  65.         except ValueError:  #if the user's naughty
  66.             print("Multiple needs to be an int!")
  67.     elif menuOption == "l": #drawing many curves in a loop
  68.         try:
  69.             upto = int(input("Loop up to: "))
  70.             init(radiusOfCircle)
  71.             loopMult(numberOfPoints, 360/numberOfPoints, radiusOfCircle, upto)
  72.         except ValueError:
  73.             print("Multiple needs to be an int!")
  74.     elif menuOption == "c": #clears turtle screen
  75.         t.clear()
  76.     elif menuOption == "p": #takes a screen grab of the turtle window
  77.         screenCap()
  78.     elif menuOption == "h" or menuOption == "help": #prints help commands
  79.         print("s for specific pattern\nl for looping multiples\nc to clear turtle\np to take a picture\nh for this help\nq to quit")
  80.     elif menuOption == "q": #quit
  81.         sys.exit()
  82.     else:
  83.         print("NOT RECOGNISED")
  84.  
  85. if __name__ == '__main__':
  86.     while True:
  87.         main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement