Warning: this is an htmlized version!
The original is across this link,
and the conversion rules are here.
-- This file:
-- http://angg.twu.net/dednat5/newrect.lua
-- http://angg.twu.net/dednat5/newrect.lua.html
--  (find-angg        "dednat5/newrect.lua")
--
-- (find-lua51manualw3m "")
-- (find-books "__comp/__comp.el" "ierusalimschy")
-- (find-pil2page 8 "Contents")
-- (find-pil2text 8 "Contents")



-- «.V»				(to "V")
-- «.V-tests»			(to "V-tests")
-- «.BoundingBox»		(to "BoundingBox")
-- «.BoundingBox-tests»		(to "BoundingBox-tests")
-- «.AsciiPicture»		(to "AsciiPicture")
-- «.AsciiPicture-tests»	(to "AsciiPicture-tests")
-- «.copyopts»			(to "copyopts")
-- «.copyopts-tests»		(to "copyopts-tests")
-- «.makepicture»		(to "makepicture")
-- «.LPicture»			(to "LPicture")
-- «.LPicture-tests»		(to "LPicture-tests")
-- «.ZHA»			(to "ZHA")
-- «.ZHA-tests»			(to "ZHA-tests")
-- «.ZHA-tests-walls»		(to "ZHA-tests-walls")
-- «.ZHA-test-connectives»	(to "ZHA-test-connectives")
-- «.ZHA-test-generators»	(to "ZHA-test-generators")
-- «.shortoperators»		(to "shortoperators")
-- «.shortoperators-tests»	(to "shortoperators-tests")
-- «.Cuts»			(to "Cuts")
-- «.Cuts-tests»		(to "Cuts-tests")
-- «.MixedPicture»		(to "MixedPicture")
-- «.MixedPicture-tests»	(to "MixedPicture-tests")
-- «.MixedPicture-arch-tests»	(to "MixedPicture-arch-tests")
-- «.MixedPicture-zset-tests»	(to "MixedPicture-zset-tests")
-- «.MixedPicture-J-tests»	(to "MixedPicture-J-tests")
-- «.asciirectpoints»		(to "asciirectpoints")
-- «.asciirectpoints-tests»	(to "asciirectpoints-tests")

minmax = function (a, b, c) return Min(a, b), Max(b, c) end
checkrange = function(a, b, c, err)
    if a <= b and b <= c then return true end
    if err then error(format("%s", err, a, b, c)) end
  end
isint = function (n) return math.floor(n) == n end



-- «V» (to ".V")
V = Class {
  type    = "V",
  __tostring = function (v) return "("..v[1]..","..v[2]..")" end,
  __add      = function (v, w) return V{v[1]+w[1], v[2]+w[2]} end,
  __sub      = function (v, w) return V{v[1]-w[1], v[2]-w[2]} end,
  __unm      = function (v) return v*-1 end,
  __mul      = function (v, w)
      local ktimesv   = function (k, v) return V{k*v[1], k*v[2]} end
      local innerprod = function (v, w) return v[1]*w[1] + v[2]*w[2] end
      if     type(v) == "number" and type(w) == "table" then return ktimesv(v, w)
      elseif type(v) == "table" and type(w) == "number" then return ktimesv(w, v)
      elseif type(v) == "table" and type(w) == "table"  then return innerprod(v, w)
      else error("Can't multiply "..tostring(v).."*"..tostring(w))
      end
    end,
  --
  -- isdd = function (s)
  --     return type(s) == "string" and s:match"^%d%d$"
  --   end,
  -- ispxcyp = function (s)
  --     return type(s) == "string" and s:match"^%((.-),(.-)%)$"
  --   end,
  -- fromdd = function (s) return V{s:sub(1,1)+0, s:sub(2,2)+0} end,
  -- frompxcyp = function (s)
  --     local x, y = a:match("^%((.-),(.-)%)$")
  --     return V{x+0, y+0}
  --   end,
  --
  fromab = function (a, b)
      if     type(a) == "table"  then return a
      elseif type(a) == "number" then return V{a,b}
      elseif type(a) == "string" then
        local l, r = a:match("^(%d)(%d)$")
        if l then return V{l+0, r+0}:lrtoxy() end
        local x, y = a:match("^%((.-),(.-)%)$")
        if x then return V{x+0, y+0} end
        error("V() got bad string: "..a)
      end
    end,
  __call = function () print "hi"
    end,
  __index = {
    xytolr = function (v)
        local x, y = v[1], v[2]
        local l = (y - x) / 2
        local r = (y + x) / 2
        return V{l, r}
      end,
    lrtoxy = function (v)
        local l, r = v[1], v[2]
        local x = r - l
        local y = r + l
        return V{x, y}
      end,
    todd = function (v) return v[1]..v[2] end,
    to12 = function (v) return v[1], v[2] end,
    to_x_y = function (v) return v:to12() end,
    to_l_r = function (v) return v:xytolr():to12() end,
    xy = function (v) return "("..v[1]..","..v[2]..")" end,
    lr = function (v) local l, r = v:to_l_r(); return l..r end,
    torowcol = function (v, nlines, w, rectw)
        local x, y = v[1], v[2]
        if checkrange(0, y, nlines-1) and
           checkrange(0, x, rectw/w - 1)
        then return nlines-y, x*w+1
        end
      end,
    naiveprod = function (v, w)
        return V{v[1]*w[1], v[2]*w[2]}
      end,
    naivemin = function (v, w)
        return V{min(v[1], w[1]), min(v[2], w[2])}
      end,
    naivemax = function (v, w)
        return V{max(v[1], w[1]), max(v[2], w[2])}
      end,
    s  = function (v) return v+V{ 0, -1} end,
    n  = function (v) return v+V{ 0,  1} end,
    w  = function (v) return v+V{-1,  0} end,
    e  = function (v) return v+V{ 1,  0} end,
    se = function (v) return v+V{ 1, -1} end,
    sw = function (v) return v+V{-1, -1} end,
    ne = function (v) return v+V{ 1,  1} end,
    nw = function (v) return v+V{-1,  1} end,
    --
    -- to_l_r_l_r = function (P, Q)
    --     local Pl,Pr = P:to_l_r()
    --     local Ql,Qr = Q:to_l_r()
    --     return Pl, Pr, Ql, Qr
    --   end,
    --
    And = function (P, Q)
        -- local Pl, Pr, Ql, Qr = P:to_l_r_l_r(Q)
        local Pl, Pr = P:to_l_r()
        local Ql, Qr = Q:to_l_r()
        return V{min(Pl, Ql), min(Pr, Qr)}:lrtoxy()
      end,
    Or = function (P, Q)
        -- local Pl, Pr, Ql, Qr = P:to_l_r_l_r(Q)
        local Pl, Pr = P:to_l_r()
        local Ql, Qr = Q:to_l_r()
        return V{max(Pl, Ql), max(Pr, Qr)}:lrtoxy()
      end,
    --
    above = function (P, Q)
        local Pl, Pr = P:to_l_r()
        local Ql, Qr = Q:to_l_r()
        return Pl >= Ql and Pr >= Qr
      end,
    below = function (P, Q)
        local Pl, Pr = P:to_l_r()
        local Ql, Qr = Q:to_l_r()
        return Pl <= Ql and Pr <= Qr
      end,
    leftof = function (P, Q)
        local Pl, Pr = P:to_l_r()
        local Ql, Qr = Q:to_l_r()
        return Pl >= Ql and Pr <= Qr
      end,
    rightof = function (P, Q)
        local Pl, Pr = P:to_l_r()
        local Ql, Qr = Q:to_l_r()
        return Pl <= Ql and Pr >= Qr
      end,
  },
}

