Coding a Sine Wave in TurtleStitch Using the Sine Wave Equation y = a sin(Fx)
Sashiko design called Hokusai
After coding the Sashiko design called Hokusai, Cynthia Solomon challenged me to code a sine wave with just Arcright/Arcleft. While the sine wave is not a circle, the arc commands can emulate the sine wave in small segments with adjustments made by a turn block.
In the following TurtleStitch program https://www.turtlestitch.org/run#cloud:Username=Elaine&ProjectName=Sine%20Wave%20Equation, I created three routines to make a sine wave.
I used the following equation to code the sine wave.
Sine Wave Equation Code
y = a sin(Fx)
where:
a is the amplitude - makes the wave taller or shorter
F is the frequency - how close the waves are together
This code creates an exact replica of a sine wave.
Arcright/Arcleft Code
This code uses arcright/arcleft to make the sine wave.
No calculations were made, just visual clues were used to code the sine wave. I coded this by using the original sine wave and then tracking the course of the curve with the turtle. I used a different pen color to illustrate the arc's path. With trial and error, using different values of arcs and turns I created this sine wave.
The red curve is the calculated sine wave equation and the blue curve is the arcright/arcleft. You will notice the the blue curve is not an exact replica of the original because it is difficult to achieve the exact path with trial and error.
Turns and Moves Code
This code uses Turns and Moves to make the sine wave. I broke up the code into four sections so that you could see the receptive nature of the code.
No calculations were made, just visual clues were used to code this sine wave. I coded this by using the original sine wave and then tracking the course of the curve with the turtle. I used a different pen color to illustrate its path. With trial and error, using different values of turns and moves, I created this sine wave.
The red curve is the calculated sine wave equation and the green curve is the turn/moves. Notice the green curve is not an exact replica of the coded "Sine Wave" because it is tedious to follow the path of the original sine wave with trial and error.
Comparing the Three Methods of Coding a Sine Wave
While all three sine waves look similar, only one of these waves is exactly correct.
The red sine wave is the coded sine wave equation. The blue and green sine waves are an approximation.
It is also interesting to see the number of blocks that each method has used to create the sine wave. The first column is the sine wave equation. The second column is the arcright/arcleft blocks. The third column onward is the turn and move blocks.
In conclusion, I think it is very interesting to compare and contrast the three coding methodologies. It shows that there are multiple ways to achieve a goal when accuracy doesn't count. When precision is required, coding with equations is the simplest and most accurate method of coding in TurtleStitch.