Home
Project Folders
Teacher Resources
Student Resources
En ESPAÑOL
How Do I?
Vocabulary
MWForum
Site Map
Search
Our Team

polygons 1 2 3 4 5 6 7 8 9
Polygons Expert 9
Download
5 KB


Goal:   The turtle creates irregular polygons with randomly-chosen angles, sides, and interior colors.


Screenshot


Vocabulary:   random, fill

Code
Project Map

to irregular-polygon
setc whatcolor
setpensize howthick
t2, pu setpos t1's "pos
t1, pd lt random 60
repeat (#sides - 1) [
t1,
fd 30 + random 50
rt (360 / #sides) - (random 20) + (random 20)
]
towards "t2
fd distance "t2

pu lt 40 bk 30
setc random 140
fill fd 30 rt 40
setc whatcolor
end

to reset
ask [t1 t2] [home seth 0]
cg
end



Notes

The basic concept here is that turtle t2 serves as home base while t1 draws all but the last side of a polygon with randomly-chosen angles and side lengths; then t1 rejoins t2.  

  1. Set the #sides slider to a maximum of 6, so that this project will create irregular triangles, quadrilaterals, pentagons, and hexagons.

  2. In the irregular-polygon procedure, fd 30 + random 50 means, "Move forward 30 plus a random number at least 0 and less than 50."

  3. The command to turn the turtle to the right is a bit complicated.   For a regular polygon, the turtle turns rt 360 / #sides.   For this project, the turtle makes two adjustments to that number:
    rt (360 / #sides) - (random 20) + (random 20)
    The turtle subtracts a number less than 20 and then adds another number less than 20.   This means that sometimes the turtle will be turning right a bit more than 360 / #sides and sometimes it will be turning a bit less than 360 / #sides.

  4. This project uses two methods for one turtle to move to the other turtle's position.   Before drawing the polygon, you may drag t1 anywhere on the page and t2 will follow it with one command: t2, setpos t1's "pos.   When t1 completes the polygon, we use two commands instead:
    towards "t2
    fd distance "t2

    In this way, t1 is facing in an appropriate direction after drawing the last side of the polygon.  

  5. The turtle then puts its pen up, turns left, backs into the interior of the polygon, changes to a randomly-selected color (setc random 140), fills the area with this color, and returns to the position, heading, and color it held when it finished drawing the outline of the polygon.




What If?
Try increasing the maximum on the #sides slider to 8 or 10 or more.   (Does the project still draw polygons? Do you need to adjust any numbers in the procedure?)


Challenge
Try different numbers after random to alter the range of possible angles and side lengths.


On Your Own - Project Ideas
Can you write a procedure that draws a specific type of irregular polygon, such as an isosceles triangle or a parallelogram?





© copyright 2001 - 2016  OpenWorld Learning.   All rights reserved.