v = V.fromab
lr = function (l, r) return V{l, r}:lrtoxy() end
-- lr = function (l, r)
--     if V.isdd(l) then return V.fromdd(l):lrtoxy() end
--     return V{l, r}:lrtoxy()
--   end

-- «V-tests» (to ".V-tests")
--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "newrect.lua"
= V{3,4}             --> (3,4)    
= V{3,4} - V{2,1}    --> (1,3)    
= V{3,4} + V{2,1}    --> (5,5)    
= V{3,4} * V{2,1}    --> 10	     
= V{3,4} * -10	     --> (-30,-40)
= -10 * V{3,4}	     --> (-30,-40)
= V{-3,3}:xytolr()   --> (3,0)    
= V{3,3}:xytolr()    --> (0,3)    
= V{2,4}:xytolr()    --> (1,3)    
= V{0,0}:torowcol(4, 2, 6)   --> 4 1
= V{1,0}:torowcol(4, 2, 6)   --> 4 3
= V{2,0}:torowcol(4, 2, 6)   --> 4 5
= V{3,0}:torowcol(4, 2, 6)   --> (nothing)
= V{0,1}:torowcol(4, 2, 6)   --> 3 1
= V{0,2}:torowcol(4, 2, 6)   --> 3 5
= V{0,3}:torowcol(4, 2, 6)   --> 3 5
= V{0,4}:torowcol(4, 2, 6)   --> (nothing)

* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "newrect.lua"
= v{2, 3}
= v(2, 3)
= v(2, 3):to12()

* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "newrect.lua"
= lr"00"
= lr"10"
= lr"03"
= lr"03":s()
--]]



