1+ # THIS SCRIPT GOES BEYOND WHAT WE COVERED IN CLASS
2+ # we will cover this next time, but I wanted to give you more to play with
3+ # it’s a multipage document that we will save
4+
5+ # import our special functions from the random library
6+ from random import randrange , random
7+
8+ # page dimensions
9+ pageWidth = 1000
10+ pageHeight = 1000
11+
12+ # define how many pages
13+ numPages = 10
14+
15+ # define how many rows and columns
16+ rows = 50
17+ cols = 50
18+
19+ # define the width and height of each unit of the grid
20+ gridWidth = pageWidth / rows
21+ gridHeight = pageHeight / cols
22+
23+ # define the width and height of the shape we will draw in that grid
24+ shapeWidth = pageWidth / rows
25+ shapeHeight = pageHeight / cols
26+
27+ # ok it’s time to loop
28+ # first we will loop through each row
29+ # and then WITHIN that loop we will loop through each column
30+ # for theThing in theRangeOfThings
31+
32+ # a loop for each page
33+ for page in range (numPages ):
34+ newPage (pageWidth , pageHeight )
35+ fill (0 )
36+ rect (0 , 0 , width (), height ())
37+
38+ # define the width and height of the shape we will draw in that grid
39+ # make it a little smaller than the grid this time
40+ shapeWidth = pageWidth / rows - 10
41+ shapeHeight = pageHeight / cols - 10
42+
43+ # define x and y as where we start? (0, 0) is in the bottom left
44+ # for each page we want to reset these
45+
46+ # we are going to draw the shapes from the center this time,
47+ # so we want our X and Y coordinates to be at the center of each grid
48+ x = gridWidth / 2
49+ y = gridHeight / 2
50+
51+ for row in range (rows ):
52+ # everything indented this much runs once for each row
53+ for col in range (cols ):
54+ # everything indented this much runs once for each grid unit
55+
56+ # before we draw our shape, let’s pick a color for it
57+ # we can create 3 variables
58+ r = random () # red
59+ g = random () # green
60+ b = random () # blue
61+ a = .8 # alpha (0 = transparent, 1 = opaque)
62+ fill (r , g , b , a )
63+
64+ # okay, now we finally draw our shape
65+
66+ # let’s define specific shape dimensions for
67+ # JUST THIS ONE shape
68+ # by using our basic dimensions and adding some randomness
69+ thisShapeWidth = shapeWidth + randrange (- 5 , 5 )
70+ thisShapeHeight = shapeHeight + randrange (- 5 , 5 )
71+
72+ # we can make a conditional
73+ # if this condition is true:
74+ # then run this code
75+ # if not:
76+ # then run this code
77+ if random () > .5 :
78+ # to draw from the center, we move the
79+ # x position to the left half of its width
80+ # and move it down half its height
81+ rect (x - thisShapeWidth / 2 , y - thisShapeHeight / 2 , thisShapeWidth , thisShapeHeight )
82+ else :
83+ oval (x - thisShapeWidth / 2 , y - thisShapeHeight / 2 , thisShapeWidth , thisShapeHeight )
84+ # at the end of the column, advance X to the next column
85+ x += gridWidth
86+ # let’s also make the shape a smidge bigger each time
87+ shapeWidth += .01
88+ shapeHeight += .01
89+ # we are OUTDENTED, which means we are back
90+ # to code that only runs once per row
91+ # now that we’ve reached the end of the row
92+ # we want to advance y to move us up to the next row
93+ y += gridHeight
94+ # and we want to return x to its original position
95+ # (a carriage return, essentially)
96+ #
97+ x -= cols * gridWidth
98+
99+ # now let’s save this image to the desktop
100+ # first as a PDF
101+ saveImage ('~/desktop/myFancyGrid.pdf' )
102+ # then as a GIF
103+ saveImage ('~/desktop/myFancyGrid.gif' )
0 commit comments