info = { type = "image", title = "Atom", category = "other", author = "BRRRO", version = "1.0", license = "MIT", desc = "Electrons orbit a pulsing nucleus", api = 33, } local tr = ({ sk = { ["Colour"] = "Farba", ["Orbit colours"] = "Farby dráh", ["mixed"] = "zmiešané", ["single"] = "jedna", ["Electrons per orbit"] = "Elektrónov na dráhe", ["Speed"] = "Rýchlosť" }, cs = { ["Colour"] = "Barva", ["Orbit colours"] = "Barvy drah", ["mixed"] = "smíšené", ["single"] = "jedna", ["Electrons per orbit"] = "Elektronů na dráze", ["Speed"] = "Rychlost" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "col", label = T("Colour"), type = "color", default = "#3C96FF" }, { key = "mix", label = T("Orbit colours"), type = "int", default = 0, opts = { T("mixed"), T("single") } }, { key = "n", label = T("Electrons per orbit"), type = "int", min = 1, max = 3, default = 2 }, { key = "speed", label = T("Speed"), type = "int", min = 1, max = 5, default = 3 }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local floor, ceil, sin, cos, sqrt = math.floor, math.ceil, math.sin, math.cos, math.sqrt local W, H, FPS = gfx.W, gfx.H, gfx.FPS local STEP_HZ = 30 local PI, TAU = 3.14159265, 6.2831853 local CX, CY = 31.5, 31.5 local function ell(cx, cy, r, g, b, rr, al) local top, bot = cy - rr, cy + rr for y = floor(top), ceil(bot) - 1 do local y0 = y > top and y or top local y1 = (y + 1 < bot) and (y + 1) or bot local cov = (y1 - y0) * al if cov > 0.08 then local dy = (y0 + y1) * 0.5 - cy local d2 = rr * rr - dy * dy if d2 > 0 then local sq = sqrt(d2) local xs, xe = floor(cx - sq + 0.5), floor(cx + sq + 0.5) if xe > xs then if cov > 0.97 then rect(xs, y, xe - xs, 1, r, g, b) else blend_rect(xs, y, xe - xs, 1, r, g, b, cov) end end end end end end local NP = 44 local CS, SN = {}, {} for k = 0, NP - 1 do CS[k] = cos(TAU * k / NP); SN[k] = sin(TAU * k / NP) end local NT = 5 local DL = 0.11 local TC, TS = {}, {} for k = 1, NT do TC[k] = cos(DL * k); TS[k] = sin(DL * k) end local TA = { 0.8, 0.58, 0.4, 0.26, 0.15 } local RAD = { 21, 24, 27 } local OMEGA = { 0.05, 0.042, 0.035 } local ECC, TILT = 0.72, 1.05 local E1, E2 = {}, {} for i = 1, 3 do local a = (i - 1) * PI / 3 E1[i] = { cos(a), sin(a), 0 } E2[i] = { -sin(a) * cos(TILT) * ECC, cos(a) * cos(TILT) * ECC, sin(TILT) * ECC } end local OC, EC, DC, BC = {}, {}, {}, {} local NUC = { 180, 215, 255 } local RING = { 22, 46, 108 } local BG = nil local NE, SPD = 2, 1.0 function on_settings() local c = cfg.col or { r = 60, g = 150, b = 255 } local single = (cfg.mix or 0) == 1 local cols = { { c.r, c.g, c.b }, { c.g, c.b, c.r }, { c.b, c.r, c.g } } for i = 1, 3 do local q = single and cols[1] or cols[i] OC[i] = { q[1], q[2], q[3] } EC[i] = { floor(q[1] + (255 - q[1]) * 0.6), floor(q[2] + (255 - q[2]) * 0.6), floor(q[3] + (255 - q[3]) * 0.6) } DC[i] = { floor(q[1] * 0.42), floor(q[2] * 0.42), floor(q[3] * 0.42) } BC[i] = { floor(q[1] * 0.6), floor(q[2] * 0.6), floor(q[3] * 0.6) } end NUC = { floor(c.r + (255 - c.r) * 0.55), floor(c.g + (255 - c.g) * 0.55), floor(c.b + (255 - c.b) * 0.55) } RING = { floor(c.r * 0.4), floor(c.g * 0.4), floor(c.b * 0.4) } NE = cfg.n or 2 SPD = (cfg.speed or 3) / 3 BG = nil end local function build_bg() rect(0, 0, W, H, 0, 0, 0) for k = 0, 219 do local a = TAU * k / 220 px(floor(CX + 30.2 * cos(a) + 0.5), floor(CY + 30.2 * sin(a) + 0.5), RING[1], RING[2], RING[3]) end BG = gfx.snapshot(0, 0, W, H) end local QX, QY, QT, QA, QC = {}, {}, {}, {}, {} local NQ = 0 function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local f = frame * STEP_HZ // FPS local th = f * 0.011 * SPD local ps = 0.2 + 0.35 * sin(f * 0.0073 * SPD) local ct, st, cp, sp = cos(th), sin(th), cos(ps), sin(ps) NQ = 0 for i = 1, 3 do local R = RAD[i] local e, g = E1[i], E2[i] local x1, z1 = e[1] * ct + e[3] * st, -e[1] * st + e[3] * ct local y1, zz1 = e[2] * cp - z1 * sp, e[2] * sp + z1 * cp local x2, z2 = g[1] * ct + g[3] * st, -g[1] * st + g[3] * ct local y2, zz2 = g[2] * cp - z2 * sp, g[2] * sp + z2 * cp local oc, dc = OC[i], DC[i] for k = 0, NP - 1 do local c, s = CS[k], SN[k] local x = floor(CX + R * (c * x1 + s * x2) + 0.5) local y = floor(CY + R * (c * y1 + s * y2) + 0.5) if c * zz1 + s * zz2 < 0 then px(x, y, dc[1], dc[2], dc[3]) else NQ = NQ + 1; QX[NQ], QY[NQ], QT[NQ], QC[NQ] = x, y, 1, i end end local w = OMEGA[i] * SPD for j = 0, NE - 1 do local ph = f * w + j * TAU / NE + (i - 1) * 0.9 local c0, s0 = cos(ph), sin(ph) for k = 0, NT do local c, s = c0, s0 if k > 0 then c, s = c0 * TC[k] + s0 * TS[k], s0 * TC[k] - c0 * TS[k] end local x = floor(CX + R * (c * x1 + s * x2) + 0.5) local y = floor(CY + R * (c * y1 + s * y2) + 0.5) local front = c * zz1 + s * zz2 >= 0 if k == 0 then if front then NQ = NQ + 1; QX[NQ], QY[NQ], QT[NQ], QC[NQ] = x, y, 3, i else local b = BC[i] rect(x, y, 2, 2, b[1], b[2], b[3]) end elseif front then NQ = NQ + 1; QX[NQ], QY[NQ], QT[NQ], QA[NQ], QC[NQ] = x, y, 2, TA[k], i else local b = BC[i] blend_rect(x, y, 1, 1, b[1], b[2], b[3], TA[k] * 0.6) end end end end local r = 4.2 + 0.7 * sin(f * TAU / 72) ell(CX, CY, OC[1][1], OC[1][2], OC[1][3], r + 2.6, 0.2) ell(CX, CY, OC[1][1], OC[1][2], OC[1][3], r + 1.3, 0.5) ell(CX, CY, NUC[1], NUC[2], NUC[3], r, 1) ell(CX, CY, 255, 255, 255, r * 0.5, 1) for q = 1, NQ do local t, i = QT[q], QC[q] local x, y = QX[q], QY[q] if t == 1 then local o = OC[i] px(x, y, o[1], o[2], o[3]) elseif t == 2 then local e = EC[i] blend_rect(x, y, 1, 1, e[1], e[2], e[3], QA[q]) else local e = EC[i] rect(x - 1, y, 3, 1, e[1], e[2], e[3]) rect(x, y - 1, 1, 3, e[1], e[2], e[3]) px(x, y, 255, 255, 255) end end end