--  ____                        _ _             ____            
-- | __ )  ___  _   _ _ __   __| (_)_ __   __ _| __ )  _____  __
-- |  _ \ / _ \| | | | '_ \ / _` | | '_ \ / _` |  _ \ / _ \ \/ /
-- | |_) | (_) | |_| | | | | (_| | | | | | (_| | |_) | (_) >  < 
-- |____/ \___/ \__,_|_| |_|\__,_|_|_| |_|\__, |____/ \___/_/\_\
--                                        |___/                 
-- «BoundingBox» (to ".BoundingBox")

BoundingBox = Class {
  type    = "BoundingBox",
  new     = function () return BoundingBox {} end,
  __tostring = function (b)
      return bb.x0y0 and tostring(bb.x0y0).." to "..tostring(bb.x1y1) or "empty"
    end,
  __index = {
    addpoint = function (bb, v)
        if bb.x0y0 then bb.x0y0 = bb.x0y0:naivemin(v) else bb.x0y0 = v end
        if bb.x1y1 then bb.x1y1 = bb.x1y1:naivemax(v) else bb.x1y1 = v end
        return bb
      end,
    addbox = function (bb, v, delta0, delta1)
        bb:addpoint(v+delta0)
        return bb:addpoint(v+(delta1 or -delta0))
      end,
    x0y0x1y1 = function (bb)
        local x0, y0 = bb.x0y0:to_x_y()
        local x1, y1 = bb.x1y1:to_x_y()
        return x0, y0, x1, y1
      end,
    x0x1y0y1 = function (bb)
        local x0, y0 = bb.x0y0:to_x_y()
        local x1, y1 = bb.x1y1:to_x_y()
        return x0, x1, y0, y1
      end,
    --
    merge = function (bb1, bb2)
        if bb2.x0y0 then bb1:addpoint(bb2.x0y0) end
        if bb2.x1y1 then bb1:addpoint(bb2.x1y1) end
        return bb1
      end,
  },
}

-- «BoundingBox-tests» (to ".BoundingBox-tests")
--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "newrect.lua"
bb = BoundingBox.new()
= bb
= bb:addpoint(v(2, 4))
= bb:addbox(v(6, 7), v(.5, .5))
= bb:addbox(v(1, 2), v(.5, .5))
= bb:x0x1y0y1()

--]]



--     _             _ _ ____  _      _                  
--    / \   ___  ___(_|_)  _ \(_) ___| |_ _   _ _ __ ___ 
--   / _ \ / __|/ __| | | |_) | |/ __| __| | | | '__/ _ \
--  / ___ \\__ \ (__| | |  __/| | (__| |_| |_| | | |  __/
-- /_/   \_\___/\___|_|_|_|   |_|\___|\__|\__,_|_|  \___|
--                                                       
-- «AsciiPicture» (to ".AsciiPicture")
-- This is a minimalistic, and V-based, reimplementation of the
-- ascii side of the "Picture" class from:
-- (find-dn5 "picture.lua" "Picture")

pad = function (spaces, str)
    return ((str or "")..spaces):sub(1, #spaces)
  end

AsciiPicture = Class {
  type = "AsciiPicture",
  new  = function (s)
      return AsciiPicture {s=s or "  ", bb=BoundingBox.new()}
    end,
  __tostring = function (ap) return ap:tostring() end,
  __index = {
    get  = function (ap, v) return pad(ap.s, ap:get0(v)) end,
    get0 = function (ap, v)
        local x, y = v:to_x_y()
        return ap[y] and ap[y][x]
      end,
    put = function (ap, v, str)
        local x, y = v:to_x_y()
        ap[y] = ap[y] or {}
        ap[y][x] = str
        ap.bb:addpoint(v)
        return ap
      end,
    --
    tolines = function (ap)
        local x0, x1, y0, y1 = ap.bb:x0x1y0y1()
        local lines = {}
        for y=y1,y0,-1 do
          local line = ""
          for x=x0,x1 do line = line..ap:get(v(x, y)) end
          table.insert(lines, line)
        end
        return lines
      end,
    tostring = function (ap) return table.concat(ap:tolines(), "\n") end,
  },
}

-- «AsciiPicture-tests» (to ".AsciiPicture-tests")
--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "newrect.lua"
ap = AsciiPicture.new("  ")
ap = AsciiPicture.new("     ")
for l=0,2 do
  for r=0,3 do
    local pos=lr(l, r)
    ap:put(pos, "..")
    ap:put(pos, pos:lr())
    ap:put(pos, pos:xy())
  end
end
= ap

--]]




--                                    _       
--   ___ ___  _ __  _   _  ___  _ __ | |_ ___ 
--  / __/ _ \| '_ \| | | |/ _ \| '_ \| __/ __|
-- | (_| (_) | |_) | |_| | (_) | |_) | |_\__ \
--  \___\___/| .__/ \__, |\___/| .__/ \__|___/
--           |_|    |___/      |_|            
--
-- «copyopts» (to ".copyopts")
copyopts = function (A, B)
    if type(A) == "string" then
      if A:match(" ")
      then for _,a in ipairs(split(A)) do copyopts(a, B) end; return B
      else return copyopts(metaopts[A] or error("No metaopt[\""..A.."\"]"), B)
      end
    end
    for k,v in pairs(A) do
      if k == "meta" then copyopts(v, B) else B[k] = v end
    end
    return B
  end

metaopts = {}
metaopts["b"]   = {bhbox = 1}
metaopts["p"]   = {paren = 1}
metaopts["()"]  = {paren = 1}
metaopts["s"]    = {cellfont="\\scriptsize",       celllower="2pt"}
metaopts["ss"]   = {cellfont="\\scriptscriptsize", celllower="1.5pt"}  -- ?
metaopts["t"]    = {cellfont="\\tiny",             celllower="1.5pt"}  -- ?
metaopts["t"]    = {cellfont="\\tiny",             celllower="1.25pt"}  -- ?
metaopts["10pt"] = {scale="10pt"} 
metaopts["8pt"]  = {scale="8pt", meta="s"} 

-- «copyopts-tests» (to ".copyopts-tests")
--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "newrect.lua"
PP(copyopts("8pt", {}))
PP(copyopts({hello=1, meta="8pt"}, {}))

--]]






--                  _              _      _                  
--  _ __ ___   __ _| | _____ _ __ (_) ___| |_ _   _ _ __ ___ 
-- | '_ ` _ \ / _` | |/ / _ \ '_ \| |/ __| __| | | | '__/ _ \
-- | | | | | | (_| |   <  __/ |_) | | (__| |_| |_| | | |  __/
-- |_| |_| |_|\__,_|_|\_\___| .__/|_|\___|\__|\__,_|_|  \___|
--                          |_|                              
-- «makepicture» (to ".makepicture")
-- (find-LATEX "edrx15.sty" "picture-cells")
--
makepicture = function (options, bb, body)
    local x0, y0, x1, y1 = bb:x0y0x1y1()
    local a = {}
    a.xdimen = x1 - x0
    a.ydimen = y1 - y0
    -- a.xoffset = (x0 + x1)/2
    -- a.yoffset = (y0 + y1)/2
    a.xoffset = x0
    a.yoffset = y0
    a.setscale = "\\unitlength="..(options.scale or "10pt").."%\n"
    a.setlower = "\\celllower="..(options.celllower or "2.5pt").."%\n"
    a.setfont  = "\\def\\cellfont{"..(options.cellfont or "").."}%\n"
    a.body = body
    local fmt = "!setscale!setlower!setfont"..
                "\\begin{picture}(!xdimen,!ydimen)(!xoffset,!yoffset)\n"..
                "!body"..
                "\\end{picture}"
    local latex = (fmt:gsub("!([a-z]+)", a))
    latex = "\\vcenter{\\hbox{"..latex.."}}"
    if options.bhbox then latex = "\\bhbox{$"..latex.."$}" end
    if options.paren then latex = "\\left("..latex.."\\right)" end
    if options.def   then latex = "\\def\\"..options.def.."{"..latex.."}" end
    return latex
  end




--  _     ____  _      _                  
-- | |   |  _ \(_) ___| |_ _   _ _ __ ___ 
-- | |   | |_) | |/ __| __| | | | '__/ _ \
-- | |___|  __/| | (__| |_| |_| | | |  __/
-- |_____|_|   |_|\___|\__|\__,_|_|  \___|
--                                        
-- «LPicture» (to ".LPicture")
-- This is a minimalistic, and V-based, reimplementation of the
-- LaTeX side of the "Picture" class from:
-- (find-dn5 "picture.lua" "Picture")

LPicture = Class {
  type = "LPicture",
  new  = function (opts)
      local lp = {latex="", bb=BoundingBox.new()}    -- start empty
      -- for k,v in pairs(opts or {}) do lp[k] = v end  -- copy options
      copyopts(opts, lp)
      return LPicture(lp)                            -- set metatable
    end,
  __tostring = function (lp) return lp:tolatex() end,
  __index = {
    addpoint = function (lp, v) lp.bb:addpoint(v); return lp end,
    put = function (lp, v, tex)
        local x, y = v:to_x_y()
        lp.latex = lp.latex .. "  \\put("..x..","..y.."){\\cell{"..tex.."}}\n"
        lp:addpoint(v-V{.5,.5})
        lp:addpoint(v+V{.5,.5})
        return lp
      end,
    putarrow = function (lp, v, dx, dy, tex)
        lp:put(v+V{dx,dy}*0.5, tex)
      end,
    addlineorvector = function (lp, src, tgt, cmd)
        lp:addpoint(src)
        lp:addpoint(tgt)
        local x0, y0 = src:to_x_y()
        local x1, y1 = tgt:to_x_y()
        local dx, dy = x1-x0, y1-y0
        local adx, ady = math.abs(dx), math.abs(dy)
        local len = max(adx, ady)
        local udx, udy = dx/len, dy/len
        local put = "  \\put("..x0..","..y0..")"..
                    "{\\"..(cmd or "line").."("..udx..","..udy.."){"..len.."}}"
        lp.latex = lp.latex..put.."\n"
      end,
    tolatex = function (lp)
        return makepicture(lp, lp.bb, lp.latex)
      end,
  },
}

-- «LPicture-tests» (to ".LPicture-tests")
--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "newrect.lua"
lp = LPicture.new {cellfont="\\scriptsize"}
for l=0,2 do
  for r=0,3 do
    local pos=lr(l, r)
    lp:put(pos, pos:xy())
  end
end
= lp

--]]






--  ______   _    _    
-- |__  / | | |  / \   
--   / /| |_| | / _ \  
--  / /_|  _  |/ ___ \ 
-- /____|_| |_/_/   \_\
--                     
-- «ZHA» (to ".ZHA")

specwidths = function (spec)
    local copydigit = function (s) return s:sub(1, 1):rep(#s) end
    return (spec:gsub("[1-9][LRV]+", copydigit))
  end
specx0 = function (spec) return -(ZHA.fromspec(spec).minx) end

ZHA = Class {
  type    = "ZHA",
  fromspec = function (spec)
      return ZHA.fromspec0(spec):calcminmaxlr()
    end,
  fromspec0 = function (spec, x0)
      x0 = x0 or 0
      local widths = specwidths(spec)
      local z = ZHA {spec=spec, widths=widths,
                     maxy=#spec-1, minx=x0, maxx=x0,
                     L={[0]=x0}, R={[0]=x0}}
      local getc = function (y) return spec  :sub(y+1, y+1)   end
      local getw = function (y) return widths:sub(y+1, y+1)+0 end
      local w, LR = 1, V{x0, x0}
      local getdeltaLRw = function (y)
          local c = getc(y)
          if c == "L"       then return V{-1, -1} end           -- move left
          if c == "R"       then return V{ 1,  1} end           -- move right
          if c == (w+1).."" then w = w+1; return V{-1,  1} end  -- become wider
          if c == (w-1).."" then w = w-1; return V{ 1, -1} end  -- become thinner
          error("Bad char: "..c)
        end
      for y=1,z.maxy do
        -- PP(y, getc(y), w)
        -- LR = LR + PP(getdeltaLRw(y))
        LR = LR + getdeltaLRw(y)
        local xL, xR = LR[1], LR[2]
        z.L[y], z.R[y] = xL, xR
        z.minx = min(z.minx, xL)
        z.maxx = max(z.maxx, xR)
      end
      return z
    end,
  --
  __tostring = function (z) return z:tostring() end,
  --
  __index = {
    PP = function (z) PP(z); return z end,
    print = function (z) print(z); return z end,
    hasxy = function (z, x,y)
        local x, y = v(x, y):to12()
        local L, R = z.L[y], z.R[y]
        if not L then return false end
        if not checkrange(L, x, R) then return false end
        if not isint((x-L)/2) then return false end
        return true
      end,
    xycontents = function (z, x,y)
        local xy = v(x, y)
        if z:hasxy(xy) then return xy:xytolr():todd() end
      end,
    tolines = function (z)
        local lines = {}
        for y=z.maxy,0,-1 do
          local f = function (x) return z:xycontents(x, y) or "  " end
          table.insert(lines, mapconcat(f, seq(z.minx, z.maxx)))
        end
        return lines
      end,
    tostring = function (z) return table.concat(z:tolines(), "\n") end,
    --
    -- Compute minl[], maxl[], minr[], maxr[], lrtop
    -- (find-dn5file "zha.lua" "calclrminmax =")
    leftpoint  = function (z, y) return v(z.L[y], y) end,
    rightpoint = function (z, y) return v(z.R[y], y) end,
    setminmaxlr0 = function (z, v)
        local l, r = v:to_l_r()
        z.minl[r], z.maxl[r] = minmax(z.minl[r], l, z.maxl[r])
        z.minr[l], z.maxr[l] = minmax(z.minr[l], r, z.maxr[l])
      end,
    calcminmaxlr = function (z)
        z.minl, z.minr, z.maxl, z.maxr = {}, {}, {}, {}
        for y=0,z.maxy do
          z:setminmaxlr0(z:leftpoint(y))
          z:setminmaxlr0(z:rightpoint(y))
        end
        z.top = z:leftpoint(z.maxy)
        z.topl, z.topr = z.top:to_l_r()
        return z
      end,
    sw = function (z, v) local l,r = v:to_l_r(); return lr(l, z.minr[l]) end,
    ne = function (z, v) local l,r = v:to_l_r(); return lr(l, z.maxr[l]) end,
    se = function (z, v) local l,r = v:to_l_r(); return lr(z.minl[r], r) end,
    nw = function (z, v) local l,r = v:to_l_r(); return lr(z.maxl[r], r) end,
    --
    points = function (z)
        return cow(function ()
            for l=z.topl,0,-1 do
              for r=z.minr[l],z.maxr[l] do
                coy(lr(l, r))    -- v
              end
            end
          end)
      end,
    arrows = function (z)
        return cow(function ()
            for y=z.maxy,1,-1 do
              for x=z.L[y],z.R[y],2 do
		if z:hasxy(x-1, y-1) then coy(v(x, y), -1, -1) end  -- v,dx,dy
		if z:hasxy(x+1, y-1) then coy(v(x, y),  1, -1) end  -- v,dx,dy
              end
            end
          end)
      end,
    --
    Le  = function (z, P, Q) return P:below(Q) end,
    T   = function (z)       return z.top end,
    F   = function (z)       return V{0, 0} end,
    And = function (z, P, Q) return P:And(Q) end,
    Or  = function (z, P, Q) return P:Or(Q) end,
    Imp = function (z, P, Q)
        if     P:below(Q)   then return z.top
        elseif P:leftof(Q)  then return z:ne(P:And(Q))
        elseif P:rightof(Q) then return z:nw(P:And(Q))
        else                     return Q
        end
      end,
    Bic = function (z, P, Q) return z:And(z:Imp(P, Q), z:Imp(Q, P)) end,
    Not = function (z, P)    return z:Imp(P, z:F()) end,
    --
    -- See:
    -- (find-angg "LUA/lua50init.lua" "eval-and-L")
    getcuts = function (z, J)
        if type(J) == "string" then J = L(J) end
        local cuts = ""
        local add = function (c) cuts = cuts..c end
        local ltoP = function (l) return lr(l, z.maxr[l]) end
        local rtoP = function (r) return lr(z.maxl[r], r) end
        local isstable = function (P) return J(P):lr() == P:lr() end
        add(z.topl)
        for l=z.topl-1,0,-1 do
          if isstable(ltoP(l)) then add("/") end
          add(l)
        end
        add(" ")
        for r=0,z.topr-1 do
          add(r)
          if isstable(rtoP(r)) then add("|") end
        end
        add(z.topr)
        return cuts
      end,
    --
    rightwallgenerators = function (z, all)
        local A = {}
        for r=1,z.topr do
          if all or z.minl[r-1] < z.minl[r] then
            table.insert(A, lr(z.minl[r], r))
          end
        end
        return A
      end,
    leftwallgenerators = function (z, all)
        local A = {}
        for l=1,z.topl do
          if all or z.minr[l-1] < z.minr[l] then
            table.insert(A, lr(l, z.minr[l]))
          end
        end
        return A
      end,
    rightwallcorners = function (z)
        local l, r = 0, 0
        local A = {}
        -- local dbg = function (str) PP(str, l, r) end
        local push = function () table.insert(A, lr(l, r)) end
        local nextoutercorner = function () r = z.maxr[l]; push() end
        local nextinnercorner = function ()
            if r < z.topr then l = z.minl[r+1]; push(); return true end
          end
        push()
        nextoutercorner()
        while nextinnercorner() do nextoutercorner() end
        l, r = z.topl, z.topr
        push()
        return A
      end,
    leftwallcorners = function (z)
        local l, r = 0, 0
        local A = {}
        -- local dbg = function (str) PP(str, l, r) end
        local push = function () table.insert(A, lr(l, r)) end
        local nextoutercorner = function () l = z.maxl[r]; push() end
        local nextinnercorner = function ()
            if l < z.topl then r = z.minr[l+1]; push(); return true end
          end
        push()
        nextoutercorner()
        while nextinnercorner() do nextoutercorner() end
        l, r = z.topl, z.topr
        push()
        return A
      end,
  },
}

-- «ZHA-tests» (to ".ZHA-tests")
--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
-- (find-dn5 "zha.lua" "ZHA")
dofile "newrect.lua"

ZHA.fromspec("12L1RRR2RL1"):PP()
ZHA.fromspec("12L1RRR2RL1", 100):PP()
ZHA.fromspec("123LLR432L1"):PP()
ZHA.fromspec("123RRL432R1"):PP()

* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
-- (find-dn5 "zha.lua" "ZHA")
dofile "newrect.lua"
z = ZHA.fromspec("123RRL432R1"):PP()
= z

for y=z.maxy-1,0,-1 do
  for x=z.minx,z.maxx do
    printf(z:xycontents(x, y) or "  ")
  end
  print()
end
= z
PP(z)

PP(z:tolines())
PP(z:tostring())


= z:hasxy(0,0)
= z
= z.xytolr



-- «ZHA-tests-walls» (to ".ZHA-tests-walls")
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
-- (find-dn5 "zha.lua" "ZHA")
dofile "newrect.lua"
z = ZHA.fromspec("12RRL1LLRR")
= z
for y=z.maxy,0,-1 do print(z:leftpoint(y):lr()) end
for y=z.maxy,0,-1 do print(z:rightpoint(y):lr()) end
for _,P in ipairs(z:rightwallgenerators("all")) do print(P:lr()) end
for _,P in ipairs(z:rightwallgenerators()) do print(P:lr()) end
for _,P in ipairs(z:leftwallgenerators("all")) do print(P:lr()) end
for _,P in ipairs(z:leftwallgenerators()) do print(P:lr()) end
for _,P in ipairs(z:rightwallcorners()) do print(P:lr()) end
for _,P in ipairs(z:leftwallcorners()) do print(P:lr()) end

--      45
--    44        
--  43
--    33
--      23
--    22  13
--      12  03
--    11  02
--  10  01
--    00

-- «ZHA-test-connectives» (to ".ZHA-test-connectives")
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
-- (find-dn5 "zha.lua" "ZHA")
dofile "newrect.lua"
z = ZHA.fromspec("12RRL1LLRR")
= z:Imp(v"11", v"02"):lr() --> 03
= z:Imp(v"03", v"12"):lr() --> 22
= z:Imp(v"23", v"12"):lr() --> 12
= z:Imp(v"12", v"23"):lr() --> 45
= z:ne(v"10"):lr()
= z:nw(v"02"):lr()
= v"12":And(v"03"):lr()

= v"11":And(v"02"):lr()
= z:ne(v"11":And(v"02")):lr()
= v("11"):leftof(v"02")
= v("11"):above(v"02")
= v("11"):below(v"02")
= v("23"):below(v"12")
= v("23"):above(v"12")

-- «ZHA-test-generators» (to ".ZHA-test-generators")
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "newrect.lua"
z = ZHA.fromspec("123RRL432R1")
for v in z:points() do printf("%s ", v:lr()) end
ap = AsciiPicture.new("  ")
for v in z:points() do ap:put(v, v:lr()) end
= ap
ap = AsciiPicture.new("  ")
for P in z:points() do ap:put(P, P:And(v"23"):lr()) end
= ap

z = ZHA.fromspec("121L")
= z
for P,dx,dy in z:arrows() do print(P:lr().."->"..(P+v(dx,dy)):lr()) end

--]]



--      _                _                             _                 
--  ___| |__   ___  _ __| |_ ___  _ __   ___ _ __ __ _| |_ ___  _ __ ___ 
-- / __| '_ \ / _ \| '__| __/ _ \| '_ \ / _ \ '__/ _` | __/ _ \| '__/ __|
-- \__ \ | | | (_) | |  | || (_) | |_) |  __/ | | (_| | || (_) | |  \__ \
-- |___/_| |_|\___/|_|   \__\___/| .__/ \___|_|  \__,_|\__\___/|_|  |___/
--                               |_|                                     
-- «shortoperators» (to ".shortoperators")
function shortoperators ()
    True  = function () return z:T() end
    False = function () return z:F() end
    And   = function (P, Q) return P:And(Q) end
    Or    = function (P, Q) return P:Or(Q) end
    Imp   = function (P, Q) return z:Imp(P, Q) end
    Not   = function (P) return Imp(P, False()) end
    Bic   = function (P, Q) return z:Bic(P, Q) end
    --
    Cloq  = function (A)    return function (P) return Or(A, P) end end
    Opnq  = function (A)    return function (P) return Imp(A, P) end end
    Booq  = function (A)    return function (P) return Imp(Imp(P, A), A) end end
    Forq  = function (A, B) return function (P) return And(Or(A, P), Imp(B, P)) end end
    Jand  = function (J, K) return function (P) return And(J(P), K(P)) end end
  end

-- «shortoperators-tests» (to ".shortoperators-tests")
--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "newrect.lua"
shortoperators()
= mpnewJ({}, "1234567654321", Opnq(v"23")):zhaPs("23")
= mpnewJ({}, "1234567654321", Cloq(v"23")):zhaPs("23")
= mpnewJ({}, "1234567654321", Booq(v"23")):zhaPs("23")
= mpnewJ({}, "1234567654321", Forq(v"42", v"24")):zhaPs("42 24")


--]]





