""" Authors: Sohie Lee Consulted: Purpose: CS111 Lab For loops and turtle graphics Date: Fall 2021 Filename: grids.py """ from turtle import * from turtleBeads import * import random def randomSquares(number,size,color1): '''Randomly places number squares of size size with color color1 across the canvas''' # Assumes canvas dimensions of 800 x 800 maxWidth = 800 maxHeight = 800 color(color1) for s in range(number): # generate a random location on the canvas x = random.randint(-maxWidth/2, maxWidth/2) y = random.randint(-maxHeight/2, maxHeight/2) teleport(x,y) begin_fill() drawSquare(size) end_fill() def makeRow(number, size, color1, color2): '''Creates a row of number squares of size size with color color1. Outline color2 helps make squares visible. Assumes turtle facing right at start, maintains position and heading invariance ''' pensize(5) color(color1, color2) for s in range(number): begin_fill() drawSquare(size) end_fill() leap(size) # maintain invariance: go back with no trace leap(-size*number) def makeOverlapRow(number, size, color1, color2): '''Creates a row of number overlapping squares of size size with color color1. Outline color helps make squares visible. Assumes turtle facing right at start, maintains position and heading invariance. ''' pensize(5) color(color1, color2) for s in range(number): begin_fill() drawSquare(size) end_fill() leap(size/4) # maintain invariance: go back with no trace leap(-size/4*number) def randomTurtleColor(): '''Returns random color''' r = random.random() # between 0-1 g = random.random() # between 0-1 b = random.random() # between 0-1 return (r, g, b) # tuple def makeRotatedRow(number, size, angle): ''' Creates a row of number rotated by angle overlapping squares of size size with random color. Outline color helps make squares visible. Assumes turtle facing East at start, maintains position and heading invariance. ''' pensize(5) for s in range(number): randomColor = randomTurtleColor() color(randomColor) # turn the turtle so the square will be rotated lt(angle*s) begin_fill() drawSquare(size) end_fill() # put the turtle back in its original rotation, facing right rt(angle*s) # move the turtle to next position for next square leap(size/4) # maintain invariance: go back with no trace leap(-size/4*number) def makeTwistySquares(number, size, angle): ''' Creates a row of number rotated by angle overlapping squares of size size with random color. Outline color helps make squares visible. Assumes turtle facing right at start, maintains position and heading invariance. ''' pensize(5) for s in range(number): color(randomTurtleColor()) lt(angle) begin_fill() drawSquare(size) end_fill() # maintain invariance: go back with no trace rt(angle*s) def moveToLLCorner(size): '''Moves turtle from center of square to Lower Left corner, leave no marks''' pu() bk(size/2) rt(90) fd(size/2) lt(90) pd() def moveToSquareCenter(size): '''Moves turtle from Lower Left corner to center of square, leave no marks''' pu() fd(size/2) lt(90) bk(size/2) rt(90) pd() def makeFlower(number, size, color1): '''Creates a flower-like drawing with non-filled squares that connect with each square touching the corners of the two adjacent squares. Turtle starts at lower right corner of leftmost square. Recall that drawSquare draws a square assuming that the turtle is at the center of the square.''' pensize(1.5) angle = 360/number color(color1) for petal in range(number): lt(angle) moveToLLCorner(size) drawSquare(size) moveToSquareCenter(size) # should be back facing right in original starting position def makeGrid(rows, columns, size, color1, color2): '''Draws a grid of row x column size with color color1 and outline color color2''' color(color1, color2) for r in range(rows): for c in range(columns): begin_fill() drawSquare(size) end_fill() leap(size) leap(-size*columns) # position turtle for next row pu() rt(90) fd(size) lt(90) pd() # maintain position and heading invariant pu() lt(90) fd(rows*size) rt(90) pd() def makeRowColorGrid(rows, columns, size): '''Draws a grid of row x column size with each row randomly changing color''' pensize(5) for r in range(rows): color(randomTurtleColor(),randomTurtleColor()) for c in range(columns): begin_fill() drawSquare(size) end_fill() leap(size) leap(-size*columns) # position turtle for next row pu() rt(90) fd(size) lt(90) pd() # maintain position and heading invariant pu() lt(90) fd(rows*size) rt(90) pd() def makeColumnColorGrid(rows, columns, size): '''Draws a grid of row x column size with each column randomly changing color''' pensize(5) for c in range(columns): color(randomTurtleColor(),randomTurtleColor()) rt(90) # draw a column first for r in range(rows): begin_fill() drawSquare(size) end_fill() leap(size) leap(-size*rows) # position turtle for next column pu() lt(90) fd(size) pd() # maintain position and heading invariant pu() bk(columns*size) pd() def makeRandomColorGrid(rows, columns, size): '''Draws a grid of row x column size with random colored squares''' for r in range(rows): for c in range(columns): color(randomTurtleColor()) begin_fill() drawSquare(size) end_fill() leap(size) leap(-size*columns) # position turtle for next row pu() rt(90) fd(size) lt(90) pd() # maintain position and heading invariant pu() lt(90) fd(rows*size) rt(90) pd() # Below here is testing code if __name__ =='__main__': setupTurtle() # # Testing random squares (Task A) # randomSquares(10, 50, 'red') # randomSquares(20, 10, 'cyan') pu() bk(100) pd() # # Testing makeRow (Task B) #makeRow(5, 100, 'navy', 'bisque') # # Testing makeOverlapRow (Task C) #makeOverlapRow(5, 100, 'navy', 'bisque') # # Testing makeRotatedRow (Task D) # makeRotatedRow(8, 100, 25) # # Testing makeTwistySquares (Task E) # makeTwistySquares(50, 200, 3) # Testing makeFlower (Task F challenge) # makeFlower(6,70, 'orange') # makeFlower(12,55, 'red') # # Testing makeGrid (Task G) # makeGrid(8, 3, 50, 'orange', 'grey') # # Testing makeRowColorGrid (Task H) # makeRowColorGrid(8, 3, 50) # # Testing makeRandomColorGrid (Task I) # makeRandomColorGrid(10,15, 50)