Circles
(define canvas-y 500)
(start canvas-x canvas-y)
;; the following two functions increment the x-axis of both the circles
(define increment2
(lambda (colour incr control radius x y)
(draw-circle (make-posn x y) radius colour)
(if (> 100 control)
(increment2 colour incr (+ 1 control) (+ incr radius)x y))))
(define increment
(lambda (colour incr x y)
(increment2 colour incr 1 incr x y)))
;; background colour
(define clear-canvas
(lambda (colour)
(draw-solid-rect (make-posn 0 0) canvas-x canvas-y colour)))
;; main function - draws the circles
(define draw-circles
(lambda (x y)
(increment 'green 80 x 350)
(increment 'purple 80 y 150)
(if (> 500 x)
(draw-circles (+ x 1)(+ y 1)))))
(draw-circles 100 250)
Cross

# Provides the functionality to draw graphics
from turtle import forward, left, right, speed, \
goto, up, down, color, begin_fill, end_fill, setup
# Sets the window size
setup(width = 750, height = 750)
# Speeds up the drawing
speed('fastest')
background_colour = 'black'
#------------------------------------------------------------------------------------------
#
# Step 1 - Introduce the function signature
#
# draw_fractal(initial_state, target, replacement, num_replacements):
# 1. Takes a string 'initial_state' and replaces items defined in
# 'target' with the items defined in 'replacement'. It iterates
# through this proccess several times depending on the number stored
# in the 'num_replacements' variable.
#
#------------------------------------------------------------------------------------------
#
# Step 2 - Introduce Iteration
#
# draw_fractal(initial_state, target, replacement, num_replacements):
# For the range 0 -> num_replacements:
# 1. Find any instances of the character 'target' in the
# string 'initial_state'
# 2. Replace those instances with the string in 'replacement'
#
#------------------------------------------------------------------------------------------
#
# Step 3 - Translate to program code
#
# Take the string 'initial_state' and store it in a variable called
# 'altered state' to avoid any confusion. Then complete the following
# steps the number of times stated in 'num_replacements': dind any
# instances of the character 'target' in the string 'initial_state'
# and replace those instances with the string in 'replacement'. Finally,
# return the string 'altered_state'. This will be used in the function
# 'draw_fractal'
#
def replace(initial_state, target, replacement, num_replacements):
# Create an L-system algorithm based on the parameters given
altered_state = initial_state
for replacement_instance in range(0, num_replacements):
altered_state = altered_state.replace(target, replacement)
# Return the L-system string (it will be used in
# the 'draw_fractal' function)
return altered_state
#------------------------------------------------------------------------------------------
#
# Step 1 - Introduce the function signature
#
# draw_fractal(initial_state, target, replacement, \
# num_replacements, length, angle):
# 1. Take a simple string and apply an L-system algorithm to it, then draw
# a fractal based on the items in that string
#
#------------------------------------------------------------------------------------------
#
# Step 2 - Introduce Iteration
#
# draw_fractal(initial_state, target, replacement, \
# num_replacements, length, angle):
# For each item in the string:
# 1. If the item is 'F', draw forward by the amount stated in
# the variable 'length'
# 2. If the item is '-', turn left by the number of degrees
# stated in the variable 'angle'
# 3. If the conditions in steps 1 and 2 are not met, turn right
# by the number of degrees stated in the variable 'angle'
#
#------------------------------------------------------------------------------------------
#
# Step 3 - Translate to program code
#
# Using the 'replace' function, the string in 'initial_state' is
# taken and an L-system algorithm is applied to it. It then takes
# this new string and iterates through the individual items in
# the string with 'F' making the turtle draw forward, '-' making
# the turtle turn left and '+' making the turtle turn right. The
# exact measurements of these movements are defined in the
# variables 'length' and 'angle.
#
def draw_fractal(initial_state, target, replacement, num_replacements, length, angle):
"""
This function draws a fractal using the following parameters:
initial_state is a string
'target' is the command to be replaced with 'replacement'
num_replacements is an integer indicating how many times to
the replacement rules
length is the length to go forward
angle is the angle to turn (either right or left)
"""
# Replace elemetns within 'initial_string', creating an L-system algorithm
replaced_string = ''
replaced_string = replace(initial_state, target, replacement, num_replacements)
# Implement the L-system algorithm
for item in replaced_string:
if item == 'F':
forward(length)
elif item == '-':
left(angle)
else:
right(angle)
##
########## END Translate Code #############################
########## BEGIN FRACTAL DRAWING ###################################
# Creates the background colour by drawing and colouring a square
up()
goto(-500, -500)
color(background_colour)
begin_fill()
down()
for sides in range(0, 4):
forward(1000)
left(90)
end_fill()
up()
# Draws a small green square fractal using the 'draw_fractal' function
up()
goto(-187.5, 191.5)
down()
color('green')
draw_fractal('F+F+F+F+', 'F', 'F+F-F-F+F', 4, 4.48, 90)
# Draws a large purple square fractal using the 'draw_fractal' function
up()
goto(-188, -172)
down()
color('purple')
draw_fractal('F-F-F-F-', 'F', 'F+F-F-F+F', 4, 4.5, 90)
# Moves the turtle off the canvas
up()
goto(1000, 0)
© 2009 Peter McDowell
