summaryrefslogtreecommitdiff
path: root/mplife.cld
blob: 11392e84e2c9c2a67f8cf59e48f3fb137572d6fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
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()