Exp 9
Exp 9
PROCESSING
Date 19.03.2025
1. Create a Scalable Geometric Pattern – Develop a program that draws a geometric pattern
that increases in size dynamically while maintaining proportionality.
AIM: To create graphical designs, shapes, and artistic patterns using Python's Turtle
ALGORITHMS:
Step1: Import Required Modules
● Import the turtle module for graphics.
● Import math if needed for calculations.
Step2: Initialize the Screen and Turtle
● Create a turtle.Screen() window and set its size and title.
● Initialize a turtle.Turtle() object and set its speed.
Step3: Define Function to Draw the Pattern
● Clear the screen to remove previous drawings.
● Move the turtle to the starting position.
● Set the number of sides (e.g., hexagon with 6 sides).
● Loop through each side:
o Move forward by a scaled length.
o Turn by an appropriate angle.
● Optionally, draw concentric hexagons by looping multiple times.
Step4: Define Functions to Scale the Pattern
● Increase Size:
o Increase the scaling factor.
o Redraw the pattern with the updated size.
● Decrease Size:
o Ensure the size does not go below a minimum limit.
o Decrease the scaling factor.
o Redraw the pattern.
Step5: Set Up Keyboard Controls
● Listen for key presses using screen.onkey().
● Assign the "Up" arrow key to increase size.
● Assign the "Down" arrow key to decrease size.
Step6: Run the Program
● Call turtle.tracer(0, 0) for smooth rendering.
● Draw the initial pattern.
● Enable key listenin
SOURCE CODE:
import turtle
import math
screen = turtle.Screen()
screen.title("Scalable Geometric Pattern")
screen.setup(width=600, height=600)
pen = turtle.Turtle()
pen.speed(0)
def draw_pattern(scale=1):
pen.clear()
pen.penup()
pen.goto(-50 * scale, 0)
pen.pendown()
num_sides = 6
radius = 50 * scale
angle = 360 / num_sides
for _ in range(num_sides):
pen.forward(radius)
pen.right(angle)
for i in range(1, 4):
pen.penup()
pen.goto(-50 * scale * i, 0)
pen.pendown()
for _ in range(num_sides):
pen.forward(radius * i)
pen.right(angle)
def increase_size():
global scale_factor
scale_factor += 0.2
draw_pattern(scale_factor)
def decrease_size():
global scale_factor
if scale_factor > 0.4:
scale_factor -= 0.2
draw_pattern(scale_factor)
turtle.tracer(0, 0)
scale_factor = 1
draw_pattern(scale_factor)
turtle.tracer(1, 1)
screen.listen()
screen.onkey(increase_size, "Up")
screen.onkey(decrease_size, "Down")
screen.mainloop()
OUTPUT:
RESULT: The program has been executed successfully.
2. Design a Rotating Shape Animation – Write a program to draw a polygon and rotate it
incrementally around a central point to form a circular pattern.
AIM: To create graphical designs, shapes, and artistic patterns using Python's Turtle
ALGORITHMS :
Step1: Import Required Modules
● Import the turtle module for graphics.
● Import math if needed for calculations.
Step2: Initialize the Screen and Turtle
● Create a turtle.Screen() window and set its size and title.
● Initialize a turtle.Turtle() object and set its speed.
● Hide the turtle for a smoother animation.
Step3: Define Function to Draw a Polygon
● Move the turtle to the starting position.
● Set the number of sides for the polygon (e.g., hexagon with 6 sides).
● Calculate the exterior angle (360 / sides).
● Rotate the shape by an angle_offset to achieve the animation effect.
● Draw the polygon by looping through its sides.
● Reset the rotation angle after drawing.
Step4: Define Animation Function
● Clear the previous frame to avoid overlapping shapes.
● Call the draw_polygon() function with an incrementing rotation angle.
● Increase the rotation angle gradually for smooth motion.
● Use screen.ontimer(animate, delay) to repeatedly call the function for continuous animation.
Step5: Start the Animation
● Set the initial rotation angle to 0.
● Call the animate() function to begin the rotation.
● Use screen.mainloop() to keep the window open and responsive.
SOURCE CODE:
import turtle
import math
screen = turtle.Screen()
screen.title("Rotating Shape Animation")
screen.setup(width=600, height=600)
pen = turtle.Turtle()
pen.speed(0)
pen.hideturtle()
OUTPUT:
AIM: To create graphical designs, shapes, and artistic patterns using Python's Turtle.
ALGORITHMS:
Step1: Import Required Module
● Import the turtle module for graphical drawing.
Step2: Initialize the Turtle Screen
● Create a turtle.Screen() window.
● Set the window title to "Recursive Spiral Design".
Step3: Initialize the Turtle
● Create a turtle.Turtle() object.
● Set the drawing speed to the fastest mode (speed(0)).
● Set the turtle’s color (e.g., blue).
Step4: Define a Recursive Function (draw_spiral)
● Base Case: If the recursion depth reaches zero, stop drawing and return.
● Recursive Step:
o Move the turtle forward by the given size.
o Turn the turtle by a fixed angle.
o Call draw_spiral again with an increased size and reduced depth.
Step5: Call the Recursive Function
● Start with an initial size, angle, and recursion depth.
● Example: draw_spiral(10, 30, 100), where:
o 10 is the starting size of each step.
o 30 is the turning angle.
o 100 is the number of recursive steps.
Step6: Keep the Window Open
● Use screen.mainloop() to ensure the window remains open after execution.
SOURCE CODE:
import turtle
screen = turtle.Screen()
screen.title("Recursive Spiral Design")
pen = turtle.Turtle()
pen.speed(0)
pen.color("blue")
def draw_spiral(size, angle, depth):
if depth == 0:
return
pen.forward(size)
pen.right(angle)
draw_spiral(size + 2, angle, depth - 1)
draw_spiral(10, 30, 100)
screen.mainloop()
OUTPUT:
4. Simulate a Color Filter Effect – Develop a program that draws multiple overlapping shapes
with varying colors and opacities to create a blended visual effect.
AIM: To create graphical designs, shapes, and artistic patterns using Python's Turtle.
ALGORITHMS:
SOURCE CODE:
import turtle
import random
screen = turtle.Screen()
screen.title("Color Filter Effect")
screen.bgcolor("black")
pen = turtle.Turtle()
pen.speed(0)
pen.hideturtle()
colors = ["red", "blue", "green", "yellow", "purple", "orange", "cyan", "magenta"]
OUTPUT:
RESULT: The program has been executed successfully.
AIM: To create graphical designs, shapes, and artistic patterns using Python's Turtle.
ALGORITHMS:
Step1: Import Required Modules
● Import turtle for graphics.
● Import math if needed for calculations.
Step2: Initialize the Turtle Screen
● Create a turtle.Screen() window.
● Set the background color (e.g., black).
● Set the window title as "Multi-Layered Artistic Design".
Step3: Initialize the Turtle
● Create a turtle.Turtle() object.
● Set the drawing speed to the fastest (speed(0)).
● Hide the turtle for better visual effects.
● Set the turtle’s color (e.g., white).
Step4: Define a Recursive Function (draw_pattern)
● Base Case: If the recursion depth reaches zero, stop drawing and return.
● Recursive Step:
o Loop through a symmetric shape (e.g., hexagon with 6 sides).
o Move forward by the given size.
o Recursively call draw_pattern() with a reduced size and decreased recursion depth.
o Move back to the starting position.
o Rotate the turtle for the next segment (e.g., by 60 degrees for hexagonal symmetry).
Step5: Define Function to Create the Overall Design (create_design)
● Move the turtle to an appropriate starting position.
● Call draw_pattern() with an initial size and recursion depth (e.g., draw_pattern(100, 4)).
Step6: Call the create_design Function to Start Drawing
● Generate the multi-layered pattern dynamically.
Step7: Keep the Window Open
● Use screen.mainloop() to ensure the drawing window remains open.
SOURCE CODE:
import turtle
import math
screen = turtle.Screen()
screen.title("Multi-Layered Artistic Design")
screen.bgcolor("black")
pen = turtle.Turtle()
pen.speed(0)
pen.color("white")
pen.hideturtle()
def create_design():
pen.penup()
pen.goto(0, -200)
pen.pendown()
draw_pattern(100, 4)
create_design()
screen.mainloop()
OUTPUT: