In this weeks project we had to create an algorithm that would draw a sequence of shapes. This was by far the most difficult assignment for me so far. The first step was to create functions that made the actual shapes. I made a square, a line, a vee, a triangle, a house, and a circle. It was also important that at the beginning of each shape function you saved the position and the heading of your turtle and that at the end of each shape function you reset the position and heading of your turtle. I learned this later on in the project. After creating the shapes, I made an algorithm to make my shapes appear in a straight line with a space between each shape. To do this I first created a function called drawShape. Within this function I had a conditional statement such as:
def drawShape(): if l == "l": sahirsch_line() elif l == "v": sahirsch_vee() else: sahirsch_square()
This instructed the program what shape to draw in my processString function that I created next. If it the character was an 'l,' then the program would draw a line. If it was a 'v,' then it would draw a vee. Within this function I created a for loop structure 'for i in string.' This then enabled me to draw whatever combination of shapes I wanted to in a straight line. The loop structure instructed the program to, draw the shape, then pick up the turtle, then go forward, and then put down the turtle. The program did these steps for each character in my string. I then expanded on this to make a grid. To do this I modified the processString function by adding a mod function and a conditional statement within the loop structure. I will explain this in greater detail below. This allowed me to draw a grid, which I could easily modify. I could change the number of characters and the number of rows within the processString function. Then I could modify my mod function and my conditional function within my loop structure accordingly.
The grid algorithm says that for each character (char) in the string ("thclvvaalvhhllvvaathhtlvahalvt") of the processString function draw the shape of that character. 'i' represents the index of each character in the string. The % represents the remainder. Thus, the line of the algorithm, 'if i % 30 == 29,' means that if you take 'i' and divide it by 30 and the remainder is 29, then you follow the indented commands below. These state: pick up the turtle, go right 90 degrees, then forward 40, then right 90 degrees, then forward 870, then right 90 degrees again, and then set the turtle down. After these commands the 'else' indicates that if the remainder is not 29 then you follow a different set of commands. These are the commands indented underneath the ÒelseÓ. These commands are: pick up the turtle, go forward 30 and then put down the turtle draw the shape. These command draw shapes in a straight line. This continues in a straight line until you reach character where the index of the character, 'i', divided by 30 has a remainder of 29.
This works because if the remainder is not 29, then you pick up the turtle, go forward, put down the turtle, and then draw the shape. If the remainder is 29 then you begin the next column. You can easily modify this algorithm if you wanted to. If you want 4 rows of shapes you just multiply your string in your process string function by 4. (processString("thclvvaalvhhllvvaathhtlvahalvt"*4)) If you want to change the number of characters drawn you need to modify the characters in your string and then you just have to modify the 'if i % ___ == ___' statement. You would fill in the blanks with the numbers that you wanted. You would also have to modify the length of the second forward command underneath the ÒifÓ portion of the algorithm so that the next row begins in the correct place.
Above is a photo of the scene A, which is a straight line of 23 shapes. This was created using a loop structure, "for i in string."
Above is a phot of scene B, which is a grid with 4 rows and 30 colomns. This was also created using a loop structure. However, within this loop structure there was also a mod function and a conditional.
Above is a photo of scene C, which includes my one of my partners shapes, a hexagon. (the other two of his shapes I already had)
For an extension I added colors for each of my shapes. I also included my partner's hexagon into my grid. This is the photo above.