Hexagons
Log in to post a comment.
// 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();
const steps=60; // min=2, max=300, step=1
const size=10; // min=2, max=50, step=1
const density=5; // min=1, max=10, step=1
var currentx = 0;
var currenty = 0;
var proposedx = 0;
var proposedy = 0;
function randomNumber(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
function hexwedge(radius) {
var width = 0;
var distance =0;
while (distance <= radius) {
turtle.penup();
turtle.forward(.5);
distance = distance + .5;
if (randomNumber(1,10)<density) {
width = Math.tan(30* (Math.PI / 180))*distance
turtle.left(90);
turtle.forward(width);
turtle.pendown();
turtle.backward(2*width);
turtle.penup();
turtle.forward(width);
turtle.right(90);
}
}
turtle.backward(radius);
}
function hexagon(radius) {
turtle.seth(0);
hexwedge(radius);
turtle.seth(60);
hexwedge(radius);
turtle.seth(120);
hexwedge(radius);
turtle.seth(180);
hexwedge(radius);
turtle.seth(240);
hexwedge(radius);
turtle.seth(300);
hexwedge(radius);
turtle.seth(0);
}
// The walk function will be called until it returns false.
function walk(i) {
switch(randomNumber(1,6)) {
case 1:
turtle.seth(0);
break;
case 2:
turtle.seth(60);
break;
case 3:
turtle.seth(120);
break;
case 4:
turtle.seth(180);
break;
case 5:
turtle.seth(240);
break;
case 6:
turtle.seth(300);
break;
}
turtle.penup();
turtle.forward(2*size);
if (turtle.xcor() < 100 && turtle.xcor() > -100 && turtle.ycor() < 100 && turtle.ycor() > -100) {
turtle.pendown();
hexagon(size);
} else {
turtle.backward(2*size);
}
return i < steps;
}