Creating Grids and Linear Patterns


The point of this project was to use aspects of python code that we have been learning about in class to create two-dimensional patterns of shapes in turtle graphics. More specifically, using strings and loops to allow the computer to do most of the work for us, rather than hard coding in each shape's position, etc.

To easily tell the program what to do we remotely defined functions to draw shapes, imported these functions into another program, and then used strings of letters (each letter corresponding to a different shape) to define our patterns of shapes. This was achieved using an if structure like so:

 def drawShape(s):
	if s == 'c':
		pskirk_circle()
	elif s == 's':
		pskirk_line()
	elif s == 't':
		pskirk_triangle()
	elif s == 'j':
		pskirk_star()
	elif s == 'p':
		pskirk_pentagon()
	else:
		print 'Invalid Selection'

This was easily used to create a linear pattern of shapes defined by the programmer.

Next we adapted this code and added a loop structure to create a grid that was ten shapes long. This loop counted the length of the string and would automatically start a new row after ten shapes by recognizing the remainder of 9 when the particular shape's numbered location in the string was divided by ten.

 	for x in range(len(S)):
		
		drawShape(S[x])
		up()
		forward(20)
		down()
		
		if x % 10 == 9:
			up()
			right(90)
			forward(33)
			right(90)
			forward(200)
			right(180)
			down()
			

This code uses the range and length functions in order to convert the string into a sequence of numbers and then iterate over this sequence, accessing the particular shapes by using their numbers in the sequence. As I said before, when the program (using the remainder function) recognizes that it has drawn ten shapes in the sequence, it will automatically begin a new line and resume the sequence of shapes where it left off. This creates a grid like so:

I partnered with Mark Ziffer, whose shapes program added a double circle, hexagon, and arrow to my program's repertoire of possible shapes to draw. Utilizing Mark's shapes as well as my own I created the linear image below.

Extensions: As can be seen above, my programs actually use five shapes rather than just three. I created a square, a right triangle, a circle, a pentagon, and a star. Additionally, all of these are shaded a randomly generated color every time they are drawn. The pentagon, triangle, and circle are filled with this shading while the star and square remain white on the inside. Lastly, I decided to include Mark's shapes in a grid program, creating the image below.

Back to Main