43 lines
1.1 KiB
Text
43 lines
1.1 KiB
Text
require "bitmap"
|
|
require "table2"
|
|
|
|
local bmp = bitmap.of(800, 600, color.black, "Dragon curve")
|
|
|
|
local function get_sequence(iterations)
|
|
local turn_sequence = {}
|
|
for _ = 1, iterations do
|
|
local copy = {}
|
|
copy:join(turn_sequence)
|
|
if #copy > 1 then copy:reverse() end
|
|
turn_sequence:insert(1)
|
|
copy:foreach(|i| -> turn_sequence:insert(-i))
|
|
end
|
|
return turn_sequence
|
|
end
|
|
|
|
local function dragon(turns, starting_angle, side)
|
|
local col = pico.blue
|
|
local angle = starting_angle
|
|
local x1 = 230
|
|
local y1 = 350
|
|
local x2 = x1 + math.trunc(math.cos(angle) * side)
|
|
local y2 = y1 + math.trunc(math.sin(angle) * side)
|
|
bmp:line(x1, y1, x2, y2, col)
|
|
x1 = x2
|
|
y1 = y2
|
|
for turns as turn do
|
|
angle += turn * math.pi / 2
|
|
x2 = x1 + math.trunc(math.cos(angle) * side)
|
|
y2 = y1 + math.trunc(math.sin(angle) * side)
|
|
bmp:line(x1, y1, x2, y2, col)
|
|
x1 = x2
|
|
y1 = y2
|
|
end
|
|
end
|
|
|
|
local iter = 14
|
|
local turns = get_sequence(iter)
|
|
local starting_angle = -iter * math.pi / 4
|
|
local side = 400 / (2 ^ (iter / 2))
|
|
dragon(turns, starting_angle, side)
|
|
bmp:view()
|