From 115767daafd2803b6cac0346d6a0cf9915d15f28 Mon Sep 17 00:00:00 2001 From: Rose Liverman Date: Mon, 21 Mar 2022 17:51:37 -0600 Subject: [PATCH] init naive attempt --- .gitignore | 1 + src/main.moon | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 .gitignore create mode 100644 src/main.moon diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d907c43 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.lua diff --git a/src/main.moon b/src/main.moon new file mode 100644 index 0000000..55aff65 --- /dev/null +++ b/src/main.moon @@ -0,0 +1,86 @@ +nodes = { + Rose: { + edges: { + Ky: { "solid" } + Luz: { "solid" } + Joel: { "solid" } + Raven: { "solid" } + } + } + -- Ky: { + -- Luz: { "encapsulate" } + -- Joel: { "encapsulate" } + -- Raven: { "encapsulate" } + -- } + Luz: { + edges: { + Joel: { "solid" } + Raven: { "dashed" } + } + } +} + +w, h = love.graphics.getDimensions! +centering_force = 0.01 +annealing = 0.9 +repelling_force = 0.25 + +for self, node in pairs nodes + for name, edge_type in pairs node.edges + unless nodes[name] + nodes[name] = { edges: {} } + nodes[name].edges[self] = edge_type + +ordered = {} +for _, node in pairs nodes + node.x = love.math.random! * w - w / 2 + node.y = love.math.random! * h - h / 2 + node.vx = 0 + node.vy = 0 + table.insert ordered, node + +repel = (a, b) -> + dx = a.x - b.x + dy = a.y - b.y + d2 = dx * dx + dy * dy + if d2 == 0 + d2 == 0.5 + + force = -repelling_force * 10000 / d2 + direction = math.atan dy, dx + dcos = math.cos direction + dsin = math.sin direction + + a.vx -= force * dcos + a.vy -= force * dsin + b.vx += force * dcos + b.vy += force * dsin + +love.update = (dt) -> + -- centering force + for _, node in pairs nodes + direction = math.atan2 node.y, node.x + node.vx -= math.abs(node.x * centering_force) * math.cos direction + node.vy -= math.abs(node.y * centering_force) * math.sin direction + + -- repelling force + for i = 1, #ordered - 1, 2 + for j = i + 1, #ordered + repel(ordered[i], ordered[j]) + + -- actually apply forces + for _, node in pairs nodes + node.x += node.vx + node.y += node.vy + node.vx *= annealing + node.vy *= annealing + +love.draw = -> + love.graphics.setColor 1, 1, 1, 1 + love.graphics.translate w / 2, h / 2 + for _, node in pairs nodes + love.graphics.circle "fill", node.x, node.y, 5 + +love.keypressed = (key) -> + if key == "escape" + love.event.quit!