info = { type = "image", title = "Mondrian drift", category = "patterns", author = "BRRRO", version = "1.0", license = "MIT", desc = "Mondrian rectangles that split and merge", api = 33, } local tr = ({ sk = { ["Speed"] = "Rýchlosť", ["Slow"] = "Pomaly", ["Normal"] = "Normálne", ["Fast"] = "Rýchlo" }, cs = { ["Speed"] = "Rychlost", ["Slow"] = "Pomalu", ["Normal"] = "Normálně", ["Fast"] = "Rychle" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "speed", label = T("Speed"), type = "int", default = "normal", opts = { keys = { "slow", "normal", "fast" }, T("Slow"), T("Normal"), T("Fast") } }, } local SPEED = { slow = 0.5, normal = 1, fast = 2 } local rect, blend_rect = gfx.rect, gfx.blend_rect local floor, ceil, sin = math.floor, math.ceil, math.sin local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local TAU = 6.28318 local PAL = { { 222, 28, 28 }, { 250, 206, 24 }, { 24, 70, 206 }, { 244, 240, 232 } } local WD = 1.4 local N = { { o = "v", p = 26, a = 2.5, per = 26, l = 2, r = 3 }, { o = "h", p = 20, a = 3, per = 31, l = 4, r = 5 }, { o = "h", p = 34, a = 3.5, per = 23, l = 7, r = 8 }, { o = "v", p = 10, a = 2.5, per = 29, pair = true }, { o = "h", p = 46, a = 3, per = 34, l = -3, r = 6 }, { o = "v", p = 11, a = 2, per = 27, pair = true }, { o = "v", p = 48, a = 3, per = 33, l = 9, r = -6 }, { o = "v", p = 44, a = 3, per = 25, l = 10, r = 11 }, { o = "h", p = 12, a = 3, per = 30, pair = true }, { o = "h", p = 50, a = 3, per = 28, pair = true }, { o = "h", p = 52, a = 2.5, per = 35, pair = true }, } local SOLO = {} for i = 1, #N do local h = hash32(i * 0x9E3779B1) local nd = N[i] nd.off = umod(h, 1000) / 1000 * nd.per if nd.pair then nd.Q, nd.off2, nd.sd, nd.fr = 20 + umod(h >> 10, 6) * 1.5, umod(h >> 14, 2000) / 100, i, umod(h >> 8, 2) end end SOLO[3] = { T = 11, off = 2, sd = 31 } SOLO[6] = { T = 14, off = 7, sd = 61 } local function hv(sd, k, j, m) return umod(hash32(sd * 977 + k * 7919 + j * 131), m) end local function fillf(x0, y0, x1, y1, c) if x1 - x0 <= 1 or y1 - y0 <= 1 then return end local r, g, b = c[1], c[2], c[3] local ix0, iy0, ix1, iy1 = ceil(x0), ceil(y0), floor(x1), floor(y1) rect(ix0, iy0, ix1 - ix0, iy1 - iy0, r, g, b) local f = ix0 - x0 if f > 0.03 then blend_rect(ix0 - 1, iy0, 1, iy1 - iy0, r, g, b, f) end f = x1 - ix1 if f > 0.03 then blend_rect(ix1, iy0, 1, iy1 - iy0, r, g, b, f) end f = iy0 - y0 if f > 0.03 then blend_rect(ix0, iy0 - 1, ix1 - ix0, 1, r, g, b, f) end f = y1 - iy1 if f > 0.03 then blend_rect(ix0, iy1, ix1 - ix0, 1, r, g, b, f) end end local function ins(a0, a1, lim) if a0 > 0 then a0 = a0 + 1 end if a1 < lim then a1 = a1 - 1 end return a0, a1 end local function cell(x0, y0, x1, y1, c0, c1, p, d) x0, x1 = ins(x0, x1, W) y0, y1 = ins(y0, y1, H) fillf(x0, y0, x1, y1, PAL[c0 + 1]) if p <= 0 then return end local c = PAL[c1 + 1] if d == 0 then fillf(x0, y0, x0 + (x1 - x0) * p, y1, c) elseif d == 1 then fillf(x1 - (x1 - x0) * p, y0, x1, y1, c) elseif d == 2 then fillf(x0, y0, x1, y0 + (y1 - y0) * p, c) else fillf(x0, y1 - (y1 - y0) * p, x1, y1, c) end end local function wipe(ct, t0, c0, c1) local u = (ct - t0) / WD if u <= 0 then return c0, c1, 0 end if u >= 1 then return c1, c1, 0 end return c0, c1, u * u * (3 - 2 * u) end local function ease(u) if u <= 0 then return 0 elseif u >= 1 then return 1 end return u * u * (3 - 2 * u) end local function solo(id, x0, y0, x1, y1, tt) local S = SOLO[id] local u = (tt + S.off) / S.T local k = floor(u) local function col(j) return j % 2 + 2 * hv(S.sd, j, 1, 2) end local c0, c1, p = wipe((u - k) * S.T, S.T - WD, col(k), col(k + 1)) cell(x0, y0, x1, y1, c0, c1, p, hv(S.sd, k, 2, 4)) end local function pair(nd, s, x0, y0, x1, y1, tt) local q = tt + nd.off2 local k = floor(q / nd.Q) local ct = q - k * nd.Q local sd, par = nd.sd, k % 2 local bp, bq, bk = hv(sd, k - 1, 3, 2), hv(sd, k - 2, 3, 2), hv(sd, k, 3, 2) local a0, b0 = (1 - par) + 2 * bq, par + 2 * bp local cA2, cM, cB = par + 2 * (1 - bp), par + 2 * bp, (1 - par) + 2 * bk local d = hv(sd, k, 2, 4) local ac0, ac1, ap if ct < 12 then if ct < 3 then ac0, ac1, ap = a0, a0, 0 else ac0, ac1, ap = wipe(ct, 3, a0, cA2) end else ac0, ac1, ap = wipe(ct, 12, cA2, cM) end local g = 1 if ct >= 8 and ct < 15 then g = 1 - ease((ct - 8) / 1.6) elseif ct >= 15 then g = ease((ct - 15) / 1.6) end local v = nd.o == "v" if g <= 0 then cell(x0, y0, x1, y1, ac0, ac1, ap, d) return end local bc0, bc1, bp2 if ct < 7.4 then bc0, bc1, bp2 = wipe(ct, 6, b0, cA2) elseif ct < 15 then bc0, bc1, bp2 = cA2, cA2, 0 elseif ct < 17 then bc0, bc1, bp2 = cM, cM, 0 else bc0, bc1, bp2 = wipe(ct, 17, cM, cB) end if v then cell(x0, y0, s, y1, ac0, ac1, ap, d); cell(s, y0, x1, y1, bc0, bc1, bp2, d) else cell(x0, y0, x1, s, ac0, ac1, ap, d); cell(x0, s, x1, y1, bc0, bc1, bp2, d) end if g < 1 then local c = PAL[(ap > 0 and ac1 or ac0) + 1] local lo, hi = ins(v and y0 or x0, v and y1 or x1, v and H or W) local cut = (hi - lo) * g if nd.fr == 1 then lo, hi = lo, hi - cut else lo, hi = lo + cut, hi end if v then fillf(s - 1, lo, s + 1, hi, c) else fillf(lo, s - 1, hi, s + 1, c) end end end local function walk(n, x0, y0, x1, y1, tt) if n < 0 then solo(-n, x0, y0, x1, y1, tt) return end local nd = N[n] local s = nd.p + nd.a * sin(TAU * (tt + nd.off) / nd.per) if nd.pair then pair(nd, s, x0, y0, x1, y1, tt) elseif nd.o == "v" then walk(nd.l, x0, y0, s, y1, tt); walk(nd.r, s, y0, x1, y1, tt) else walk(nd.l, x0, y0, x1, s, tt); walk(nd.r, x0, s, x1, y1, tt) end end function render() gfx.clear(0, 0, 0) walk(1, 0, 0, W, H, frame / FPS * (SPEED[cfg.speed] or 1)) end