RosettaCodeData/Task/Pythagoras-tree/Pluto/pythagoras-tree.pluto
2026-04-30 12:34:36 -04:00

37 lines
981 B
Text

require "bitmap"
local bmp = bitmap.of(640, 640, color.white, "Pythagoras_tree")
local depth_limit = 7
local hue = 0.15
local function draw_tree(x1, y1, x2, y2, depth)
if depth == depth_limit then return end
local dx = x2 - x1
local dy = y1 - y2
local x3 = x2 - dy
local y3 = y2 - dx
local x4 = x1 - dy
local y4 = y1 - dx
local x5 = math.round(x4 + 0.5 * (dx - dy))
local y5 = math.round(y4 - 0.5 * (dx + dy))
-- Draw a square.
local col = bitmap.hsvColor((hue + depth * 0.02) * 360, 1, 1)
local v = {{x1, y1}, {x2, y2}, {x3, y3}, {x4, y4}}
bmp:polygon(v, col, 0)
bmp:polygon(v, pico.lightgray, 1)
-- Draw a triangle.
col = bitmap.hsvColor((hue + depth * 0.035) * 360, 1, 1)
v = {{x3, y3}, {x4, y4}, {x5, y5}}
bmp:polygon(v, col, 0)
bmp:polygon(v, pico.lightgray, 1)
draw_tree(x4, y4, x5, y5, depth + 1)
draw_tree(x5, y5, x3, y3, depth + 1)
end
draw_tree(275, 500, 375, 500, 0)
bmp:view()