Exercise 2 Final Result
I solved this problem by creating a function that does everything that Exercise 2 requires. Here is an example:
turtle_star(n=10, length=20, type=3, width=3)Here is another example:
turtle_star(n=15, length=30, color="blue", type=1, width=7)Here is another example:
turtle_star(n=360, length=20, color="lightblue", type=1, width=1)If the n value is large enough, it will look like a solid figure. In reality, it is still a lot of rays.
Exercise 2 Hint
Recall the second practice problem from section 5.3.2, asking the user to create a function called superstar8(). This function gave the user options to change the width and color of the lines that the turtle is making. Here is the solution to that problem:
superstar8 <- function(width, color = "burlywood") {
turtle_init(mode = "clip")
turtle_lwd(lwd = width)
turtle_col(col = color)
turtle_do({
for ( i in 1:8 ) {
turtle_forward(20)
turtle_backward(20)
turtle_turn(45)
}
})
}There are three main differences within the parameters of superstar8() and the function we have been asked to create, turtle_star. turtle_star uses three additional parameters: n, being the number of rays; length, being the length of the line; and type, being the type of line. In this hint, I will show you how to add the n option to this function.
Step 1: Add the Parameter
First, we will need to add the parameter to the function. As of right now, there is no value n in superstar8(). Exercise 2 also asks for the default value of n to be 6. Here is how you add that parameter with the default value for n:
When defining the function, add the parameter.
superstar8 <- function(n=6, width, color = "burlywood") {
turtle_init(mode = "clip")
turtle_lwd(lwd = width)
turtle_col(col = color)
turtle_do({
for ( i in 1:8 ) {
turtle_forward(20)
turtle_backward(20)
turtle_turn(45)
}
})
}Let’s test this function, overriding the default value of n when we call the function. We want the turtle to draw 12 rays instead of the default, being 6.
superstar8(n=12, width=5, color="burlywood")As we can see, we get 8 rays. We don’t get the default value, or the assigned value when we call the function. It’s almost as if the function is ignoring the values we hae given it. The next step will help us get closer to solving this issue.
Step 2: Add the Value n to the Body of the Function
Next, we need incorporate n as a parameter in the function. We do this by adjusting the for() loop. We replace the last number in the loop with the value n.
superstar8 <- function(n=6, width, color = "burlywood") {
turtle_init(mode = "clip")
turtle_lwd(lwd = width)
turtle_col(col = color)
turtle_do({
for ( i in 1:n ) {
turtle_forward(20)
turtle_backward(20)
turtle_turn(45)
}
})
}Here, we have told the for() loop not to stop at a set number, but instead to stop at the n value that the user supplies. Now, if we use the default value n, or if we were to override this value when we call the function, it should give us that value.
superstar8(n=12, width=5, color="burlywood")Oops! 8 rays instead of 12.
We still get 8 rays. We get this because of the turtle_turn part of the function, it tells the turtle to turn 45 degrees after it makes each ray. Since 45 times 8 is 360, the new lines that the turtle is creating overlap with the others, so the user is unable to see the new lines.
Step 3: Create Evenly Distributed Lines
In both superstar8() and turtle_star, the lines are evenly spaced around the starting position of the turtle. To do that, we must know that the turle can rotate 360 degrees and end up facing the same direction. If we compute \(360/n\), then that should evenly space the lines within the rotation.
Here is a table representing why \(360/n\) is the correct expression:
| Number of Rays | Degree Turn |
|---|---|
| 4 | \(90=360/4\) |
| 8 | \(45=360/8\) |
| 360 | \(1=360/360\) |
| \(n\) | \(\text{rays}=360/n\) |
superstar8 <- function(n=6, width, color = "burlywood") {
turtle_init(mode = "clip")
turtle_lwd(lwd = width)
turtle_col(col = color)
turtle_do({
for ( i in 1:n ) {
turtle_forward(20)
turtle_backward(20)
turtle_turn(360/n)
}
})
}Now we can test this function, as we have changed the turtle_turn to evenly distribute the lines around 360 degrees.
superstar8(n=12, width=5, color="burlywood")We got the 12 rays that we had set up when calling the function.
Last Step
To conclude, there are still two other parameters that need to be added to solve Exercise 2. Within this hint, we have added the parameter n, to allow us to assign a different number of rays. When we added these rays, however, we still had to make sure that they were evenly distributed across the 360 degrees. In order to solve Exercise 2, you wil need to add the type parameter and the length parameter.