turtle geometry the python way

19
Turtle Geometry the Python Way @SteveBattle

Upload: steve-battle

Post on 22-Apr-2015

177 views

Category:

Education


6 download

DESCRIPTION

An introduction to coding using Python’s on-screen ‘turtle’ that can be commanded with a few simple instructions including forward, backward, left and right. The turtle leaves a trace that can be used to draw geometric figures.

TRANSCRIPT

Page 1: Turtle Geometry the Python Way

Turtle Geometry the Python Way

@SteveBattle

Page 2: Turtle Geometry the Python Way

Turtle Geometry“The tradition of calling our display creatures ‘turtles’ started with Grey Walter, a neurophysiologist who experimented in Britain… with tiny robot creatures he called ‘tortoises’. These inspired the first turtles designed at MIT.” (1980)

Page 3: Turtle Geometry the Python Way

Turtles and Tortoises“Not even Dr. Grey Walter, their creator, can say what these mechanical tortoises are going to do next.”

Bristol based Inventor of the first autonomous robots in 1948.

Page 4: Turtle Geometry the Python Way

BBC Buggy (1983)

The Buggy was tethered to the BBC Micro and used the ‘Logo’ language.

Page 5: Turtle Geometry the Python Way

The Python Way

Idle, use tabs not spaces

Start IDLE and start typing

Page 6: Turtle Geometry the Python Way

angles

The turtle starts off

facing this way

A right angle going clockwise

A 360˚ turn returns the turtle to 0˚

Page 7: Turtle Geometry the Python Way

sequenceCreate a new file (Python module)

Run

Save the file (but not as ‘turtle’)

Page 8: Turtle Geometry the Python Way

turtle functionsforward(length) backward(length)

left(angle) right(angle)

penup() pendown()

done()

speed(s) e.g. ‘slow’, ‘fast’, ‘fastest’

shape(name) e.g. ‘turtle’, ‘classic’

goto(x,y) x,y coordinates

Page 9: Turtle Geometry the Python Way

for loops

Everything inside the loop is indented. Use TAB not

SPACE

The loop repeats a fixed number

of times (4).

slow down the output

Page 10: Turtle Geometry the Python Way

defining functionsFunction definition

Call the function

Note the double indentation ‘turtle’ shape

Page 11: Turtle Geometry the Python Way

function parametersAdd parameter

‘length’

Call ‘square’ with argument

100

Page 12: Turtle Geometry the Python Way

polygonsComma

separated parameters

calculate angle and assign to a

variable

The output from ‘print’ appears in the IDLE console

The angles of a polygon sum to

360˚

Page 13: Turtle Geometry the Python Way

Interior & Exterior angles

The (interior) angles of a

triangle sum to only 180˚

The turtle turns through the

exterior angles, which sum to

360˚

Page 14: Turtle Geometry the Python Way

stars

What happens if we turn turtle

through a multiple of 360˚?

Try also heptagons: star(100,7,2)star(100,7,4)

Double the angle of a normal pentagon

Page 15: Turtle Geometry the Python Way

while loops

Try a non-integer multiple:

spiral(400, 3, 1.01, 10)

Exit the ‘while’ loop when the

condition is false.

‘goto’ these coordinates.

Page 16: Turtle Geometry the Python Way

recursion

Recursive functions call themselves.

The angle is close to 0 on the ‘straights’

We can also get spiral motion by increasing

the angle

Page 17: Turtle Geometry the Python Way

The Snowflake Curve

Every side has smaller sides and so on, ad

infinitum.

Page 18: Turtle Geometry the Python Way

conditionals test if we’ve bottomed out.

Page 19: Turtle Geometry the Python Way