xxxxxxxxxx
60
/*
* Draw a text (wireframe) using a LOGO / TURTLE type language with very small code
* see my other related sketchs
*
* jump table is optimized out by using the result of the jump table directly from an array
* this is smallest and will work great on platforms such as x86 (because jumping can cost quite a lot on instructions size)
* on x86 the stack can be used to push the commands and pop every iteration for example
*/
let lineLength = 64
let index = 0
function computeStartPosition() {
// compute start drawing position given as a precomputed index (a single instruction on x86)
let x = width / 2 - lineLength * 5 / 2 + lineLength
let y = height / 2 + lineLength / 2
// fix the pixels to the grid
x = Math.floor(x)
y = Math.floor(y)
index = (x + y * width) * 4
//
}
function setup() {
createCanvas(512, 512);
background(0);
computeStartPosition()
drawOnce()
}
function drawOnce() {
let commands = [-width, -1, width, 1, 0, -width, 1, 0, 1, width - 1, 1]
loadPixels()
for (let l = 0; l < commands.length; l += 1) {
let r = commands[l]
let c = abs(r) << 8
for (let i = 0; i < lineLength; i += 1) {
pixels[index + 0] = c
pixels[index + 1] = c
pixels[index + 2] = c
if (r == 0) {
index += 4
} else {
index += r * 4
}
}
}
updatePixels()
}
function draw() {
}