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!
Learn More;
1 Comment
Drawing Panda with Python » StudyMuch · October 1, 2023 at 5:51 am
[…] Draw Rangoli Design with Python. […]