Handout 15A

As you know, LOGO can be very useful in teaching about coordinate systems. In this exercise we start with an example of an exercise in coordinate geometry and progress to some elementary linear algebra.

Draw a picture on a piece of graph paper so that all the lines in the picture are straight and so that you can trace the lines in the picture without lifting your pencil from the paper.

Right facing moth
Choose an origin {0 0} for your drawing. Write down the coordinates your pencil moves through as you trace out the picture. For the the moth shown above this looks like:
{{-10 10} {-10 20} {0 0} {-20 20} {-20 -20} {0 0} {10 20} {-10 -20}}

The LOGO program below takes as its argument an array of arrays. Each item in the argument list gives the coordinates of a point in the plane. The procedure moves the turtle to each of those points.

TO TURTLETO :POINT
  SETXY (ITEM 1 :POINT) (ITEM 2 :POINT)
END

TO DRAWARRAY :COORDINATES
  PU TURTLETO (ITEM 1 :COORDINATES) PD  ; Start at first point in array.
  FOR [I 1 [COUNT :COORDINATES]] ~
   [
    TURTLETO (ITEM :I :COORDINATES)     ; Move to each point in the array.
   ]
END
Use the DRAWARRAY procedure to draw your picture. For example, the following command draws a very tiny moth:
MAKE "MOTH {{-10 10} {-10 20} {0 0} {-20 20} {-20 -20} {0 0} {10 20} {-10 -20}}
DRAWARRAY :MOTH
(If your picture comes out too small, don't worry. We can fix that problem in the next section.)

Read the DRAWARRAY and TURTLETO procedures carefully. Together the two procedures handle an array whose items are arrays. The ability to have an array of arrays is a powerful tool. An array of arrays can be thought of as simply that, or, if all the subarrays are the same size, it can be thought of as a matrix. The matrix for the MOTH array might look like:

-10  10
-10  20
 0    0
-20  20
-20 -20
 0    0
 10  20
-10 -20
Of course it isn't very helpful to write a list of coordinates as a matrix, but it is helpful to use matrices to represent distances between vertices in a graph, linear transformations or tables of data.


One very simple linear transformation is a dilation. In a dilation, the coordinates of each point in the plane are multiplied by some constant so the entire plane shrinks or expands. Use the following LOGO procedures to dilate your picture (e.g. DILATE :MOTH 4.)

TO DILATETO :POINT :RATIO
  SETXY (:RATIO * (ITEM 1 :POINT)) (:RATIO * (ITEM 2 :POINT))
END

TO DILATE :COORDINATES :RATIO
  PU DILATETO (ITEM 1 :COORDINATES) :RATIO PD
  FOR [I 1 [COUNT :COORDINATES]] ~
   [
    DILATETO (ITEM :I :COORDINATES) :RATIO
   ]
END
Compare the DILATE and DRAWARRAY procedures; they are very similar. It would save us some typing if we had a procedure which takes an array of points and a transformation and draws the transformed figure, but we won't do that today.


Another simple transformation of the plane is reflection across the y-axis. This is achieved simply by replacing each X coordinate with -1*X. Modify the TURTLETO and DRAWARRAY procedures so that they draw a reflection of your figure.


Almost any linear transformation of the plane (i.e. any way of transforming the plane so that all straight lines remain straight) can be described by multiplying the coordinates of a point by a two by two matrix then adding a vector. A two by two matrix in LOGO is just a two item array containing two two item arrays (e.g. {{1 0} {0 1}}.) It is not hard to write a LOGO procedure to multiply this matrix by the coordinate vector of a point, and it is even easier to write a LOGO procedure to add one coordinate vector to another. So we could use LOGO to apply almost any linear transformation to our figure!

Besides the obvious value of this exercise as a review of linear algebra (what is the matrix of a dilation? of reflection across the y-axis?) it also provides us with insight into how computers draw pictures. The objects to be drawn in, say, a flight simulation program, are stored in terms of their coordinates. The computer does some linear algebra with these coordinates, and decides where on the screen each tree, house or mountain will appear. It is therefore important that modern computers (and modern programmers) be able to do linear algebra well and quickly. The difference between a good video card and a bad one is whether and how fast it can help the computer do this algebra!


Mtht420