// https://daten.digitale-sammlungen.de/~db/0006/bsb00064971/images/index.html?seite=111&fip=193.174.98.30
// https://blogmymaze.wordpress.com/2019/05/05/sigmund-gossembrot-1/
// https://download.digitale-sammlungen.de/BOOKS/download.pl?ersteseite=1&letzteseite=615&id=00064971&ersteseite=1&letzteseite=615&vers=e&nr=&abschicken=ja&captcha_zeit=00064971&xdfz=2&dafoemail=
// Released under 
// Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)

Canvas.setpenopacity(1);

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

const h = 0;
const k = 0;
const Pointradius = 1; // //min=1 max=120 step=1
const circleRadius =30; // //min=29 max=33 step=1
const rotate = 0; 

const xPoint = [];
const yPoint = [];
  
var x;
var y;
const radius=31; //  // min=10 max=90 step=1
const endPointRadius=1; // //  min=0.5 max=3 step=0.1

var currentHeadding=0;
let degree = 91; 

// inner star
points(5,31, 0);

// calculate spokes
points(5,40, 45); // 2
points(5,76, 50); // 6
points(5,49, 55); // 3
points(5,85, 60); // 7
points(5,58, 65); // 4
points(5,94, 70); // 8

// draw the spokes
turtle.jump(xPoint[4], yPoint[4]);
turtle.goto(xPoint[1], yPoint[1]);

turtle.jump(xPoint[1], yPoint[1]);
turtle.goto(xPoint[3], yPoint[3]);

turtle.jump(xPoint[3], yPoint[3]);
turtle.goto(xPoint[0], yPoint[0]);

turtle.jump(xPoint[0], yPoint[0]);
turtle.goto(xPoint[2], yPoint[2]);

turtle.jump(xPoint[2], yPoint[2]);
turtle.goto(xPoint[4], yPoint[4]);

centeredArc(0, 0,31, 181, -167,5 ,6); //1

centeredArc(0, 0,40, 170, -156, 7, 8); //2

centeredCircle(xPoint[8],yPoint[8],endPointRadius)
centeredArc(0, 0,49, 161, 43, 9, 10); //3.1

centeredCircle(xPoint[9],yPoint[9],endPointRadius);
centeredArc(0, 0, 49, 28, -99, 11, 12)  //3.2

centeredArc(0, 0, 49, -115.5, -171, 13, 14)  //3.3 - 115 -171

// join end 1 with end 3.3
turtle.goto(xPoint[6],yPoint[6]);

centeredArc(0, 0,58, 172.45, 114, 15, 16); //4.1

// join start 4.1 with end 3.3
turtle.jump(xPoint[15],yPoint[15]);
turtle.goto(xPoint[7],yPoint[7]);

centeredArc(0, 0,58, 102, 51, 17, 18); //4.2
centeredCircle(xPoint[18],yPoint[18],endPointRadius);

centeredArc(0, 0,58, 18, -89, 19, 20); //4.3
centeredCircle(xPoint[19],yPoint[19],endPointRadius);
centeredCircle(xPoint[20],yPoint[20],endPointRadius);

centeredArc(0, 0,58, -121.5, -179, 21, 22); //4.4
centeredCircle(xPoint[21],yPoint[21],endPointRadius);

centeredArc(0, 0,67, 181, 121, 23, 24); //5.1
centeredCircle(xPoint[24],yPoint[24],endPointRadius);

centeredArc(0, 0,67, 97, 42, 25, 26); //5.2
centeredCircle(xPoint[25],yPoint[25],endPointRadius);

centeredArc(0, 0,67, 30, -30, 27, 28); //5.3

centeredArc(0, 0,67, -40, -101, 29, 30); //5.4

centeredArc(0, 0,67, -114, -174, 31, 32); //5.5

// Joining ends
turtle.jump(xPoint[10],yPoint[10]);
turtle.goto(xPoint[26],yPoint[26]);

