Advertisement
here2share

# t_random_cloud.py

Aug 5th, 2020
1,285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.71 KB | None | 0 0
  1. # t_random_cloud.py
  2.  
  3. import turtle
  4. import math
  5. import random
  6.  
  7. screen = turtle.Screen()
  8. screen.setup(800,600)
  9. screen.title("Random Cloud")
  10.  
  11. turtle.speed(0)
  12. turtle.hideturtle()
  13. turtle.up()
  14. turtle.bgcolor('dodger blue')
  15. turtle.pencolor('white')
  16. turtle.pensize(2)
  17.  
  18. n = 500 # number of points on each ellipse
  19. # X,Y is the center of ellipse, a is radius on x-axis, b is radius on y-axis
  20. # ts is the starting angle of the ellipse, te is the ending angle of the ellipse
  21. # P is the list of coordinates of the points on the ellipse
  22. def ellipse(X,Y,a,b,ts,te,P):
  23.     t = ts
  24.     for i in range(n):
  25.         x = a*math.cos(t)
  26.         y = b*math.sin(t)
  27.         P.append((x+X,y+Y))
  28.         t += (te-ts)/(n-1)
  29.  
  30. # computes Euclidean distance between p1 and p2
  31. def dist(p1,p2):
  32.     return ((p1[0]-p2[0])**2 + (p1[1]-p2[1])**2)**0.5
  33.  
  34. # draws an arc from p1 to p2 with extent value ext
  35. def draw_arc(p1,p2,ext):
  36.     turtle.up()
  37.     turtle.goto(p1)
  38.     turtle.seth(turtle.towards(p2))
  39.     a = turtle.heading()
  40.     b = 360-ext
  41.     c = (180-b)/2
  42.     d = a-c
  43.     e = d-90
  44.     r = dist(p1,p2)/2/math.sin(math.radians(b/2)) # r is the radius of the arc
  45.     turtle.seth(e) # e is initial heading of the circle
  46.     turtle.down()
  47.     turtle.circle(r,ext,100)
  48.     return (turtle.xcor(),turtle.ycor()) # returns the landing position of the circle
  49.                                          # this position should be extremely close to p2 but may not be exactly the same
  50.                                          # return this for continuous drawing to the next point
  51.  
  52.  
  53. def cloud(P):
  54.     step = n//10 # draw about 10 arcs on top and bottom part of cloud
  55.     a = 0 # a is index of first point
  56.     b = a + random.randint(step//2,step*2) # b is index of second point
  57.     p1 = P[a] # p1 is the position of the first point
  58.     p2 = P[b] # p2 is the position of the second point
  59.     turtle.fillcolor('white')
  60.     turtle.begin_fill()
  61.     p3 = draw_arc(p1,p2,random.uniform(70,180)) # draws the arc with random extention
  62.     while b < len(P)-1:
  63.         p1 = p3 # start from the end of the last arc
  64.         if b < len(P)/2: # first half is top, more ragged
  65.             ext = random.uniform(70,180)
  66.             b += random.randint(step//2,step*2)
  67.         else: # second half is bottom, more smooth
  68.             ext = random.uniform(30,70)
  69.             b += random.randint(step,step*2)
  70.         b = min(b,len(P)-1) # make sure to not skip past the last point
  71.         p2 = P[b] # second point
  72.         p3 = draw_arc(p1,p2,ext) # draws an arc and return the end position
  73.     turtle.end_fill()
  74.  
  75. P = [] # starting from empty list
  76. ellipse(0,0,300,200,0,math.pi,P) # taller top half
  77. ellipse(0,0,300,50,math.pi,math.pi*2,P) # shorter bottom half
  78. cloud(P)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement