Tip:
Highlight text to annotate it
X
Hi. In this video, I'm going to show you a couple cool things about angles and arcs.
Let's go back into Pencil Code and make a program.
My account was called "newbie", so when I visit "newbie.pencilcode.net", I see a directory
of files that I've made, and I can create a new one by clicking on this link.
I'll call it "polygon".
And I'll make program that makes a square, like this.
Remember, the function "pen" selects a pen color.
"fd" moves the turtle forward by some number of pixels.
And "rt" turns the turtle right by some number of degrees.
This program makes a perfect square, because it moves forward the same amount four times, turning right by 90 degrees each time.
Now, what if we want to make a triangle?
If you remember your geometry, an equilateral triangle has three equal angles, 60 degrees each.
So to make a triangle, I'm going to try something. It's not going to quite work, but let's see what it does.
I'm going to change each one of my turns to
60 degrees, and I'm going to expect it to draw a triangle.
Instead, it draws this shape. It looks like the beginnings of a hexagon.
If I extend this to six sides, it will draw a perfect regular hexagon.
What happened here?
What I need for a triangle is an interior angle of 60 degrees, but what I drew is an exterior angle.
Let's take a look at a picture.
When you say "rt 60", you're changing the direction of the turtle by 60 degrees.
That actually ends up creating an obtuse angle, like this. Think about it this way.
If you turn the turtle a tiny amount, like one degree,
the turtle only changes direction a little bit, but the angle that it forms is huge.
The turtle measures its turns using what mathematicians call an "exterior" angle.
The exterior angle measures how much the direction changes when going around a corner.
The angle we normally think about when looking at a corner is the "interior" angle.
Exterior and interior angles always add up to 180 degrees.
So we want a 60 degree interior angle, which means we need to tell the turtle to turn
180 minus 60 degrees, which is 120 degrees. So if we change our program to use
120 degree turns, we'll get a nice triangle.
Here I'm going to load up a script that modifies "rt" so that you can see the angle as it's drawn.
It will visualize every exterior angle.
The interesting thing about exterior angles around a polygon is that they always add up to 360 degrees.
You can see that more easily if we shrink the polygon down. Let's do that here.
Going around every corner will eventually make a complete circle.
How would we make a regular pentagon?
Since it has five equal exterior angles that all add up to 360, the angle that we would have to turn is 360 divided by 5.
We can actually tell CoffeeScript to do the math inside our program like this.
When I run it, it will compute the answer, which is 72 degrees.
Notice that the interior angle of the pentagon is 180 minus 72 degrees, which is 108.
This is true for any regular polygon. The interior angle is 180 minus 360 divided
by the number of corner. You can try other shapes.
Think about what happens if, instead of using 360, you use a multiple of 360, like 720.
The last thing I'll show you is how to draw arcs and rounded corners.
To do that, provide a second argument to the "rt" function.
Normally, the turtle turns in place. But by using a second argument, it makes it
turn like a car. It will move as it turns, tracing out an arc with a radius.
This second argument is the turning radius, and it's this length, right here.
So by adding a turning radius to every corner, I can make a nice rounded polygon.
With angles and arcs, you can draw some pretty interesting shapes.
Have some fun with it!