# Recursive and Iterative Drawing in Python with Turtle

## Homework 2

### DrawStar(S, N)

Recursion to draw snowflake

```
python
def DrawStar(S, N):
"""
Recursion to draw snowflake
"""
if N == 1:
pass
else:
turtle.forward(S/3)
DrawStar(S/3, N-1)
turtle.forward(-S/3)
turtle.left(60)
turtle.forward(S/3)
DrawStar(S/3, N-1)
turtle.forward(-S/3)
turtle.left(60)
turtle.forward(S/3)
DrawStar(S/3, N-1)
turtle.forward(-S/3)
turtle.left(120)
turtle.forward(S/3)
DrawStar(S/3, N-1)
turtle.forward(-S/3)
turtle.left(60)
turtle.forward(S/3)
DrawStar(S/3, N-1)
turtle.forward(-S/3)
turtle.left(60)
```

### DrawBase(S, N)

Recursion to draw base of snowflake

```
python
def DrawBase(S, N):
"""
Recursion to draw base of snowflake
"""
if N == 0:
pass
else:
turtle.forward(S)
DrawStar(S, N)
turtle.forward(-S)
turtle.left(60)
turtle.forward(S)
DrawStar(S, N)
turtle.forward(-S)
turtle.left(60)
turtle.forward(S)
DrawStar(S, N)
turtle.forward(-S)
turtle.left(60)
turtle.forward(S)
DrawStar(S, N)
turtle.forward(-S)
turtle.left(60)
turtle.forward(S)
DrawStar(S, N)
turtle.forward(-S)
turtle.left(60)
turtle.forward(S)
DrawStar(S, N)
turtle.forward(-S)
turtle.left(60)
```

### main(S, N)

Main function to run the program and set the turtle speed

```
python
def main(S, N):
"""
Main function to run the program and set the turtle speed
"""
turtle.speed('fastest')
DrawBase(S, N)"""
Calls the main function and asks the user to input the size and depth
"""
main(int(input('Enter size ')), int(input('Enter depth ')))
input('Press enter to finish...')
```

## Homework 3

### countWords(textFileName)

```
python
def countWords(textFileName):
openfile = open(textFileName, "r")
openfile.read()
openfile.seek(0)
words = 0
for line in openfile:
for char in line.strip():
if char == " ":
words += 1
words += 1
return words
```

### main()

```
python
def main():
n = countWords(input("Please enter the file name"))
print("The number of words in the file is:: " + str(n))main()
```

## Lab 2

### init()

This function puts the turtle in the correct starting position and sets it to the fastest speed

- Pre-Conditions: Turtle starts facing to the right.
- Post Conditions: Turtle ends facing down.

```
python
def init():
"""
This function puts the turtle in the correct starting position and sets it to the fastest speed
Pre-Conditions: Turtle starts facing to the right.
Post Conditions: Turtle ends facing down.
"""
turtle.speed('fastest')
turtle.right(90)
```

### drawhalfsquare(depth, length)

This function draws half of a square

- Pre-Conditions: Turtle is facing down
- Post-Conditions: Turtle is facing up

```
python
def drawhalfsquare(depth, length):
"""
This function draws half of a square
Pre-Conditions: Turtle is facing down
Post-Conditions:Turtle is facing up
"""
turtle.forward(length/2)
turtle.left(90)
turtle.forward(length)
turtle.left(90)
turtle.forward(length/2)
```

### inner(depth, length)

This function is a recursion that draws the inner squares

- Pre-Conditions: The turtle is facing up after the bottom half of of the square is drawn
- Post-Conditions: The turtle is facing up in the same spot where it started

