Python Turtle Code:: Triangle with color

Python Turtle Code:: Triangle with color

Drawing a Sierpinski triangle using GUI/Turtle.

Write a program that draws Sierpinski triangle using recursive methods as shown below:

The three points of the triangle are passed to invoke displayTriangles. If order == 0, the displayTriangles (order, p1, p2, p3) function displays a triangle that connects three points p1, p2, and p3, as shown in Figure a. Otherwise, it performs the following tasks:

  1. Obtains a midpoint between p1 and p2), a midpoint between p2 and p3, and a midpoint between p3 and p1, as shown in Figure b.
  2. Recursively invokes displayTriangles with a reduced order to display three smaller Sierpinski triangles. Note that each small Sierpinski triangle is structurally identical to the original big Sierpinski triangle except that the order of a small

triangle is one less, as shown in Figure b.

Drawing a Sierpinski triangle spawns calls to draw three small Sierpinski triangles recursively.

Once done, rewrite the programs for filled Sierpinski triangles.

The Correct Answer and Explanation is :

he Sierpinski Triangle is a fractal pattern formed by recursively subdividing an equilateral triangle into smaller equilateral triangles.his process results in a self-similar structure, where each smaller triangle mirrors the overall shape.mplementing this pattern in Python using the Turtle graphics library allows for a visual demonstration of recursion and fractal geometry.
Understanding the Recursive Process:

. Base Case (Order 0): Draw a single equilateral triangle.. Recursive Case (Order > 0): Divide the current triangle into three smaller triangles by connecting the midpoints of its sides. Recursively apply the same process to each of the three smaller triangles.
Python Turtle Implementation:

“python
import turtle

def draw_triangle(points, color, my_turtle):
“””Draws a filled triangle with the given points and color.”””
my_turtle.fillcolor(color)
my_turtle.up()
my_turtle.goto(points[0][0], points[0][1])
my_turtle.down()
my_turtle.begin_fill()
my_turtle.goto(points[1][0], points[1][1])
my_turtle.goto(points[2][0], points[2][1])
my_turtle.goto(points[0][0], points[0][1])
my_turtle.end_fill()

def get_mid(p1, p2):
“””Calculates the midpoint between two points.”””
return ((p1[0] + p2[0]) / 2, (p1[1] + p2[1]) / 2)

def sierpinski(points, degree, my_turtle):
“””Recursively draws the Sierpinski triangle.”””
colormap = [‘blue’, ‘red’, ‘green’, ‘white’, ‘yellow’, ‘violet’, ‘orange’]
draw_triangle(points, colormap[degree], my_turtle)
if degree > 0:
sierpinski([points[0],
get_mid(points[0], points[1]),
get_mid(points[0], points[2])],
degree-1, my_turtle)
sierpinski([points[1],
get_mid(points[0], points[1]),
get_mid(points[1], points[2])],
degree-1, my_turtle)
sierpinski([points[2],
get_mid(points[2], points[1]),
get_mid(points[0], points[2])],
degree-1, my_turtle)

def main():
“””Sets up the Turtle environment and starts the drawing.”””
my_turtle = turtle.Turtle()
my_win = turtle.Screen()
my_points = [[-100, -50], [0, 100], [100, -50]]
sierpinski(my_points, 3, my_turtle)
my_win.exitonclick()

if name == ‘main‘:
main()

**Explanation of the Code:**

. **`draw_triangle` Function:** This function takes three points and a color as input, moves the turtle to the first point, and draws a filled triangle connecting all three points.. **`get_mid` Function:** Given two points, this function calculates and returns their midpoint, which is essential for subdividing the triangle.. **`sierpinski` Function:** This is the core recursive function. It draws a triangle and, if the recursion depth (`degree`) is greater than zero, it calculates the midpoints of the triangle's sides and recursively draws three smaller triangles at each corner. The `colormap` list assigns different colors to triangles at different recursion levels, enhancing the visual appeal.. **`main` Function:** Sets up the Turtle graphics environment, defines the initial points of the large equilateral triangle, and starts the recursive drawing process.
**Running the Program:**

o execute this program, ensure you have Python installed on your system. Save the code in a file named `sierpinski.py` and run it using the command:
``bash
python sierpinski.py

his will open a window displaying the Sierpinski Triangle with a depth of 3. You can adjust the depth by changing the second argument in the sierpinski function call within the main function.
Visual Demonstration:

or a visual demonstration of drawing a Sierpinski Triangle using Python Turtle, you can refer to the following video:
videoDrawing a Sierpinski Triangle with Python Turtleturn0search4

his video provides a step-by-step guide to creating the Sierpinski Triangle, which can be helpful for understanding the process and visualizing the outcome.

Scroll to Top