Created
June 1, 2013 22:03
-
-
Save clofresh/5691856 to your computer and use it in GitHub Desktop.
Dragon curves. Wikipedia reference: https://en.wikipedia.org/wiki/Dragon_curve
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local curves = {} | |
local timer = 0 | |
local iterations = 0 | |
function newCurve(startX, startY, startDir, lineLen, color) | |
return { | |
startX = startX, | |
startY = startY, | |
startDir = startDir, | |
lineLen = lineLen, | |
color = color, | |
vals = {"F", "X"} | |
} | |
end | |
function updateCurve(curve) | |
local newCurve = {} | |
for i, r in pairs(curve.vals) do | |
if r == "X" then | |
table.insert(newCurve, "X") | |
table.insert(newCurve, "+") | |
table.insert(newCurve, "Y") | |
table.insert(newCurve, "F") | |
table.insert(newCurve, "+") | |
elseif r == "Y" then | |
table.insert(newCurve, "-") | |
table.insert(newCurve, "F") | |
table.insert(newCurve, "X") | |
table.insert(newCurve, "-") | |
table.insert(newCurve, "Y") | |
else | |
table.insert(newCurve, r) | |
end | |
end | |
curve.vals = newCurve | |
end | |
function drawCurve(curve) | |
love.graphics.setColor(curve.color) | |
local x1 = curve.startX | |
local y1 = curve.startY | |
local x2, y2 | |
local dir = curve.startDir | |
local lineLen = curve.lineLen | |
for i, r in pairs(curve.vals) do | |
if r == "F" then | |
x2 = x1 + lineLen * math.sin(dir) | |
y2 = y1 + lineLen * math.cos(dir) | |
love.graphics.line(x1, y1, x2, y2) | |
x1 = x2 | |
y1 = y2 | |
elseif r == "+" then | |
dir = dir + math.pi / 2 | |
elseif r == "-" then | |
dir = dir - math.pi / 2 | |
end | |
end | |
end | |
function love.load() | |
curves = { | |
newCurve(400, 300, 0, 4, {178, 139, 101}), | |
newCurve(400, 300, math.pi/2, 4, { 25, 255, 49}), | |
newCurve(400, 300, math.pi, 4, {255, 125, 0}), | |
newCurve(400, 300, 3*math.pi/2, 4, { 72, 20, 204}), | |
} | |
end | |
function love.update(dt) | |
timer = timer + dt | |
if timer >= 2 then | |
iterations = iterations + 1 | |
for i, curve in pairs(curves) do | |
updateCurve(curve) | |
end | |
timer = 0 | |
end | |
end | |
function love.draw() | |
for i, curve in pairs(curves) do | |
drawCurve(curve) | |
end | |
if iterations >= 16 then | |
love.event.quit() | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment