A Python "function" is block of code that can be re-used.
Functions may include arguments (inputs) and a return value. (Think of y = f(x) from Algebra.)
Python tutorial on functions: https://www.w3schools.com/python/python_functions.asp
A function is useful when your program needs to do something multiple times. Instead of copying/pasting the code, you can define a function
and "call" it as needed.
Here's a start of creating a function name "polygon" -->
Review:
Given a polygon of n sides, what is the exterior angle that your robot needs to turn at each vertix (corner) of the polygon?
Write the code in the example. Use a function and name it "polygon."
As in the previous exercise, use the "wait for button press" block to control when the driving starts. Use a "repeat while true" loop to drive the polygon every time your press the user button .
Use variables specify the number of sides of the polygon and the length of the sides.
(Variables ... create variable)
Call the polygon function using these variables.
Question: What happens as you increase the number of sides (square, pentagon, hexagon ...?
Improve your polygon program to keep the XRP within a bounding box.
The radius of the polygon should be 10 cm.
The radius (R) of a polygon is the distance from the center of the polygon to any vertex. See diagram --->
We need to calculate the length of the side of the polygon (a) given the Radius (R).
What's the formula?
Review:
Write a function named "PolygonSide" that returns the side length "side" given the "Radius" and number of sides ("nSides").
Here's a start, but you need to change the sideLength calculation to match the formula you found in the previous step.
Run your program, trying several values of R.
Check your results using this polygon calculator.
If you are stuck, here's a solution:
polygon_side_function.blocks
Use the PolygonSide function in your program to drive a polygon of a selected number of sides.
Use the text ... "prompt for number" block to prompt the user to enter the number of sides.
See example, below
Note that there is an XRP Code Editor bug that prevents keyboard input:
https://github.com/Open-STEM/XRPCode/issues/61
Workaround:
RUN (uploads the program, but won't accept keyboard input)
STOP (stops the program)
In the shell area of the window, type CTRL-D, which restarts the program and allows keyboard input
Fixed in the v1.25.0-preview.bet06 release?
What does your program do when:
nSides = 0
nSides = 1
nSides = -1
nSides = 100
nSides = 100000
Get a dry erase marker, put it in the hole in your XRP, place the XRP on a whiteboard on the floor and draw a polygon.
Bonus if you've studied calculus: How does this experiment correspond to mathematical limit theory?