Windmill Python Turtle program using Triangles
Windmill Python Turtle program using Triangles
The Python code should draw six triangles forming a windmill. The internal angles of each triangle are 30, 60 and 90 degrees. The length of the other two sides are given by the expressions
S * sin (π / 6)
S * cos (π / 6).
Python Program
#Python Turtle program forming a windmill
import turtle
import math
num_triangles = 6
# function to draw the stand vertical line
def drawStand():
t.right(180)
t.right(90)
t.forward(150)
t.right(90)
# function to draw triangle
def drawTriangle(S):
t.forward(S)
t.right(120)
t.forward(S*math.sin(math.pi/6))
t.right(90)
t.forward(S*math.cos(math.pi/6))
# main
S = input("Please enter side length:")
t = turtle.Turtle()
drawStand()
for n in range(num_triangles):
drawTriangle(int(S))
t.left(150)
turtle.mainloop()
Screenshot
Explanation
This Python program uses the turtle
module to draw a windmill-like structure composed of triangular blades connected to a vertical stand.
Importing Required Modules
import turtle
import math
– The turtle
module is used for drawing.
– The math
module is used for trigonometric calculations.
Defining Constants
num_triangles = 6
– The windmill consists of 6 triangular blades.
Function to Draw the Stand
def drawStand():
t.right(180)
t.right(90)
t.forward(150)
t.right(90)
– This function draws the vertical stand (pole) of the windmill.
Function to Draw a Triangle
def drawTriangle(S):
t.forward(S)
t.right(120)
t.forward(S * math.sin(math.pi/6))
t.right(90)
t.forward(S * math.cos(math.pi/6))
– This function draws a single triangular blade of the windmill using trigonometric calculations.
Main Execution
S = input("Please enter side length:")
t = turtle.Turtle()
– The program takes user input for the triangle side length and creates a turtle object.
Drawing the Windmill
drawStand()
for n in range(num_triangles):
drawTriangle(int(S))
t.left(150)
– First, the stand is drawn.
– Then, six triangles are drawn in a circular pattern.
– The turtle rotates 150 degrees after each triangle.
Keeping the Window Open
turtle.mainloop()
– This keeps the Turtle window open to display the windmill.