# Part 2: Draw Polygons with Procedural Abstraction

# Exercises:

NOTE: The following exercises have been copied directly from the official Mobile CSP course materials for the LOGO project.

To test your algorithm, you have to call it from the draw() procedure because that’s the procedure that will be run when you click on the Draw button.

2. Design an algorithm for drawing an equilateral triangle -- i.e., a triangle with equal sides and equal angles. First design it by hand -- how much does the Android have to turn? Because this is another example of a repetition, you can use the for-range statement in your algorithm. How many repetitions are necessary?

Once you’ve got the algorithm figured out, implement it in java to test it. Because you might want to use your triangle algorithm again, define it into a procedure with a parameter. What should the parameter represent?

3. Draw a pentagon -- i.e., a 5-sided figure with equal sides and angles. Again, first design it by hand -- how much does the Android have to turn to draw a pentagon? Since this is another example of a repetition, use the for-range statement in your algorithm. How many repetitions are necessary?

HINT: To draw a square the Android had to turn 4 times each by 90 degrees meaning it turned a total of 360 degrees.

Once you’ve got the algorithm figured out, implement it in App Inventor and test it. Because you might want to use your pentagon algorithm again, define it into a procedure with a parameter. What should the parameter represent?

4. (Advanced) Squares and pentagons are both examples of a more general shape, a polygon. A polygon is an multi-sided figure. So a square is a polygon with 4 sides and a pentagon is a polygon with 5 sides. If you could design a polygon(N) procedure, then you could use it to draw a square or a pentagon or hexagon (6 sides) or octagon (8 sides) or even approximate a circle (36 sides?). So give it a try. There’s quite a payoff if you can do it.

HINT: Your procedure will need 2 parameters, N, and L, where N is the number of sides (e.g., 4, 5, 6, etc.) and L is the length of each side.

HINT: A 4-sided figure has 4 sides and turns by 360/4 degrees. A 5-sided figure has 5 sides and turns by 360/5 degrees.

Test your polygon() procedure by using it to draw a hexagon (6 sides) and a octagon (8 sides). Again, you will have to call your procedures from the draw() procedure.

5. Use your drawPolygon() procedure to draw a circle. This exercise might require some trial and error to get the the number of sides and the length of the sides right. Does a 36-sided polygon look like a circle?

6. Draw a flower by repeatedly drawing a square and turning right by some number of degrees. (NOTE: To change the color of the drawing pen you need to call the setPenColor() method. If you want a random color you can use the getRandomColor() method that is provided as a parameter like this: setPenColor(getRandomColor()). An example similar to the diagram below is provided in the project file.

7. Draw a flower with some missing petals -- i.e., use if/else and randomness.

8. Design and draw your own shapes, including flowers, spirals, stars. For example, here’s an interesting flower-like shape that was made by rotating a circle:

*1 point*