From d5dcf34a13119496b1a3c7f0ce13e24c84292bac Mon Sep 17 00:00:00 2001
From: Philipp Gesang <pgesang@ix.urz.uni-heidelberg.de>
Date: Fri, 6 Aug 2010 15:14:20 +0200
Subject: simple metapost grid drawing routines (as cld)

---
 .hgignore  |   1 +
 life.lua   |   2 +
 mplife.cld | 195 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 run.lua    |   2 +-
 4 files changed, 199 insertions(+), 1 deletion(-)
 create mode 100644 mplife.cld

diff --git a/.hgignore b/.hgignore
index dbf151e..21aeae0 100644
--- a/.hgignore
+++ b/.hgignore
@@ -35,3 +35,4 @@ syntax:glob
 *.swo
 *.png
 *.citator
+*.run
diff --git a/life.lua b/life.lua
index aa444c4..6d5a2cb 100644
--- a/life.lua
+++ b/life.lua
@@ -323,4 +323,6 @@ function gol.pre_movie (frames, section)
     io.write("\n")
 end
 
+
 return gol
+
diff --git a/mplife.cld b/mplife.cld
new file mode 100644
index 0000000..11392e8
--- /dev/null
+++ b/mplife.cld
@@ -0,0 +1,195 @@
+--
+--------------------------------------------------------------------------------
+--         FILE:  mplife.cld
+--        USAGE:  ./mplife.cld 
+--  DESCRIPTION:  Metapost output for Conway's Game of Life
+--      OPTIONS:  ---
+-- REQUIREMENTS:  ---
+--         BUGS:  ---
+--        NOTES:  ---
+--       AUTHOR:  Philipp Gesang (Phg), <megas.kapaneus@gmail.com>
+--      COMPANY:  
+--      VERSION:  1.0
+--      CREATED:  06/08/10 12:28:35 CEST
+--     REVISION:  ---
+--------------------------------------------------------------------------------
+--
+
+require "run"
+
+local mplife = {}
+
+mplife.fade = true
+
+mplife.format = {
+
+    nl     = "\n",  space  = " ",
+
+    opar   = "(",   cpar   = ")",
+
+    hypen  = "-",   comma  = ",",
+    scolon = ";",   path   = "--",
+
+    filldraw = "fill",
+        draw = "draw",
+       cycle = "cycle",
+
+    p = function (x,y)
+        return string.format("(%s,%s)", x, y)
+    end,
+}
+
+function mplife.runcode (mpcode)
+    context.startMPcode()
+    context(mpcode)
+    context.stopMPcode()
+    --context.MPdrawing(mpcode) -- comes with next beta!
+end
+
+function mplife.p_square (from, filled, fade_level)
+    local f = mplife.format
+
+    --      p1          p2
+    --      from        from + d.x
+    --          +-------+
+    --          |       |
+    --          |       |
+    --          |       |
+    --          |       |
+    --          +-------+
+    --      from - d.y  from + d.x && - d.y
+    --      p4          p3
+ 
+    local d = { x = 1, y = 1, unit = "cm" }
+    local points = {
+        [1] = { x = from.x,     y = from.y      },
+        [2] = { x = from.x+d.x, y = from.y      },
+        [3] = { x = from.x+d.x, y = from.y-d.y  },
+        [4] = { x = from.x,     y = from.y-d.y  },
+    }
+
+
+    local buildsqr = function (unit)
+        local draw
+        if filled then
+            draw = f.filldraw
+        else
+            draw = f.draw
+        end
+
+        local tmp = f.nl .. draw
+        tmp = tmp .. f.p( points[1].x .. unit, points[1].y .. unit ) .. f.path -- automatic type conversion to string 
+        tmp = tmp .. f.p( points[2].x .. unit, points[2].y .. unit ) .. f.path
+        tmp = tmp .. f.p( points[3].x .. unit, points[3].y .. unit ) .. f.path
+        tmp = tmp .. f.p( points[4].x .. unit, points[4].y .. unit ) .. f.path
+        tmp = tmp .. f.cycle 
+
+        if filled and fade_level then
+            --tmp = tmp .. f.space .. "withcolor" .. f.space .. 1/fade_level .. "black"
+            tmp = tmp .. f.space .. "withcolor" .. f.space .. .1*fade_level .. "white"
+        end
+
+        tmp = tmp .. f.scolon
+        return tmp
+    end
+
+    return buildsqr (from.unit)
+end
+
+function mplife.draw_grid(grid)
+    print (grid)
+    local h = gol.helpers
+    local pat = ""
+    if type(grid) == "string" then
+        grid = h.split(grid, "\n") -- may leave an empty string as last item
+        if grid[#grid] == "" then grid[#grid] = nil end
+    end
+
+    local n = 1
+    --for n, row in ipairs(grid) do
+    while grid[n] ~= nil do
+        local row = grid[n]
+
+        local pos = 1
+        repeat
+            local cell = row:sub(pos, pos)
+            local p = { x = pos, y = 1 - n, unit = "cm" }
+
+            local fill, fade_level
+            if    cell ~= "0" then fill = true end
+
+            if fill and mplife.fade then
+                fade_level = tonumber(cell)
+            end
+                
+
+            pat = pat .. mplife.p_square( p, fill, fade_level )
+                --.. "\nlabel.bot(btex $" .. pos .. "$ etex, (" .. 
+                --pos .. "cm," .. 1 - n .. "cm));" 
+            pos = pos + 1
+            --print ("pos: " .. pos .. "," .. n .. " -->" .. row .. "<-- " .. type(row))
+        until pos > row:len()
+        pat = pat .. "\n"
+        n = n + 1
+    end
+
+    mplife.runcode(pat)
+    return true
+end
+
+--- testing section
+
+function mplife.checker(n)
+    local squares = ""
+    local cnt = 0
+    for y=1, n, 1 do
+        for x=1, n, 1 do
+            local fill
+            if cnt % 2 == 0 then fill = true end
+            local p = { x = x, y = y, unit = "cm" }
+            squares = squares .. mplife.p_square( p, fill )
+            cnt = cnt + 1
+        end
+        if n % 2 == 0 then cnt = cnt + 1 end -- needed for even column numbers
+    end
+    --print (squares)
+    return squares
+end
+
+function mplife.rand_pattern(n, digits)
+    local pat = ""
+    for y=1, n, 1 do
+        for x=1, n, 1 do
+            pat = pat .. math.random(0,digits)
+        end
+        pat = pat .. "\n"
+    end
+    return pat
+end
+
+--print(mplife.rand_pattern(10,1))
+
+        
+
+function mplife.main ()
+    mpgraphic = [[
+fill fullcircle scaled 200pt withcolor .625yellow;
+]]
+
+    context.setupcolors({state = "start"})
+    context.starttext()
+
+    --apoint = { x = 3, y = 4, unit = "cm" }
+    --bpoint = { x = 7, y = 5, unit = "cm" }
+    --mplife.runcode (mplife.p_square( apoint, true) .. mplife.p_square( bpoint, false))
+
+    --mplife.runcode (mplife.checker(20))
+
+    mplife.draw_grid(mplife.rand_pattern(10,6))
+
+    context.stoptext()
+    return 0
+end
+
+
+return mplife.main()
diff --git a/run.lua b/run.lua
index 611120e..db1c70a 100644
--- a/run.lua
+++ b/run.lua
@@ -107,5 +107,5 @@ function main ()
     return 0
 end
 
-return main()
+--return main()
 
-- 
cgit v1.2.3