For this drawing, I needed to write a method called multistar(Turtle t, int n, double length)
that makes the turtle draw a star like the one shown below. The parameter n
specifies the number of rays (rays meaning the set of lines passing through a single point). The parameter length
indicates the size of the long rays (the short rays are 1/4 the length of the long rays).
The Code
public class Multistar {
public static void main (String [] arguments) {
Turtle sammy = new Turtle (); // called the turtle sammy
multistar(sammy, 7, 100);
}
Above the is main
method, which houses all our code and executions. At line 3, I create and execute the Turtle named sammy
and then call upon the multistar
method. Again, notice a }
is missing for the Multistar
class, this is because the code will continue in the next code block.
public static void multistar (Turtle sammy, int n, double length) {
for (int i = 0; i < n; i ++) {
sammy.right(360.00/n); // the angle of the larger rays
sammy.forward(length); // length of the larger rays
// before going back to the origin; create the "baby rays"
for (int j = 0; j < n; j ++) {
sammy.forward(length/4); // baby rays are 1/4 the length
sammy.right(360.00/n); // angle of the baby rays divided by the number of rays
sammy.backward(length/4); // back to the origin of the baby ray
}
sammy.backward(length); // back to the origin of the larger ray
}
}
}
Above is the multistar()
method. Starting at line 2, a for-loop
is implemented which creates the 7 long rays, however, before returning to origin, another for-loop
is implemented at line 7 creating the 7 smaller rays. Again, I put the turtle back in its original starting position before ending the method.
The Result
Below are some other examples of the multistar, playing with the colour and number of rays.
Back to Introduction to Turtle Graphics | Polyspiral Project