```
python
def inner(depth, length):
"""
This function is a recursion that draws the inner squares
Pre-Conditions: The turtle is facing up after the bottom half of of the square is drawn
Post-Conditions: The turtle is facing up in the same spot where it started
"""
if depth == 0:
pass
elif depth >= 1:
Color(depth)
turtle.left(45)
turtle.forward(length/(2*math.sqrt(2)))
turtle.left(90)
turtle.forward((length/(2*math.sqrt(2)))/2)
inner(depth-1, length/(2*math.sqrt(2)))
Color(depth)
drawhalfsquare(depth, length/(2*math.sqrt(2)))
inner(depth-1, length/(2*math.sqrt(2)))
Color(depth)
turtle.forward((length/(2*math.sqrt(2)))/2)
turtle.left(45)
```

### Color(depth)

This function finds out which color needs to be used

```
python
def Color(depth):
"""
This function finds out which color needs to be used
"""
if depth % 2 == 0:
turtle.color('blue')
else:
turtle.color('green')
```

### drawsquares(depth, length)

This is a recursion that draws the squares

- Pre-Conditions: Side of the square facing up
- Post-Conditions: Same as pre-condition

```
python
def drawsquares(depth, length):
"""
This is a recursion that draws the squares
Pre-Conditions:Side of the square facing up
Post-Conditions: Same as pre-condition
"""
if depth == 0:
pass
else:
Color(depth)
drawhalfsquare(depth, length)
inner(depth-1, length)
Color(depth)
drawhalfsquare(depth, length)
inner(depth-1, length)
Color(depth)
```

### main(depth, length)

This function runs the program

```
python
def main(depth, length):
"""
This function runs the program
"""
init()
drawsquares(depth, length)
input('Please press enter to exit...')main(int(input('Input depth ')), 300)
```

## Lab 3

### outsidebox()

Keeps the turtle inside the boundaries

```
python
def outsidebox():
"""
Keeps the turtle inside the boundaries
"""
x, y = turtle.position()
if x > 160:
turtle.back(35)
turtle.right(180)
elif x < -160:
turtle.back(35)
turtle.right(180)
if y > 160:
turtle.back(35)
turtle.right(180)
elif y < -160:
turtle.back(35)
turtle.right(180)
```

### randomdistance()

Randomly decides the distance between triangles

```
python
def randomdistance():
"""
Randomly decides the distance between triangles
"""
d = random.randint(0, MAX_DISTANCE())
turtle.forward(d)
```

### drawfiguresrec(n)

Recursive function to draw arrows

```
python
def drawfiguresrec(n):
"""
Recursive function to draw arrows
"""
if n == 0:
return 0
elif n >= 0:
randomcolor()
a = arrow()
randomturn()
turtle.up()
randomdistance()
outsidebox()
turtle.down()
return (a + drawfiguresrec(n-1))
```

### drawfiguresite(n)

Iterative function to draw arrows

```
python
def drawfiguresite(n):
"""
Iterative function to draw arrows
"""
a = 0
while n > 0:
randomcolor()
len = random.randint(0, MAX_SIZE())
turtle.begin_fill()
turtle.forward(len)
turtle.left(120)
turtle.forward(len)
turtle.left(120)
turtle.forward(len)
turtle.left(120)
turtle.end_fill()
randomturn()
turtle.up()
randomdistance()
outsidebox()
turtle.down()
a = ((math.sqrt(3)/2)*len + a)
n = n - 1
print('The total area is ', a)
```

### numcheck(n)

Checks to see if the iterations are in a valid range

```
python
def numcheck(n):
"""
Checks to see if the iterations are in a valid range
"""
if n < 0:
print("The number you entered is not in the valid range [0,500]")
n = int(input('Enter iterations '))
numcheck(n)
elif n > 500:
print("The number you entered is not in the valid range [0,500]")
n = int(input('Enter iterations '))
numcheck(n)
else:
pass
```

### main(n)

Main function to run the recursive and iterative

```
python
def main(n):
"""
Main function to run the recursive and iterative
"""
numcheck(n)
init()
BOUNDING_BOX()
print("The total area is", (drawfiguresrec(n)))
input('Press enter to draw using the iterative function')
turtle.reset()
init()
n = int(input('Enter iterations '))
BOUNDING_BOX()
drawfiguresite(n)
input('Please press enter to exit...')main(int(input('Enter iterations ')))
```