Вы находитесь на странице: 1из 3

PROBLEMS WEEK 4

GRAPHICS AND FUNCTIONS


Valentina Reyes Martínez

For the following problems we´ll be using Turtle Geometry, this is a new kind of geometry that
exists only in the virtual world.

Problem 1, Draw a square


Draw a square with the turtle,
For the following problem we have two ways of solving it, the first one is:

# Problem 1 – Draw a square


>>> from turtle import *
>>> home()
>>> forward(50)
>>>left(90)
>>>forward(50)
>>>left(90)
>>>forward(50)
>>>left(90)
>>>forward(50)
>>>left(90)
>>>done ()

The second and easier one is,

# Problem 1.1 - Draw a square


>>>from turtle import *
>>>for x in [100,100,100,100]:
forward(x)
left(90)
>>>done()

Problem 2, make function square ()


Convert the algorithm of Problem #1 to a function that draws a square of any size.
By naming this procedure the turtle will remember the command, and the next time we need to
draw a similar square we just call its name.

# Problem 2 – Draw a square of any size


>>>from turtle import *
>>>def square():
for x in [100,100,100,100]:
forward(x)
left(90)
>>>square()
>>>done()

Problem 3, draw multiple squares


With the function of Problem #2 draw several squares of various sizes that share the same left lower
vertex.

>>>from turtle import *


>>>def square(lenght):
i=0
while i < 360:
forward(lenght)
left(90)
>>>square(7, 100)
>>> size = 50
>>> square(size)
>>> done()

Problem 4, draw a triangle


Draw an equilateral triangle with the turtle.

There are two options for drawing the tringle:

Calculate the angle of rotation (left) and calculate the units.


The other option is to move the turtle home (), where it started.

In this case we´re using home ().

# Problem 4 – draw a triangle


>>>from turtle import *
>>>forward(100)
>>>left(60)
>>>forward(100)
>>>home()
>>>done()

Problem 5, make function triangle ()


Convert the algorithm of Problem #4 to a function that draws a triangle of any size.

>>>from turtle import *


>>>def triangle():
for x in [100,100,100]:
forward(x)
left(60)
>>>triangle()
>>>done()

Problem 6, draw a regular polygon


Write a function that draws any regular polygon with the side of adjustable length.
A regular polygon is a closed geometric figure in which all of its sides and angles are equal. To
draw any regular polygon we need to define the number of sides and, optionally, the length of a side
if we want the function to draw polygons of varying size.

>>>from turtle import *


>>>n_sides = int(input("How many sides? "))
>>>angle = 360 / n_sides
>>>i = 0 while i < 360:
forward(100)
left(angle)
i += angle
>>>done()

Problem 7, draw a growing polygon – Spyral


Write a function that draws a growing regular polygon of various number of sides. It should be able
to reproduce any of the drawings below.

I´m sorry teacher, I didn´t understood this very well.

Вам также может понравиться