Escriu per cercar…

Geometry

Aquesta pàgina encara no s'ha traduït — es mostra en l'idioma original:English

Introduction

We start with the simplest vocabulary of images, with “left” and “right” and “one, two, three,” and before we know how it happened the words and numbers have conspired to make a match with nature: we catch in them the pattern of mind and matter as one.

Imagine that you have control of a little creature called a turtle that exists in a mathematical plane or, better yet, on a computer display screen.

The turtle can respond to a few simple commands:

  • forwward moves the turtle, in the direction it is facing, some number of units.
  • right rotates it in place, clockwise, some number of degrees.
  • back and left cause the opposite movements.

The number that goes with a command to specify how much to move is called the command’s input.

In describing the effects of these operations, we say that:

  • forward and back change the turtle’s position (the point on the plane where the turtle is located)
  • right and left change the turtle’s heading (the direction in which the turtle is facing).
python
from turtle import *

forward(100)
right(90)
forward(150)
left(45)
backward(100)

mainloop()

The turtle can leave a trace of the places it has been: The position-changing commands can cause lines to appear on the screen. This is controlled by the commands penup and pendown. When the pen is down, the turtle draws lines.

python
left(45)
penup()
forward(100)

In describing the effects of these operations, we say that forward and back change the turtle’s position (the point on the plane where the turtle is located); right and left change the turtle’s heading (the direction in which the turtle is facing).

The turtle can leave a trace of the places it has been: The position-changing commands can cause lines to appear on the screen. This is controlled by the commands penup and pendown. When the pen is down, the turtle draws lines.

Function

Turtle geometry would be rather dull if it did not allow us to teach the turtle new functions.

But luckily all we have to do to teach the turtle a new trick is to give it a list of functions it already knows.

For example, here’s how to draw a square with sides 100 units long:

python
from turtle import *

def square():
    forward(100)
    right(90)
    forward(100)
    right(90)
    forward(100)
    right(90)
    forward(100)
    right(90)

square()

mainloop()

This is an example of function.

The first line of the funcion definition specifies the function’s name. We’ve chosen to name this procedure square, but we could have named it anything at all.

The rest of the function (the body) specifies a list of instructions the turtle is to carry out in response to the square command.

There are a few useful tricks for writing functions. One of them is called iteration, meaning repetition doing something over and over.

Here’s a more concise way of telling the turtle to draw a square, using iteration:

python
def square():
    for _ in range(4):
        forward(100)
        right(90)

Estàs llegint una vista prèvia.

Inicia sessió per llegir l'article complet. Qualsevol compte obre 4 articles gratuïts al mes; l'alumnat i el professorat llegeixen les pàgines del seu curs sense límit.

Inicia sessió