So far, we have told the turtle to draw pictures by giving it instructions like FORWARD, BACK, LEFT, and RIGHT. These instructions have something in common: they are all relative to the turtle's point of view.
In this lesson, we will give instructions that are relative to the turtle's home, not the turtle's current position. We will use these instructions to draw pictures that resemble string art.
These new instructions use something called "The Cartesian Coordinate System". Don't let the fancy name fool you, it's really very simple. It was invented by a guy named Rene Descartes while he was sick in bed. He did nothing all day but stare at a tile ceiling. One day, there was a fly on the ceiling and he noticed that he could describe the fly's location by using the tiles. He described the fly's location relative to a tile in the corner (for example, three tiles over and one tile up).
The same system can be used to describe locations on any flat surface, including your computer screen. Each point is named by a pair of numbers called "coordinates". The first number is the "X" value and the second number is the "Y" value. The "X" value is how far you are to the right of the turtle's home. The "Y" value is how far you are up from the turtle's home. Note that the order of the numbers is important; the coordinate (2,3) is different from (3,2), as seen in the image below.
The turtle's home is at the coordinates (0,0). This is sometimes called "the origin", which just means "the start". The horizontal line that goes through the origin is called the "x-axis". The vertical line that goes through the origin is called the "y-axis".
You can tell the turtle to move anywhere on the screen by giving it the coordinates of where you want it to move to.
Command | Example | What Happens |
---|---|---|
HOME | HOME | Moves the turtle back to where it started. |
SETXY x-number y-number | SETXY 20 30 | Moves the turtle to the point 20 steps to the right and 30 steps up from the turtle's home. |
SETXY 0 100 SETXY 100 100 SETXY 100 0 HOME |
What happens if you move the turtle before running these instructions?
What happens if you give SETXY a negative number?
What other shapes can you draw with SETXY?
Now we're ready to make some string art! The idea behind string art is that you start with a simple set of points and create interesting pictures just by connecting those points. This is called "string art" because it can be made sewing string through cardboard.
Let's start off by connecting points that are along the X-axis and Y-axis.
TO STRINGART REPEAT 10 [ PENUP SETXY (REPCOUNT * 10) 0 PENDOWN SETXY 0 (REPCOUNT * 10) ] END |
Things get more interesting when you let the lines cross by reversing the order on one of the axes.
TO STRINGART REPEAT 10 [ PENUP SETXY (REPCOUNT * 10) 0 PENDOWN SETXY 0 (110 - REPCOUNT * 10) ] END |
Activity: Now it's your turn. Come up with an interesting set of points and connect them using SETXY. For example, try connecting points along a "Y" shape or an "X" shape. You can also take an example that uses FORWARD, BACK, LEFT, and RIGHT and rewrite it to use SETXY, instead.
TO CYCLE :INDEX :LAST SETXY :INDEX 0 SETXY :LAST :INDEX SETXY (:LAST - :INDEX) :LAST SETXY 0 (:LAST - :INDEX) SETXY :INDEX 0 END TO SQUAREART REPEAT 10 [ CYCLE REPCOUNT * 10 100 ] END SQUAREART |
|
TO CYCLE :INDEX :LAST PENUP SETXY :INDEX 0 PENDOWN SETXY 0 (:LAST - :INDEX) SETXY -:INDEX 0 SETXY 0 (:INDEX - :LAST) SETXY :INDEX 0 END TO PLUSART REPEAT 11 [ CYCLE ((REPCOUNT - 1) * 10) 100 ] END PLUSART |
|
TO SPIKEDSQUARE ; left-right lines REPEAT 20 [ HOME SETXY (100) (REPCOUNT * 10 - 110) SETXY (-100) (110 - REPCOUNT * 10) HOME ] ; up-down lines REPEAT 20 [ HOME SETXY (REPCOUNT * 10 - 100) (100) SETXY (100 - REPCOUNT * 10) (-100) HOME ] END TO SQUARE :HALF_LENGTH SETXY :HALF_LENGTH :HALF_LENGTH SETXY -:HALF_LENGTH :HALF_LENGTH SETXY -:HALF_LENGTH -:HALF_LENGTH SETXY :HALF_LENGTH -:HALF_LENGTH SETXY :HALF_LENGTH :HALF_LENGTH HOME END TO PIT REPEAT 20 [SQUARE REPCOUNT * 5] SPIKEDSQUARE END PIT |
|
TO HOUSE ; go to the origin PENUP HOME PENDOWN ; draw the main box SETXY 100 0 SETXY 100 100 SETXY 0 100 SETXY 0 0 ; draw the roof SETXY 0 100 SETXY 50 150 SETXY 100 100 ; draw the door PENUP SETXY 40 0 PENDOWN SETXY 40 40 SETXY 60 40 SETXY 60 0 END HOUSE |