// Uses grid code from: Lissajous curves. Created by Reinder Nijhoff 2018
// @reindernijhoff

// Released under the MIT licence 
// https://mit-license.org/
// you can use this for commercial gain if you like eg you can sell artworks with this image.


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

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


// cardiod_modulo_n_455_500
// How to calculate points on a circle
// Based on code from http://www.mathopenref.com/coordcirclealgorithm...


/* 
There are 2 * PI Radians in a circle
If we have a circle of radius 20 with its center at the origin, the circle can be described by the pair of equations
x = 20 cos(t) 
y = 20 sin(t)
also pased on https://www.youtube.com/watch?v=qhbuKbxJsk8&amp... 
Times Tables, Mandelbrot and the Heart of Mathematics
by Burkard Polster and Giuseppe Geracitano
see: http://codepen.io/hippiefuturist/full/NAvqgk/

Artwork on Redbubble at: https://www.redbubble.com/shop/ap/24619400
Code on Github at: https://github.com/rupertrussell/cardiod_modulo_n_455_500

*/
const count = 4; //min=1, max=5, step=1
const s = 195;//min=1, max=200, step=1
// const r = 11 //min=1, max=100, step=1

const radius=22; //min=10 max=100 step=1
const n = 119; //min=10 max=500 step=1
const fineN = 0; // //min=-5 max=5 step=0.01 
const m = 90; //min=10 max=455 step=1
const fineM = 0; //min=-10 max=10 step=1 

var xOffset = 0; 
var yOffset = 0; 

const turtles = [];
// create multiple turtles and space them out in a grid
for (let x=0; x<count; x++) {
    for (let y=0; y<count; y++) {
        turtles.push(new Turtle((x + .5) * s/count - s/2, (y + .9 + yOffset) * s/count - s/2));
    }
}


for (var i = 0; i < n + fineN; i++) {
    connectPoints(radius, i, i * m + fineM);
}


function connectPoints(r, firstPoint, secondPoint) {
    // Connect two points on a circle

    var step = 2 * Math.PI/n; 

    for (let x=0; x<count; x++) {
        for (let y=0; y<count; y++) {
            
        turtles[y+x*count].pendown();
            
        xOffset = (x + .5) * s/count - s/2;
        yOffset = (y + .5)  * s/count - s/2;
        // draw n points on circle
        const x1 = xOffset + r * Math.cos(firstPoint * step);
        const y1 = yOffset - r * Math.sin(firstPoint * step);
        const x2 = xOffset + r * Math.cos(secondPoint * step);
        const y2 = yOffset -r * Math.sin(secondPoint * step);

        turtles[y+x*count].jump(x1, y1);
        turtles[y+x*count].goto(x2, y2);
        
            
        }
    }
   
}

function getRandomInt(max) {
  return Math.floor(Math.random() * max);
}

console.log(getRandomInt(3));
// Expected output: 0, 1 or 2