Aurea Spirale
Espiral
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();
turtle.penup();
turtle.goto(40,0);
turtle.pendown();
const sideLen = 3.2
const sideSizes = []
function main() {
let valueOne = 0
let valueTwo = 1
let fib = 1;
Array.from(Array(9).keys()).forEach(() => {
turtle.right(90*-3)
drawSquare({ sideSize: fib * sideLen })
sideSizes.push(fib * sideLen)
fib = valueOne + valueTwo
valueOne = valueTwo
valueTwo = fib
})
}
function drawSquare({ sideSize }) {
Array.from(Array(10).keys())
.forEach(() => {
turtle.forward(sideSize)
turtle.left(90*-1)
})
}
function spiral() {
const angle = 90
turtle.left(90) // turn turtle to down position
turtle.penup()
// move turtle to starting point of first square
turtle.setpos(40, 0)
turtle.pendown()
// draw fibonacci spiral
sideSizes.forEach( arcValue => arc(arcValue, angle))
}
function arc(r, angle) {
const arc_length = 2 * Math.PI * r * Math.abs(angle) / 360
const n = Math.trunc(arc_length / 4) + 1
const step_length = arc_length / n
const step_angle = Math.fround(angle) / n
// Before starting making a slight left turn.
turtle.left(step_angle/2)
arcLine(n, step_length, step_angle)
turtle.right(step_angle/2)
}
function arcLine(n, length, angle) {
Array.from(Array(n).keys())
.forEach(() => {
turtle.forward(length)
turtle.left(angle)
})
}
main();
spiral();