Created
February 13, 2020 22:49
-
-
Save shuuki/c200d6508ef443879bb646d86280438c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- z to grow a new tree | |
-- x to grow a branch | |
-- up arrow to select +1 | |
-- down arrow to select -1 | |
-- right arrow to select newest | |
-- left arrow to select oldest | |
function branch(tree,ox,oy,length,depth,angle,spread,parent,child) | |
-- new xy from origin | |
local nx = ox + length * cos(angle) | |
local ny = oy + length * sin(angle) | |
--circfill(ox,oy,1,9) | |
--line(ox,oy,nx,ny,10) | |
-- set parent index | |
local parent = parent or 0 | |
local child = child or {} | |
local current = #tree+1 | |
-- first branch creates a parent from origin | |
-- otherwise tells its parent the good news | |
if parent == 0 then | |
tree[parent] = {x=ox,y=oy,c={current}} | |
else | |
add(tree[parent].c,current) | |
end | |
-- write to tree | |
tree[current] = {x=nx,y=ny,p=parent,c=child} | |
-- recurse branch | |
if depth > 0 and length >= 1 then | |
-- special sauce per step | |
depth -= 1 | |
length = length | |
branch(tree,nx,ny,length,depth,angle+spread,spread,current) | |
branch(tree,nx,ny,length,depth,angle-spread,spread,current) | |
end | |
end | |
function drawtree(tree) | |
local nodecolor,linecolor | |
for i=1,#tree do | |
if i == highlight then | |
nodecolor = 12 | |
linecolor = 12 | |
else | |
nodecolor = 9 | |
linecolor = 5 | |
end | |
-- origin xy | |
local ox = tree[i].x | |
local oy = tree[i].y | |
-- parent xy | |
local px = tree[tree[i].p].x | |
local py = tree[tree[i].p].y | |
line(px,py,ox,oy,linecolor) | |
circfill(ox,oy,1,nodecolor) | |
end | |
end | |
--- the tree ---------- | |
bonsai = {} | |
highlight = 1 | |
function _init() | |
local defaultx = 64 | |
local defaulty = 120 | |
local defaultlength = 16 | |
local defaultdepth = 1 | |
local defaultangle = 0.25 | |
local defaultspread = 0.11 | |
function spawntree() | |
bonsai = {} | |
highlight = 1 | |
branch(bonsai,defaultx,defaulty,defaultlength,defaultdepth,defaultangle,defaultspread) | |
end | |
spawntree() | |
end | |
function _update() | |
-- z to snip at highlight | |
if btnp(4) then | |
-- z to generate new tree | |
--spawntree() | |
end | |
-- x to branch at highlight | |
if btnp(5) then | |
-- step calculations | |
local growx = bonsai[highlight].x | |
local growy = bonsai[highlight].y | |
local growlength = 4+(rnd()+rnd()+rnd()+rnd()) | |
local growdepth = 1 | |
local growangle = 0.5-rnd()/2 | |
local growspread = 0.11 | |
branch(bonsai,growx,growy,growlength,growdepth,growangle,growspread,highlight) | |
end | |
-- left and right to snap highlight | |
if btnp(0) then | |
highlight = 1 | |
end | |
if btnp(1) then | |
highlight = #bonsai | |
end | |
-- up and down to highlight node | |
if highlight >= 1 and highlight <= #bonsai then | |
if btnp(3) then | |
highlight -= 1 | |
end | |
if btnp(2) then | |
highlight += 1 | |
end | |
end | |
-- fix out of bounds highlight | |
if highlight < 1 then | |
highlight = 1 | |
end | |
if highlight > #bonsai then | |
highlight = #bonsai | |
end | |
end | |
function _draw() | |
cls() | |
drawtree(bonsai) | |
print("size "..#bonsai,0,0,10) | |
print("selected "..highlight,0,7,12) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment