In the last activity we assumed that the user wanted a square (a regular polygon with 4 sides) and we only allowed the user to modify the length of each side by changing and recompiling the code. In this activity we want to make a MUCH more dynamic robot.
GOAL :
Travel the perimeter of an n-sided regular polygon with sides of length m cm. [Review time: Recall that a regular polygon is one which is both equiangular and equilateral.]
DETAILS :
Your task is to program your RoverBot so that it can travel the perimeter of an n-sided regular polygon with sides of length m cm. If inputs of 4 and 50 are provided to your robot it should behave exactly the same as it did for Activity #1. However, if inputs of 3 and 100 are provided, it should produce an equilateral triangle – travel forward exactly one meter, form an angle of 60 degrees by turning 120 degrees, repeat 2 more times, arriving back at the exact starting point.
The first thing you will need to do is to modify your code so that the number of sides (n) isn't hardcoded to 4 as it was in the last activity (you did have a nice loop rather than 4 sets of the same commands didn't you???).
For starters, you might introduce a variable for this value and require it to be modified in the code to make the changes. However, your finished ShapeTracer should give the user a simple menu system when the program first starts to run (Using the TextMenu class that we looked at in "lecture" and was used in the "View" program we used on day two). This should have the user select from some predetermined values of n (probably 3-8 is sufficient) and then some predetermined values for m (perhaps multiples of 10 or 20 cms from 40 cm to 140 cm depending on what you actually choose)
One challenge is know how far to turn at each corner. This distance changes depending on the shape. You turn 90 degrees for a square but 45 degrees for an octagon. The value is linked to whether the number entered is a 3 or a 4 or even an 8 (this will produce an octagon like a stop sign). To help with this, recall the geometric formula that each interior angle of an n-sided polygon is equal to
(n-2)*180 / n
I would like your code to, in theory, handle any positive values of n and m. This will make this code more adaptable for future projects. However, we will perform all of our testing with m=100 cm. Furthermore, I will not test your RoverBot with any value n>8 due to time (remember, each side is 100 cm long so a dodecahedron takes a while to trace out) and space (figure out how wide across said dodecahedron is). However, let me repeat this, your code should handle any positive value of n. That is, you shouldn’t hard code what to do with n=3, n=4, etc. but instead should use the geometric formula that each angle of an n-sided polygon is equal to (n-2)/n*180.
Finally, you may wish to explore (although you are not required to do so) the Pilot class which is a class which MIGHT help with some of the base navigation. Some students really liked Pilot. Others found it more trouble than it was worth. The choice is yours.