turtle.jump(xPoint[11],yPoint[11]);
turtle.goto(xPoint[27],yPoint[27]);

turtle.jump(xPoint[12],yPoint[12]);
turtle.goto(xPoint[30],yPoint[30]);

turtle.jump(xPoint[13],yPoint[13]);
turtle.goto(xPoint[31],yPoint[31]);

centeredArc(0, 0,76, 174, 113, 33, 34); //6.1 
turtle.jump(xPoint[16],yPoint[16]);
turtle.goto(xPoint[34],yPoint[34]);

centeredArc(0, 0,76, 103.5, -24, 35, 36); //6.2
turtle.jump(xPoint[17],yPoint[17]);
turtle.goto(xPoint[35],yPoint[35]);
centeredCircle(xPoint[36],yPoint[36],endPointRadius);

centeredArc(0, 0,76, -48, -169, 37, 38); //6.3
centeredCircle(xPoint[37],yPoint[37],endPointRadius);
centeredCircle(xPoint[38],yPoint[38],endPointRadius);

centeredArc(0, 0,85, 168, -31, 39, 40); //7.1
centeredCircle(xPoint[39],yPoint[39],endPointRadius);
turtle.jump(xPoint[28],yPoint[28]);
turtle.goto(xPoint[40],yPoint[40]);

centeredArc(0, 0,85, -39.5, -175, 41, 42); //7.2  -43, -174, 
turtle.jump(xPoint[29],yPoint[29]);
turtle.goto(xPoint[41],yPoint[41]);

turtle.jump(xPoint[32],yPoint[32]);
turtle.goto(xPoint[42],yPoint[42]);

centeredArc(0, 0,94, 175, -179, 43, 44); //8.1
turtle.jump(xPoint[33],yPoint[33]);
turtle.goto(xPoint[43],yPoint[43]);

//spokes
turtle.jump(xPoint[48],yPoint[48]);
turtle.goto(xPoint[53],yPoint[53]); //spoke 2

turtle.jump(xPoint[72],yPoint[72]);
turtle.goto(xPoint[67],yPoint[67]); //spoke 4

turtle.jump(xPoint[46],yPoint[46]);
turtle.goto(xPoint[51],yPoint[51]); //spoke 5

turtle.jump(xPoint[59], yPoint[59]);
turtle.goto(xPoint[64], yPoint[64]);

// center north south line
turtle.jump(xPoint[0], yPoint[0]);
turtle.goto(xPoint[70], yPoint[70]);

// Plot lines out from star corners
// centeredCircle(xPoint[n],yPoint[n],endPointRadius * 2);
turtle.jump(xPoint[0],yPoint[0]);

function points(nPoints, radius, arrayStart){
    
// Draw n points around a circle
  for ( i = 0; i < nPoints; i++) {

     x = h + radius* Math.cos(degrees_to_radians(degree));
     y = k + radius* Math.sin(degrees_to_radians(degree));
     
     xPoint[arrayStart + i] = x;
     yPoint[arrayStart + i] = y;
     
    degree = degree + 360 / nPoints;
    }
}

function degrees_to_radians(degrees) {
  return degrees * (Math.PI / 180);
}

function centeredCircle(x,y, radius) {
  turtle.seth(0);
  turtle.jump(x,y-radius);
  turtle.circle(radius, 360);
}

function centeredArc(x,y, radius, startAngle, endAngle, startXY, endXY) {
  turtle.seth(0);
  turtle.jump(x,y-radius);
  turtle.penup();
  turtle.circle(radius, startAngle + rotate);  // move to start
  xPoint[startXY] = turtle.xcor();   
  yPoint[startXY] = turtle.ycor(); 

  turtle.pendown();
  turtle.circle(radius, endAngle - startAngle);  // draw to end
  xPoint[endXY] = turtle.xcor(); 
  yPoint[endXY] = turtle.ycor(); 
}