Study Smart, Not Hard. - StudyMuch

Draw Rangoli Design Using Python

Draw Rangoli Design Using Python

Rangoli is a traditional Indian art form that involves creating intricate and colorful patterns on the ground, typically during festivals and special occasions. These designs are not only visually stunning but also hold cultural significance. In this blog post, we’ll explore how to create a simple yet beautiful Rangoli design using Python (Draw Rangoli Design Using Python) and the Turtle graphics library.

Prerequisites

Before we get into the code, make sure you have Python and the Turtle graphics library installed on your system. You can install Turtle Graphics using pip:

`pip install PythonTurtle`

The Python Code;

```import turtle as tur Â  Â  Â  Â  Â # Import the Turtle graphics library as 'tur'
import colorsys as cs Â  Â  Â  Â  # Import the colorsys module as 'cs'

tur.setup(800, 800)
tur.speed(0)
tur.tracer(10)
tur.width(2) Â  Â  Â  Â  Â  Â  Â  Â  # Set the pen width to 2 pixels
tur.bgcolor("black")

# Nested loops for creating the pattern
for j in range(25): Â  Â  Â  Â  Â # Outer loop with 25 iterations
Â  for i in range(15): Â  Â  Â # Inner loop with 15 iterations
Â  Â  Â  # Calculate and set the pen color based on the hue (color variation
Â  Â  Â  tur.color(cs.hsv_to_rgb(i /15, 1, 1))
Â  Â  Â  # Right turn and draw a quarter of a circle with a decreasing radius
Â  Â  Â  tur.right(90)
Â  Â  Â  tur.circle(200 - j * 4, 90)
Â  Â  Â  # Left turn and draw another quarter of a circle
Â  Â  Â  tur.left(90)
Â  Â  Â  tur.circle(200 - j * 4, 90)
Â  Â  Â  # Right turn and draw a small circle (a part of the pattern)
Â  Â  Â  tur.right(180)
Â  Â  Â  tur.circle(50, 24)
tur.hideturtle() Â  Â  Â  Â  Â  Â  # Hide the Turtle cursor
tur.done() Â  Â  Â  Â  Â  Â  Â  Â  Â  # Finish drawing
```

Code Description

Let’s break down the code step by step:

• We import the Turtle graphics library as tur and the colorsys module
as cs.
• The tur.setup(800, 800)
function initializes a Turtle graphics window with dimensions 800×800 pixels.
• tur.speed(0)
sets the drawing speed to the maximum (fastest).
• tur.tracer(10)
enables automatic screen updates after every 10 iterations, which makes the drawing smoother.
• tur.width(2)
sets the pen width to 2 pixels.
• tur.bgcolor(“black”)
sets the background color of the Turtle graphics window to black.

The code then enters nested loops:

• The outer loop (for j in range(25))
runs 25 times.
• The inner loop (for i in range(15))
runs 15 times.
• Inside the inner loop, the code:
• Calculates the pen color based on the hue using cs.hsv_to_rgb().
• Makes a right turn, draws a quarter of a circle with a decreasing radius.
• Makes a left turn, draws another quarter of a circle.
• Makes a right turn again, and draws a small circle, creating part of the pattern.
• After completing the nested loops, we hide the Turtle cursor using tur.hideturtle().
• Finally, we finish drawing with tur.done().

When you run this code, it will create a mesmerizing and colorful geometric pattern on the Turtle graphics window, with each iteration of the inner loop changing the pen color based on the hue value.

Running the Code

To run this code, simply copy and paste it into a Python script or IDE, and execute it. You should see a colorful Rangoli design appear on the Turtle graphics window.

Feel free to experiment with different values of n and customize the colors to create your own unique Rangoli designs. This simple Python script allows you to explore the beauty of traditional Indian art through code. Enjoy creating your Rangoli designs!