Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

Lab 3c.py import turtle import math # Koch snowflake. koch = {'start': 'F++F++F', 'F': 'F-F++F-F'} koch_draw = {'F': 'F 1', '+': 'R 60', '-': 'L 60'} # Hilbert curve. hilbert = {'start': 'A', 'A':...

1 answer below »

Lab 3c.py
import turtle
import math
# Koch snowflake.
koch = {'start': 'F++F++F',
'F': 'F-F++F-F'}
koch_draw = {'F': 'F 1',
'+': 'R 60',
'-': 'L 60'}
# Hilbert curve.
hilbert = {'start': 'A',
'A': '-BF+AFA+FB-',
'B': '+AF-BFB-FA+'}
hilbert_draw = {'F': 'F 1',
'-': 'L 90',
'+': 'R 90'}
# Sierpinski triangle.
sierpinski = {'start': 'F-G-G',
'F': 'F-G+F+G-F',
'G': 'GG'}
sierpinski_draw = {'F': 'F 1',
'G': 'F 1',
'+': 'L 120',
'-': 'R 120'}
# ----------------------------------------------------------------------
# L-systems functions.
# ----------------------------------------------------------------------
def update(lsys, s):
"""
This function generates the L-system
string via L-system rules in order to
turn all strings into one.
"""
pass
def iterate(lsys, n):
"""
This function returns the result of the starting string
for the L-system for a number of times.
"""
pass
def lsystem_to_drawing_commands(draw, s):
"""
This returns the steps in drawing the figure we want.
"""
pass
def next_location(x, y, angle, cmd):
"""
This returns a tuble of a generated x-coordinate,
y-coordinate,and angle the turtle must move.
"""
pass
def bounds(cmds):
"""
This returns a tuple of the minimum and maximum of both
x and y by calculating the boudaries of the drawing.
"""
pass
def save_drawing(filename, bounds, cmds):
"""
This will write the turtle's movement to a file, one movement
per line.
"""
pass
def make_drawings(name, lsys, ldraw, imin, imax):
"""Make a series of L-system drawings at different iteration levels.
Arguments:
- name: the root name of the files to be generated
- lsys: the L-system dictionary
- ldraw: the drawing commands for the L-system
"""
print('Making drawings for {}...'.format(name))
for i in range(imin, imax):
XXXXXXXXXXs = iterate(lsys, i)
XXXXXXXXXXcmds = lsystem_to_drawing_commands(ldraw, s)
XXXXXXXXXXb = bounds(cmds)
XXXXXXXXXXsave_drawing('{}_{}'.format(name, i), b, cmds)
def make_all_drawings():
"""Make a group of drawings of different L-systems.
The L-systems are simulated at different iteration levels.
The drawings are saved as files.
"""
make_drawings('koch', koch, koch_draw, 0, 6)
make_drawings('hilbert', hilbert, hilbert_draw, 1, 7)
make_drawings('sierpinski', sierpinski, sierpinski_draw, 0, 7)
if __name__ == '__main__':
make_all_drawings()

lab 3_draw.py
"""lab3_draw.py: drawing commands for lab 3 code."""
import sys
from turtle import *
def load_drawing(filename):
"""Load the drawing information and the bounding box from a file.
Argument: filename: the name of the file to load from.
Return value: a 2-tuple containing:
XXXXXXXXXXthe bounds as an (`xmin`, `xmax`, `ymin`, `ymax`) tuple
XXXXXXXXXXthe list of drawing commands
"""
try:
XXXXXXXXXXf = open(filename, 'r')
XXXXXXXXXXbounds = f.readline()
XXXXXXXXXXbounds = bounds.split()
XXXXXXXXXXassert len(bounds) == 4
XXXXXXXXXXbounds = map(float, bounds)
XXXXXXXXXXcmds = []
XXXXXXXXXXfor line in f:
XXXXXXXXXXnext = line.split()
XXXXXXXXXXassert (len(next) == 2 and next[0] in ['F', 'L', 'R']) or \
XXXXXXXXXXlen(next) == 4 and next[0] == 'G')
XXXXXXXXXXif next[0] in ['F', 'L', 'R']:
XXXXXXXXXXnext[1] = int(next[1])
XXXXXXXXXXelse: # 'G'
XXXXXXXXXXnext[1] = float(next[1])
XXXXXXXXXXnext[2] = float(next[2])
XXXXXXXXXXnext[3] = float(next[3])
XXXXXXXXXXcmds.append(next)
XXXXXXXXXXreturn (bounds, cmds)
except FileNotFoundE
or:
XXXXXXXXXXprint('ERROR: file not found: {}'.format(filename), file=sys.stde
)
XXXXXXXXXXsys.exit(1)
def draw_picture(bounds, cmds, fast=False):
"""Given the bounds and the drawing commands, draw the picture."""
(xmin, xmax, ymin, ymax) = bounds
assert xmin <= xmax
assert ymin <= ymax
reset()
hideturtle()
speed(0) # fastest
if fast:
XXXXXXXXXXtracer(0) # no tracing
xscale = float(xextent) / (float(xmax - xmin) * 1.3)
yscale = float(yextent) / (float(ymax - ymin) * 1.3)
scale = min(xscale, yscale)
xorigin = int(scale * (xmin + (xmax - xmin) / 2))
yorigin = int(scale * (ymin + (ymax - ymin) / 2))
penup()
goto(-xorigin, -yorigin)
pendown()
counter = 1
for cmd in cmds:
XXXXXXXXXXif cmd[0] == 'L':
XXXXXXXXXXleft(cmd[1])
XXXXXXXXXXelif cmd[0] == 'R':
XXXXXXXXXXright(cmd[1])
XXXXXXXXXXelif cmd[0] == 'F':
XXXXXXXXXXforward(scale * cmd[1])
XXXXXXXXXXcounter += 1
XXXXXXXXXXelse: # cmd[0] == 'G':
XXXXXXXXXXpenup()
XXXXXXXXXXnew_x = -xorigin + scale * cmd[1]
XXXXXXXXXXnew_y = -yorigin + scale * cmd[2]
XXXXXXXXXXgoto(new_x, new_y)
XXXXXXXXXXsetheading(cmd[3])
XXXXXXXXXXpendown()
def usage():
"""Print a usage message."""
usageMsg = 'usage: python3 {} [-fast] filename [filename2 ...]'
print(usageMsg.format(sys.argv[0]), file=sys.stde
)
sys.exit(1)
if __name__ == '__main__':
if len(sys.argv) < 2:
XXXXXXXXXXusage()
fast = sys.argv[1] == '-fast'
if fast:
XXXXXXXXXXfiles = sys.argv[2:]
else:
XXXXXXXXXXfiles = sys.argv[1:]
if not files:
XXXXXXXXXXusage()
xextent = 800
yextent = 800
setup(xextent, yextent)
for file in files:
XXXXXXXXXXbounds, cmds = load_drawing(file)
XXXXXXXXXXdraw_picture(bounds, cmds, fast)
XXXXXXXXXXupdate()
XXXXXXXXXXinput('Hit return to continue...')



Part C: Miniproject: L-Systems
In this week’s miniproject, we’ll be exploring a fascinating topic called L-systems. An L-system
is a string rewriting system (we’ll explain what this means below) that can also be used to
generate a
itrarily complex images. L-systems were invented by
Answered Same Day Dec 11, 2021

Solution

Sandeep Kumar answered on Dec 12 2021
124 Votes
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here