--   ____      _       
--  / ___|   _| |_ ___ 
-- | |  | | | | __/ __|
-- | |__| |_| | |_\__ \
--  \____\__,_|\__|___/
--                     
-- «Cuts» (to ".Cuts")

Cuts = Class {
  type    = "Cuts",
  new = function ()
      return Cuts {minicuts={}, asciibb=BoundingBox.new(),
                   latex="",    latexbb=BoundingBox.new()}
    end,
  __tostring = function (c) return c:tostring() end,
  __index = {
    get = function (c, v)
        return (c.minicuts[v[2]] or {})[v[1]] or " "
      end,
    tolines = function (c)
        local x0, x1, y0, y1 = c.asciibb:x0x1y0y1()
        local lines = {}
        for y=y1,y0,-1 do
          local s = ""
          for x=x0,x1 do
            local char = (c.minicuts[y] or {})[x] or " "
            s = s..char
          end
          table.insert(lines, s)
        end
        return lines
      end,
    tostring = function (c) return table.concat(c:tolines(), "\n") end,
    print = function (c) print(c); return c end,
    --
    -- Low-level functions for adding cuts
    addminicut = function (c, x, y, slash)
        c.minicuts[y] = c.minicuts[y] or {}
        c.minicuts[y][x] = slash
        c.asciibb:addpoint(v(x, y))
      end,
    addcut0 = function (c, src, tgt)
        c.latexbb:addpoint(src)
        c.latexbb:addpoint(tgt)
        --
        local x0, y0 = src:to_x_y()
        local x1, y1 = tgt:to_x_y()
        if y1 < y0 then x0, y0, x1, y1 = x1, y1, x0, y0 end
        local dx, dy = x1-x0, y1-y0
        if     dy == dx then  -- northeast, with "/"s
          for a=0.5,dy-0.5 do
            c:addminicut(x0+a, y0+a, "/")
          end
        elseif dy == -dx then -- northwest, with "\"s
          for a=0.5,dy-0.5 do
            c:addminicut(x0-a, y0+a, "\\")
          end
        else PP("dx=", dx, "dy=", dy); error()
        end
        c:addlatexcut(src, tgt) -- defined below
        return c
      end,
    addcuts0 = function (c, list)
        for i=1,#list-1 do c:addcut0(list[i], list[i+1]) end
        return c
      end,
    --
    -- Medium-level functions for adding cuts
    addcontour = function (c, zha)
        local leftwall  = zha:leftwallcorners()
        local rightwall = zha:rightwallcorners()
        local corners = {}
        table.insert(corners, leftwall[1]:s())
        for i=2,#leftwall-1 do table.insert(corners, leftwall[i]:w()) end
        table.insert(corners, leftwall[#leftwall]:n())
        for i=#rightwall-1,2,-1 do table.insert(corners, rightwall[i]:e()) end
        table.insert(corners, rightwall[1]:s())
        c:addcuts0(corners) 
        return c
      end,
    addlcut = function (c, zha, l)
        local westcell = lr(l+1, zha.minr[l+1])
        local eastcell = lr(l,   zha.maxr[l])
        c:addcut0(westcell:s(), eastcell:n())
        return c
      end,
    addrcut = function (c, zha, r)
        local eastcell = lr(zha.minl[r+1], r+1)
        local westcell = lr(zha.maxl[r],   r)
        c:addcut0(westcell:n(), eastcell:s())
        return c
      end,
    --
    -- A high-level function for adding (all kinds of) cuts.
    addcuts = function (c, zha, str)
        if str:match"c" then c:addcontour(zha) end
        for l in str:gmatch"/(%d)" do c:addlcut(zha, l+0) end 
        for r in str:gmatch"(%d)|" do c:addrcut(zha, r+0) end 
        --
        local f = function (lr, dir) local P = v(lr); return P[dir](P) end
        local pat = "(%d%d)([ensw])-(%d%d)([ensw])"
        for src,sdir,tgt,tdir in str:gmatch(pat) do
          c:addcut0(f(src, sdir), f(tgt, tdir))
        end
        return c
      end,
    --
    -- Communication with Picture objects
    -- Similar to: (find-dn5 "newrect.lua" "LPicture" "addlineorvector =")
    addlatexcut = function (c, src, tgt)
        local x0, y0 = src:to_x_y()
        local x1, y1 = tgt:to_x_y()
        local dx, dy = x1-x0, y1-y0
        local len = math.abs(dx)
        local udx, udy = dx/len, dy/len
        local put = "  \\put("..x0..","..y0..")"..
                    "{\\line("..udx..","..udy.."){"..len.."}}"
        c.latex = c.latex..put.."\n"
      end,
    -- copylatexcuts = function (c, pic)
    --   end,
  },
}


--[[
-- «Cuts-tests» (to ".Cuts-tests")
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "newrect.lua"
c = Cuts.new()
z = ZHA.fromspec("12RRL1LLRR")
= z
= c:addcuts0{v"00":w(), v"01":n(), v"01":e()}
PP(c)
= c:addcontour(z)
= c.latex

* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "newrect.lua"
c = Cuts.new()
z = ZHA.fromspec("12RRL1LLRR")
= c:addcontour(z)
= c:addlcut(z, 0)
= c:addlcut(z, 1)
= c:addlcut(z, 2)

= c:addrcut(z, 0)
= c:addrcut(z, 1)
= c:addrcut(z, 2)

* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "newrect.lua"
c = Cuts.new()
z = ZHA.fromspec("12RRL1LLRR"):print()
= c:addcuts(z, "c 432/1/0 0|1|2|345")

z = ZHA.fromspec("1234567654321"):print()
c = Cuts.new()
= c:addcuts(z, "c 01e-41n 40w-44n 14n-64n 11n-16n")

--]]





--  __  __ _              _ ____  _      _                  
-- |  \/  (_)_  _____  __| |  _ \(_) ___| |_ _   _ _ __ ___ 
-- | |\/| | \ \/ / _ \/ _` | |_) | |/ __| __| | | | '__/ _ \
-- | |  | | |>  <  __/ (_| |  __/| | (__| |_| |_| | | |  __/
-- |_|  |_|_/_/\_\___|\__,_|_|   |_|\___|\__|\__,_|_|  \___|
--                                                          
-- «MixedPicture» (to ".MixedPicture")
-- A MixedPicture object has both and ascii representation and a LaTeX
-- representation...

strtotex = function (str)  -- Used by MixedPicture:put(...)
    if str:match"^%.+$" then return "\\cdot" end
    if str:match"^%*+$" then return "\\bullet" end
  end

MixedPicture = Class {
  type    = "MixedPicture",
  new     = function (options, zha, J)
      return MixedPicture {
        ap   = AsciiPicture.new(options.s or "  "),
        lp   = LPicture.new(options),
        cuts = Cuts.new(),
        zha  = zha,
        J    = J,
      }
    end,
  __tostring = function (mp) return mp:tostring() end,
  __index = {
    tolines_mixed = function (mp)
        local  x0,  x1,  y0,  y1 = mp.ap       .bb:x0x1y0y1()
        local cx0, cx1, cy0, cy1 = mp.cuts.asciibb:x0x1y0y1()
        local lines = {}
        for y=max(y1, cy1),min(y0, cy0),-.5 do
          local line = ""
          for x=min(x0, cx0),max(x1, cx1),.5 do
            if isint(x)
            then line = line..mp.ap:get(v(x, y))
            else line = line..mp.cuts:get(v(x, y))
            end
          end
          table.insert(lines, line)
        end
        return lines
      end,
    tolines = function (mp)
        local hascuts  = mp.cuts.asciibb.x0y0
        local hasnodes = mp.ap.bb.x0y0
        if hasnodes and hascuts then return mp:tolines_mixed() end
        if hasnodes then return mp.ap:tolines() end
        if hascuts  then return mp.cuts:tolines() end
        return {"empty"}
      end,
    tostring = function (mp) return table.concat(mp:tolines(), "\n") end,
    print  = function (mp) print(mp); return mp end,
    lprint = function (mp) print(mp:tolatex()); return mp end,
    output = function (mp) output(mp:tolatex()); return mp end,
    --
    addcontour = function (mp) mp.cuts:addcontour(mp.zha); return mp end,
    addlcut = function (mp, ...) mp.cuts:addlcut(mp.zha, ...); return mp end,
    addrcut = function (mp, ...) mp.cuts:addrcut(mp.zha, ...); return mp end,
    addcuts = function (mp, ...) mp.cuts:addcuts(mp.zha, ...); return mp end,
    --
    points = function (mp, ...) return mp.zha:points() end,
    --
    put = function (mp, v, str, tex)
        mp.ap:put(v, str)
        mp.lp:put(v, tex or strtotex(str) or str)
        return mp
      end,
    --
    zfunction = function (mp, asciirect)
        for x,y,c in asciirectpoints(asciirect) do
          if c:match"%d" then
            mp:put(v(x, y), "#"..c)
            mp.lp.def = mp.lp.def.."#"..c
          end
        end
        return mp
      end,
    zsetbullets = function (mp, asciirect)
        for x,y,c in asciirectpoints(asciirect) do
          if c:match"%d" then mp:put(v(x, y), "**") end
        end
        return mp
      end,
    zhabullets = function (mp)
        for v in mp.zha:points() do mp:put(v, "**") end
        return mp
      end,
    zhadots = function (mp)
        for v in mp.zha:points() do mp:put(v, "..") end
        return mp
      end,
    zhalr = function (mp)
        for v in mp.zha:points() do mp:put(v, v:lr()) end
        return mp
      end,
    zhaPs = function (mp, str)
        for _,w in ipairs(split(str)) do mp:put(v(w), w) end
        return mp
      end,
    --
    putpiledef = function (mp, v)
        local L, R = mp.zha.topl, mp.zha.topr
        local l, r = v:to_l_r()
        local pile = function (h, n) return ("0"):rep(h-n)..("1"):rep(n) end
        local piledef = function (A, B, a, b)
            return "\\foo{"..a..b.."}{"..pile(A, a).." "..pile(B, b).."}"
          end
        mp:put(v, v:lr(), piledef(L, R, l, r))
        return mp
      end,
    zhapiledefs = function (mp)
        for v in mp.zha:points() do mp:putpiledef(v) end
        return mp
      end,
    --
    -- (find-angg "LUA/lua50init.lua" "eval-and-L")
    setz = function (mp) z = mp.zha; return mp end,
    zhalrf = function (mp, f)
        if type(f) == "string" then f = L(f) end
        for v in mp.zha:points() do mp:put(v, f(v):lr()) end
        return mp
      end,
    zhaJ     = function (mp) return mp:zhalrf(mp.J) end,
    zhaJcuts = function (mp) return mp:addcuts(mp.zha:getcuts(mp.J)) end,
    --
    tolatex = function (mp)
        local body = mp.cuts.latex .. mp.lp.latex
        local bb = BoundingBox.new():merge(mp.lp.bb):merge(mp.cuts.latexbb)
        return makepicture(mp.lp, bb, body)
      end,
  },
}

mpnew = function (opts, spec, J)
    local z  = ZHA.fromspec(spec)
    local mp = MixedPicture.new(opts, z, J)
    return mp, z
  end

mpnewJ = function (opts, spec, J)
    return mpnew({}, spec, J):setz():zhaJcuts():addcontour()
  end

-- «MixedPicture-tests» (to ".MixedPicture-tests")
--[[
latex = mp:tolatex()
latex = "\\def\\foo{%\n"..latex.."}"
latex = "\\def\\foo#1{\\hbox#1%\n".. mp:tolatex() .."}"
= latex
writefile("/tmp/o.tex", latex)  -- (find-fline "/tmp/o.tex")


* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "newrect.lua"
z = ZHA.fromspec("12RRL1LLRR")
= z
mp = MixedPicture.new({}, z)
mp = MixedPicture.new({bhbox=1, paren=1, scale="10pt", def="foo"}, z)
= mp:put(v"02", "02")
= mp:tolatex()
for v in z:points() do mp:put(v, v:lr()) end
= mp:addcontour()
= mp:addlcut(0):addrcut(2)
latex = mp:tolatex()
= latex
-- latex = "\\def\\foo{"..latex.."}"
-- = latex
writefile("/tmp/o.tex", latex)  -- (find-fline "/tmp/o.tex")
-- (find-ist "-handouts.tex" "mixedpicture-tests")

* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "newrect.lua"
z = ZHA.fromspec("1234R3L21L")
= z
mp = MixedPicture.new({bhbox=1, paren=1, scale="10pt", def="foo"}, z)
mp = MixedPicture.new({def="foo"}, z)
  for v in z:points() do mp:put(v, v:lr()) end
= mp:addcontour()
= mp:addrcut(0):addrcut(2)
= mp:addlcut(1)
= mp:put(v"20", "P")
= mp:put(v"30", "P*", "P^*")
= mp:put(v"11", "Q")
= mp:put(v"12", "Q*", "Q^*")
= mp:put(v"03", "R")
= mp:put(v"14", "R*", "R^*")
latex = mp:tolatex()
-- latex = "\\def\\foo{"..latex.."}"
= latex
writefile("/tmp/o.tex", latex)  -- (find-fline "/tmp/o.tex")
-- (find-ist "-handouts.tex" "mixedpicture-tests")


* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "newrect.lua"
z = ZHA.fromspec("121L")
= z
mp = MixedPicture.new({bhbox=1, paren=1, scale="20pt"}, z)
mp = MixedPicture.new({bhbox=1, scale="14pt", cellfont="\\scriptsize"}, z)
-- for v in z:points() do mp:put(v, v:lr()) end
for v in z:points() do mp:put(v, v:xy()) end
= mp
for v,dx,dy in mp.zha:arrows() do print(v, dx, dy) end
for v,dx,dy in mp.zha:arrows() do
  local tex = (dx==-1) and "\\swarrow" or "\\searrow"
  mp.lp:putarrow(v, dx, dy, tex)
end
= mp.lp
latex = mp:tolatex()
latex = "\\def\\foo{"..latex.."}"
= latex
writefile("/tmp/o.tex", latex)  -- (find-fline "/tmp/o.tex")
-- (find-ist "-handouts.tex" "mixedpicture-tests")


-- «MixedPicture-arch-tests» (to ".MixedPicture-arch-tests")
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "newrect.lua"

-- The (v*) cube
mp = mpnew({def="orStarCubeArchetypal"}, "12321L"):addcuts("c 21/0 0|12")
mp:put(v"10", "P"):put(v"20", "P*", "P^*")
mp:put(v"01", "Q"):put(v"02", "Q*", "Q^*"):print()

-- The (&*) cube
mp = mpnew({def="andStarCubeArchetypal"}, "12321"):addcuts("c 2/10 01|2")
mp:put(v"20", "P"):put(v"21", "P*", "P^*")
mp:put(v"02", "Q"):put(v"12", "Q*", "Q^*"):print()

-- The (->*) cube
mp = mpnew({def="impStarCubeArchetypal"}, "12321"):addcuts("c 2/10 01|2")
mp:put(v"10", "P")
mp:put(v"00", "Q"):print()

-- (&R) is *-functorial
mp = mpnew({def="andRIsStarFunctorial"}, "1234R321"):addcuts("c 32/10 01|23|4")
mp:put(v"30", "P"):put(v"31", "P*", "P^*")
mp:put(v"22", "Q"):put(v"33", "Q*", "Q^*")
mp:put(v"04", "R"):put(v"14", "R*", "R^*"):print()

-- (Pv) is *-functorial
mp = mpnew({def="PvIsStarFunctorial"}, "1234R3L21L"):addcuts("c 5432/10 0|12|34")
mp:put(v"20", "P"):put(v"30", "P*", "P^*")
mp:put(v"11", "Q"):put(v"12", "Q*", "Q^*")
mp:put(v"03", "R"):put(v"14", "R*", "R^*"):print()

-- «MixedPicture-zset-tests» (to ".MixedPicture-zset-tests")
-- (find-dn5file "newrect.lua" "zfunction =")
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "newrect.lua"
z = ZHA.fromspec("12321L"):print()
house = ".1.|2.3|4.5"
mp = MixedPicture.new({def="dagHouse"}):zfunction(house):print():lprint()
mp = MixedPicture.new({def="dagHouse", meta="t", scale="5pt"}, z):zfunction(house):lprint()
mp = MixedPicture.new({def="House"}):zsetbullets(house):print():lprint()
mp = MixedPicture.new({def="Ten"}, z):zhabullets():print()
mp = MixedPicture.new({def="Ten"}, z):zhadots():print()
mp = MixedPicture.new({def="Ten"}, z):zhalr():print()

mp = mpnew({scale="15pt"}, "121L"):zhapiledefs():print():lprint()

* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "newrect.lua"
threefour = "..1|2.3|4.5|6.7"
MixedPicture.new({def="dagThreeFour", meta="s"}):zfunction(threefour):print():lprint()


-- «MixedPicture-J-tests» (to ".MixedPicture-J-tests")
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "newrect.lua"
= mpnewJ({}, "1234RR321", "P -> z:Or(P, v'12')")
= mpnewJ({}, "1234RR321", "P -> z:Imp(v'12', P)")
= mpnewJ("", "1234RR321", "P -> z:Imp(v'12', P)")

* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "newrect.lua"


--]]



--                 _ _               _               _       _       
--   __ _ ___  ___(_|_)_ __ ___  ___| |_ _ __   ___ (_)_ __ | |_ ___ 
--  / _` / __|/ __| | | '__/ _ \/ __| __| '_ \ / _ \| | '_ \| __/ __|
-- | (_| \__ \ (__| | | | |  __/ (__| |_| |_) | (_) | | | | | |_\__ \
--  \__,_|___/\___|_|_|_|  \___|\___|\__| .__/ \___/|_|_| |_|\__|___/
--                                      |_|                          
--
-- Asciirects are a good way to specify ZSets and ZFunctions - for
-- example, ".1.|2.3|4.5" is (reading order on) the "House" ZSet.
--
-- «asciirectpoints» (to ".asciirectpoints")
asciirectpoints = function (lines)
    return cow(function ()
        if type(lines) == "string" then
          lines = splitlines((lines:gsub("|", "\n")))
        end
        for y=#lines-1,0,-1 do
          local line = lines[#lines-y]
          for x=0,#line-1 do
            local c = line:sub(x+1, x+1)
            coy(x, y, c)
          end
        end
      end)
  end

-- «asciirectpoints-tests» (to ".asciirectpoints-tests")
--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "newrect.lua"
str = ".1.|2.3|4.5"
for x,y,c in asciirectpoints(str) do print(x, y, c) end
for x,y,c in asciirectpoints(str) do
  if c:match"%d" then
    local p = "#"..c
    print(v(x, y), c)
  end
end

opts = {def="dagHouse", scale="4pt", meta="p b s"}
mp = MixedPicture.new(opts)
PP(mp.lp)
for x,y,c in asciirectpoints(str) do
  if c:match"%d" then
    mp:put(v(x, y), "#"..c)
    mp.lp.def = mp.lp.def.."#"..c
  end
end
= mp
= mp:tolatex()

opts = {def="PvSTbullets", meta="p b s", scale="4pt"}
z = ZHA.fromspec("1234R3L21L"):print()
mp = MixedPicture.new(opts, z)
for v in z:points() do mp:put(v, "**") end
= mp
= mp:tolatex()

-- (find-istfile "1.org" "* 2-column graphs")
-- (find-istfile "1.org" "* 2-column graphs" "how to convert between proper")
z = ZHA.fromspec("12RR1234321L"):print()

--      56	             56	       
--        46                   ..       
--      45  36               ..  ..     
--    44  35  26           ..  ..  ..   
--  43  34  25  16       43  ..  ..  16 
--    33  24  15           33  ..  15   
--      23  14               23  14     
--        13                   ..       
--      12  03               ..  03     
--    11  02               ..  02       
--  10  01	         10  01	       
--    00                   ..           

-- (find-istfile "1.org" "* ZQuotients")
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "newrect.lua"
z = ZHA.fromspec("12345RR4321"):print()
z = ZHA.fromspec("1234543RR21"):print()
z = ZHA.fromspec("1234543RR21"):print()
z = ZHA.fromspec("1R2R3212RL1"):print()
mp = MixedPicture.new({def="foo"}, z):zhalr()
mp:addcuts("c 4321/0 0123|45|6"):print()

--]]




--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "newrect.lua"
opts = {def="graphid", scale="6pt", meta="p b s"}
mp = MixedPicture.new(opts)
spec = "012345" 
for y=0,5 do mp:put(v(-1, y), y.."") end
for x=0,5 do mp:put(v(x, -1), x.."") end
for a=0,5 do local aP = spec:sub(a+1, a+1)+0; mp:put(v(a, aP), "*") end
mp.lp:addlineorvector(v(0, 0), v(6, 0), "vector")
mp.lp:addlineorvector(v(0, 0), v(0, 6), "vector")
mp:put(v(7, 0), "a")
mp:put(v(0, 7), "aP", "a^P")
= mp.lp
= mp
= mp.lp
= mp

* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "newrect.lua"
z = ZHA.fromspec("1R2R3212RL1")
mp = MixedPicture.new({def="ZQ", scale="1pt", meta="b ss"}, z)
mp:zhadots()
mp:lprint()

-- (find-dn5 "newrect.lua" "LPicture" "addlineorvector =")
-- (find-dn5 "newrect.lua" "LPicture")
-- (find-dn5 "newrect.lua" "MixedPicture")
--]]

--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "newrect.lua"

--]]







-- Local Variables:
-- coding: raw-text-unix
-- End: