Welcome to Coding Projects!
Hey there, welcome to Coding Projects! I'm Amya Moore, and this site documents my progress in the Computer Science field. On this site is a list of assignments organised by language. Each post describes the objective of the assignment and a link to the completed project. Each project has full documentation of the code, broken down into paragraphs, explaining what the code does and a short demonstration video found at the bottom of the post.

Multistar

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).

Image of Multistar: multistar(t, 7, 100)

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.

multistar(sammy, 10, 70)
multistar(sammy, 25, 120)
multistar(sammy, 7, 100)
multistar(sammy, 10, 70)

Back to Introduction to Turtle Graphics | Polyspiral Project