Draw a Sheep With a Cloud and a Circle
Let's take a look at the the following unfilled cloud film. As you tin see, we drew the cloud merely past drawing many arcs of different sizes and extent.
The problem is how we can draw this arcs to form the shape of a cloud (You tin effort randomly draw these arcs to see what shape it forms). The solution is to use an oval shape to command the end points of these arcs. Every bit you can see in the following picture show, the arcs starts and ends on the points of an oval shape.
Hither is the full general idea of the algorithm for drawing the cloud shape:
1. Create an oval shape and put the coordinates of points on the oval into a list.
2. Starting from a point in the list, randomly skip certain number of points and stop on another point.
3. Use ii points from step 2, make up one's mind the extent of arc randomly and depict the arc.
4. Repeat 2,3 until all the points on the oval is covered.
Now let'due south get into the details:
Creating the Oval
We tin use 2 halves of ellipses to draw the oval shape. Considering clouds have flatter bottom than the height, nosotros should make peak ellipse is taller than the lesser ane. Both of them should accept the aforementioned width. The easiest style to create this shape is to utilize parametric equations for ellipse. The post-obit is the code snippet:
n = 500 # number of points on each ellipse # X,Y is the center of ellipse, a is radius on x-axis, b is radius on y-axis # ts is the starting bending of the ellipse, te is the ending angle of the ellipse # P is the list of coordinates of the points on the ellipse def ellipse(X,Y,a,b,ts,te,P): t = ts turtle.up() for i in range(n): ten = a*math.cos(t) y = b*math.sin(t) P.append((ten+X,y+Y)) turtle.goto(10+10,y+Y) turtle.down() t += (te-ts)/(n-1) P = [] # starting from empty list ellipse(0,0,300,200,0,math.pi,P) # taller top one-half ellipse(0,0,300,50,math.pi,math.pi*2,P) # shorter lesser half
Cartoon an Random Arc
The side by side problem we need to solve is: how tin we draw an arc given 2 points and extent? We demand to effigy out the radius and the initial heading for drawing the circle. This requires some geometry work details of which will be skipped for this tutorial. The following lawmaking snippet for this part:
# computes Euclidean altitude between p1 and p2 def dist(p1,p2): return ((p1[0]-p2[0])**2 + (p1[1]-p2[one])**2)**0.5 # draws an arc from p1 to p2 with extent value ext def draw_arc(p1,p2,ext): turtle.up() turtle.goto(p1) turtle.seth(turtle.towards(p2)) a = turtle.heading() b = 360-ext c = (180-b)/2 d = a-c due east = d-90 r = dist(p1,p2)/2/math.sin(math.radians(b/2)) # r is the radius of the arc turtle.seth(e) # e is initial heading of the circumvolve turtle.down() turtle.circle(r,ext,100) return (turtle.xcor(),turtle.ycor()) # returns the landing position of the circle # this position should be extremely close to p2 only may not be exactly the same # return this for continuous drawing to the side by side point draw_arc((0,0), (-100,200), 150)
Drawing the Cloud
With previous two sections done, we are set to draw the deject. We randomly skip a number of points to get to the next point, randomly generate an extent, and draw the arc between these two points. This is to be repeated until all points are covered. At that place are a few details to address. The height part of cloud is more ragged than the bottom function. Therefore, we should give wider range for the extent and steps for random number generator. The post-obit is the consummate code for cartoon clouds:
import turtle import math import random screen = turtle.Screen() screen.setup(k,1000) screen.title("Random Deject - PythonTurtle.Academy") turtle.speed(0) turtle.hideturtle() turtle.up() turtle.bgcolor('dodger blueish') turtle.pencolor('white') turtle.pensize(2) n = 500 # number of points on each ellipse # X,Y is the middle of ellipse, a is radius on ten-axis, b is radius on y-axis # ts is the starting bending of the ellipse, te is the ending angle of the ellipse # P is the list of coordinates of the points on the ellipse def ellipse(X,Y,a,b,ts,te,P): t = ts for i in range(north): x = a*math.cos(t) y = b*math.sin(t) P.suspend((x+X,y+Y)) t += (te-ts)/(n-one) # computes Euclidean distance between p1 and p2 def dist(p1,p2): return ((p1[0]-p2[0])**2 + (p1[1]-p2[1])**ii)**0.five # draws an arc from p1 to p2 with extent value ext def draw_arc(p1,p2,ext): turtle.upwards() turtle.goto(p1) turtle.seth(turtle.towards(p2)) a = turtle.heading() b = 360-ext c = (180-b)/2 d = a-c e = d-90 r = dist(p1,p2)/2/math.sin(math.radians(b/ii)) # r is the radius of the arc turtle.seth(east) # e is initial heading of the circumvolve turtle.down() turtle.circle(r,ext,100) return (turtle.xcor(),turtle.ycor()) # returns the landing position of the circle # this position should be extremely shut to p2 but may not exist exactly the same # return this for continuous drawing to the next point def cloud(P): footstep = north//10 # draw about x arcs on top and lesser office of deject a = 0 # a is index of first betoken b = a + random.randint(step//2,footstep*2) # b is index of second indicate p1 = P[a] # p1 is the position of the start point p2 = P[b] # p2 is the position of the second bespeak turtle.fillcolor('white') turtle.begin_fill() p3 = draw_arc(p1,p2,random.uniform(70,180)) # draws the arc with random extention while b < len(P)-i: p1 = p3 # start from the end of the final arc if b < len(P)/2: # first half is tiptop, more ragged ext = random.uniform(70,180) b += random.randint(stride//ii,step*2) else: # 2nd one-half is bottom, more than smooth ext = random.uniform(30,70) b += random.randint(step,step*2) b = min(b,len(P)-1) # make sure to non skip past the concluding betoken p2 = P[b] # second point p3 = draw_arc(p1,p2,ext) # draws an arc and render the terminate position turtle.end_fill() P = [] # starting from empty list ellipse(0,0,300,200,0,math.pi,P) # taller top one-half ellipse(0,0,300,50,math.pi,math.pi*two,P) # shorter bottom half cloud(P)
Related Projects:
Random Cloud Generator
Random Mount Generator
Random City Skyline Generator
Random Island Generator
Source: https://pythonturtle.academy/tutorial-drawing-clouds-with-python-turtle/
0 Response to "Draw a Sheep With a Cloud and a Circle"
Post a Comment