How to code l-systems
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): s = iterate(lsys, i) cmds = lsystem_to_drawing_commands(ldraw, s) b = bounds(cmds) save_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: - the bounds as an (`xmin`, `xmax`, `ymin`, `ymax`) tuple - the list of drawing commands """ try: f = open(filename, 'r') bounds = f.readline() bounds = bounds.split() assert len(bounds) == 4 bounds = map(float, bounds) cmds = [] for line in f: next = line.split() assert (len(next) == 2 and next[0] in ['F', 'L', 'R']) or \ (len(next) == 4 and next[0] == 'G') if next[0] in ['F', 'L', 'R']: next[1] = int(next[1]) else: # 'G' next[1] = float(next[1]) next[2] = float(next[2]) next[3] = float(next[3]) cmds.append(next) return (bounds, cmds) except FileNotFoundError: print('ERROR: file not found: {}'.format(filename), file=sys.stderr) sys.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:="" tracer(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:="" if="" cmd[0]="=" 'l':="" left(cmd[1])="" elif="" cmd[0]="=" 'r':="" right(cmd[1])="" elif="" cmd[0]="=" 'f':="" forward(scale="" *="" cmd[1])="" counter="" +="1" else:="" #="" cmd[0]="=" 'g':="" penup()="" new_x="-xorigin" +="" scale="" *="" cmd[1]="" new_y="-yorigin" +="" scale="" *="" cmd[2]="" goto(new_x,="" new_y)="" setheading(cmd[3])="" pendown()="" def="" usage():="" """print="" a="" usage="" message."""="" usagemsg='usage: python3 {} [-fast] filename [filename2 ...]' print(usagemsg.format(sys.argv[0]),="" file="sys.stderr)" sys.exit(1)="" if="" __name__="=" '__main__':="" if="" len(sys.argv)="">=>< 2: usage() fast = sys.argv[1] == '-fast' if fast: files = sys.argv[2:] else: files = sys.argv[1:] if not files: usage() xextent = 800 yextent = 800 setup(xextent, yextent) for file in files: bounds, cmds = load_drawing(file) draw_picture(bounds, cmds, fast) update() input('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 arbitrarily complex images. l-systems were invented by 2:="" usage()="" fast="sys.argv[1]" =='-fast' if="" fast:="" files="sys.argv[2:]" else:="" files="sys.argv[1:]" if="" not="" files:="" usage()="" xextent="800" yextent="800" setup(xextent,="" yextent)="" for="" file="" in="" files:="" bounds,="" cmds="load_drawing(file)" draw_picture(bounds,="" cmds,="" fast)="" update()="" input('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="" arbitrarily="" complex="" images.="" l-systems="" were="" invented=""> 2: usage() fast = sys.argv[1] == '-fast' if fast: files = sys.argv[2:] else: files = sys.argv[1:] if not files: usage() xextent = 800 yextent = 800 setup(xextent, yextent) for file in files: bounds, cmds = load_drawing(file) draw_picture(bounds, cmds, fast) update() input('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 arbitrarily complex images. l-systems were invented by>