Advertisement
uopspop

Untitled

Oct 9th, 2018
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.40 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Mon Oct  1 23:57:09 2018
  4.  
  5. What to learn in this sample:
  6.  
  7. Encapsulation:
  8.    Wrapping a piece of code up in a function is called encapsulation. One of the benefits of
  9.    encapsulation is that it attaches a name to the code, which serves as a kind of documentation.
  10.  
  11. Generalization:
  12.    make a attribute/behavior more general by adding parameters in a function
  13.  
  14. Refactoring:
  15.    The process in which you rearrange a program in order to improve the following two things:
  16.        * interfaces
  17.        * code re-use
  18.  
  19. @author: sam
  20. """
  21. import turtle
  22. import math
  23.  
  24. # for both polygon and arc
  25. # Refactoring: extract the common parts from polygon(..) and arc(..)
  26. def polyline(t, len, sides, angle):
  27.     # we have a number of sides, but it might not be complete to form a polygon
  28.     # for each side, we know how long it should be by len
  29.     # and we know after each side, how many angle we should turn by angle
  30.     for side in range(sides):
  31.         t.lt(angle) # improvement: to let the circle turn first
  32.         t.fd(len)
  33.        
  34.  
  35. # draw a polygon with flexible side and length
  36. # generalization for sides_arc sides and length
  37. def polygon(t, len, sides):
  38.     angle_for_each_side = 360.0 / sides # to see how many angle is assigned to each side
  39.     polyline(t, len, sides, angle_for_each_side)
  40.  
  41. # draw a square with a fixed length
  42. def square(t):
  43.     square(t, 50)
  44.    
  45. # draw a square with flexible length
  46. # generalization for len
  47. def square(t, len):
  48.     polygon(t, len, 4)
  49.  
  50. # draw an arc
  51. def arc(t, r, angle):
  52.     circumference = 2*math.pi*r
  53.     arc_length = circumference * (angle/360)
  54.     sides = int(arc_length/3) + 3 # get an approximate sides according to the arc_length
  55.     len = arc_length/sides
  56.     angle_for_each_side = angle / sides
  57.     polyline(t, len, sides, angle_for_each_side)
  58.  
  59. # draw an approximate circle
  60. def circle(t, r):
  61.     arc(t, r, 360)
  62.  
  63.  
  64.    
  65.  
  66. """
  67. Use cases
  68. """
  69.  
  70. bob = turtle.Turtle()
  71.  
  72. # draw a square use a turtle
  73. # square(bob)
  74.  
  75. # draw a square use a turtle with a specific length
  76. #square(bob, 10)
  77. #square(bob, 150)
  78.  
  79. # draw a n-sided polygon
  80. # polygon(bob, 100, 6)
  81.  
  82. # draw an approximate circle
  83. circle(bob, 100)
  84.  
  85. # draw an arc
  86. # using keyword arguments
  87. # arc(bob,r=25,angle=270)
  88. # arc(bob,r=75,angle=270)
  89.  
  90. # wait for the user to close the window
  91. turtle.mainloop()
  92.  
  93.  
  94. # bob.pd() # ???
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement