ComputerCraft Fractal Tree Morph | Woltvint.net

Fractal Trees rendered in Minecraft using the ComputerCraft mod.

I always wanted to try rendering some fractals on the ComputerCraft computers from the first time I saw "alongtimeago" played in this mod, and these are some of my results.

I used two mods for this (and the Replay mod to capture it)

ComputerCraft: Restitched for the rendering of the fractal trees, and Create Fabric to make a gimmicky way to control the morph animation speed with hand cranks (read completely unnecessary)

I had to relearn Lua to program this one, but it has been quite a pleasant time writing in it, although I am sure my code is not as good as it could have been had I known the language better.

There was also one error made at the start, and that was programming the whole thing in the game, which made it quite difficult to then get the code out of the game 😄

The thing that was the hardest during the few hours spent on this project was probably the line rendering, I have written a few line renderers in the past, but I tried to cheat while rendering the lines many times instead of doing it the right way because I didn't want to write the code for a proper one in the in-game terminal (i wrote way more lines trying to cheat it that I would have had if I just wrote it properly)

The code that was used to render the fractal tree:

tArgs = { ... }

if(tArgs[1] == "left" 
or tArgs[1] == "right" 
or tArgs[1] == "top" 
or tArgs[1] == "bottom" 
or tArgs[1] == "front" 
or tArgs[1] == "back")then
    mon = peripheral.wrap(tArgs[1])
else
    mon = term
end

if (tArgs[2] == nil) then
    rot = math.pi/6
else
    rot = math.pi/tArgs[2]
end

mon.setTextScale(0.5)

w, h = mon.getSize()

function round(x)
    bx = math.floor(x)

    nx = x - bx

    if (nx > 0.5) then
        bx = bx+1
    end

    return bx
end

function drawPoint(x,y)
    w, h = mon.getSize()

    if (x < 0 or x >= w) then return 0 end
    if (y < 0 or y >= h) then return 0 end

    mon.setCursorPos(round(x), round(y))
    mon.setBackgroundColor(colors.white)
    mon.write(" ")
end

function drawLine(x1,y1,x2,y2)
    dx = (x2 - x1)
    dy = (y2 - y1)

    if (math.abs(dx) >= math.abs(dy))
    then
        step = math.abs(dx)
    else
        step = math.abs(dy)
    end

    dx = dx / step
    dy = dy / step
    x = x1
    y = y1
    i = 0

    while (i <= step) 
    do
        drawPoint(x, y)
        x = x + dx
        y = y + dy
        i = i + 1
    end
end

function branch(x,y,r,l)
    local nx = (math.cos(r) * l) + x
    local ny = (math.sin(r) * l) + y

    local nl = l * 0.75

    drawLine(x,y,nx,ny)

    if (nl > 1) then
        branch(nx,ny,r - rot,nl)
        branch(nx,ny,r + rot,nl)
    end
end

mon.setBackgroundColor(colors.black)
mon.clear()

branch(w/2,h,-math.pi/2,20)

It was only after finishing the video I found out about an error I made in the line renderer that caused some of the pixels not to be drawn

Missing pixel

The problem was that this:

i = 1

Was supposed to look like this:

i = 0

It is fixed in the code above to work alright if anyone decides to copy it to their world.

The speed control is accomplished using a few simple gears and a speedometer from create connected to a comparator which feeds directly into the back of the computer.

Gears

Sun Nov 13 2022