// You can find the Turtle API reference here: https://turtletoy.net/syntax
Canvas.setpenopacity(0.5);

// Global code will be evaluated once.
const turtle = new Turtle();

// The walk function will be called until it returns false.

buffer_x = 14
buffer_y = -85
grid_size = 13
rose_size = 5

function walk(i) {
    
    const x = i % grid_size;
    const y = i / grid_size >> 0;
    
    
    center_x = buffer_y + x * buffer_x
    center_y = buffer_y + y * buffer_x
    k = (x+1)/(y+1)
    
    turtle.up();
    turtle.goto(center_x+rose_size,center_y);
    turtle.down();
    
    bound = 360//2*Math.pi
    
     /*
    Where k is even, the entire graph of the rose will be traced out exactly once when the value of theta, θ changes from 0 to 2π. 
    When k is odd, this will happen on the interval between 0 and π. (More generally, this will happen on any interval of length 2π for k even, and π for k odd.)

    If k is a half-integer (e.g. 1/2, 3/2, 5/2), the curve will be rose-shaped with 4k petals. Example: n=7, d=2, k= n/d =3.5, as θ changes from 0 to 4π.

    If k can be expressed as n±1/6, where n is a nonzero integer, the curve will be rose-shaped with 12k petals.

    If k can be expressed as n/3, where n is an integer not divisible by 3, the curve will be rose-shaped with n petals if n is odd and 2n petals if n is even.
    
    */
    if(k%2 == 0){
        bound = 360
    } else if(k%2 != 0 && Math.round(k) == k){
        bound = 360/2
    }
    
    
    for(theta=0;theta<=bound;theta+=0.1){
        turtle.goto(
            center_x+rose_size*Math.cos(k*theta)*Math.cos(theta),
            center_y+rose_size*Math.cos(k*theta)*Math.sin(theta)
            );
        
        
    }
    turtle.up()
 
    
    return i < grid_size*grid_size-1;
}