Warning: this is an htmlized version!
The original is across this link,
and the conversion rules are here.
-- This file:
-- http://angg.twu.net/LATEX/edrxpict.lua
-- http://angg.twu.net/LATEX/edrxpict.lua.html
--  (find-angg        "LATEX/edrxpict.lua")
--
-- Complements: (find-angg "LATEX/edrxtikz.lua")

-- «.Points»		(to "Points")
-- «.pictp0-pictp3»	(to "pictp0-pictp3")
-- «.pictbounds»	(to "pictbounds")
-- «.beginpicture»	(to "beginpicture")
-- «.pictaxes»		(to "pictaxes")
-- «.pictgrid»		(to "pictgrid")
-- «.pictpgrid»		(to "pictpgrid")
-- «.pictdots»		(to "pictdots")
-- «.Piecewise»		(to "Piecewise")
-- «.pictFxy»		(to "pictFxy")
-- «.pict2evector»	(to "pict2evector")
-- «.TCG»		(to "TCG")
-- «.TCG-tests»		(to "TCG-tests")

-- loaddednat6()  -- (find-angg "LUA/lua50init.lua" "loaddednat6")
require "edrxtikz"  -- (find-LATEX "edrxtikz.lua")


-- «Points» (to ".Points")
Points = Class {
  type  = "Points",
  fromfx = function (a, b, n, f)
      local ps = Points {}
      for i=0,n do
        local x = a + (b-a)*(i/n)
        table.insert(ps, v(x,f(x)))
      end
      return ps
    end,
  fromFt = function (a, b, n, F)
      local ps = Points {}
      for i=0,n do
        local t = a + (b-a)*(i/n)
        table.insert(ps, v(F(t)))
      end
      return ps
    end,
  __tostring = function (li) return li:tostring(" ") end,
  __index = {
    tostring = function (ps, sep) return mapconcat(tostring, ps, sep or "") end,
    add = function (ps, ...)
        for _,p in ipairs({...}) do table.insert(ps, p) end
        return ps
      end,
    copy = function (ps) return Points(shallowcopy(ps)) end,
    polyunder = function (ps)
        local x0, x1 = ps[1][1], ps[#ps][1]
        return ps:copy():add(v(x1,0), v(x0,0))
      end,
    pict = function (ps, pre) return (pre or "")..ps:tostring() end,
    --
    line = function (ps) return ps:pict"\\polyline" end,
    poly = function (ps) return ps:pict"\\polygon" end,
    fill = function (ps) return ps:pict"\\polygon*" end,
  },
}


--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "edrxpict.lua"
ps = Points.fromfx(2, 3, 5, function (x) return 10*x end)
= ps
= ps:polyunder()
= ps:polyunder():pict"\\polyline"
= ps:polyunder():pict"\\polygon"
= ps:polyunder():pict"\\polygon*"
= Points{v(1,2), v(3,4)}:polyunder():pict"\\polygon*"
= ps:line()
= ps:poly()
= ps:fill()
-- (find-pict2epage 9 "2.4     Extensions")

--]]



-- «pictdots» (to ".pictdots")
-- (find-LATEX "2016-2-GA-algebra.tex" "picturedots")
pictdots = function (str)
    local bprint, out = makebprint()
    for x,y in str:gmatch("([-.%d]+),([-.%d]+)") do
      local p = v(x+0, y+0)
      bprint("\\put%s{\\circle*{0.5}}", p)
    end
    return out()
  end

--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "edrxpict.lua"
= pictdots "3,1  3,2  3,3"

--]]



-- «pictp0-pictp3» (to ".pictp0-pictp3")
-- global variables, sample values
pictp1 = v(-1,-2)           -- lower left,  integer coordinates
pictp2 = v( 3, 5)           -- upper right, integer coordinates
pictp0 = pictp1 - v(.2,.2)  -- lower left
pictp3 = pictp2 + v(.2,.2)  -- upper right

-- «pictbounds» (to ".pictbounds")
pictbounds = function (p1, p2, e)
    e = e or .2
    pictp1 = p1
    pictp2 = p2
    pictp0 = pictp1 - v(e,e)
    pictp3 = pictp2 + v(e,e)
  end

-- «beginpicture» (to ".beginpicture")
beginpicture0 = function ()
    local size   = pictp3 - pictp0
    local origin = pictp0
    return formatt("\\begin{picture}%s%s", size, origin)
  end
beginpicture = function (p1, p2, e)
    pictbounds(p1, p2, e)
    return beginpicture0()
  end

-- «pictaxes» (to ".pictaxes")
pictaxes = function (p1, p2)
    p1, p2 = p1 or pictp1, p2 or pictp2
    local xmin, xmax = min(p1[1], p2[1]), max(p1[1], p2[1])
    local ymin, ymax = min(p1[2], p2[2]), max(p1[2], p2[2])
    local bprint, out = makebprint()
    bprint("%% Horizontal axis:")
    bprint("\\Line%s%s", v(xmin-.2,0), v(xmax+.2,0))
    bprint("%% Horizontal axis, ticks:")
    for x=xmin,xmax do
      bprint("\\Line%s%s", v(x,-.2), v(x,.2))
    end
    bprint("%%")
    bprint("%% Vertical axis:")
    bprint("\\Line%s%s", v(0,ymin-.2), v(0,ymax+.2))
    bprint("%% Vertical axis, ticks:")
    for y=ymin,ymax do
      bprint("\\Line%s%s", v(-.2,y), v(.2,y))
    end
    bprint("%%")
    return out()
  end

--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "edrxpict.lua"
= beginpicture(v(-1,-2), v(3,5))
= pictaxes()
= pictaxes(v(-2,-1), v(3,4))

-- \expr{pictsizepos(v(-1.2,-2.2), v(3.2,5.2))}
-- \expr{pictaxes   (v(-1,  -2),   v(3,  5))}

--]]



-- «pictgrid» (to ".pictgrid")
pictgrid = function (p1, p2)
    p1, p2 = p1 or pictp1, p2 or pictp2
    local xmin, xmax = min(p1[1], p2[1]), max(p1[1], p2[1])
    local ymin, ymax = min(p1[2], p2[2]), max(p1[2], p2[2])
    local bprint, out = makebprint()
    bprint("%% Grid")
    bprint("%% Horizontal lines:")
    for y=ymin,ymax do
      bprint("\\Line%s%s", v(xmin-.1,y), v(xmax+.1,y))
    end
    bprint("%% Vertical lines:")
    for x=xmin,xmax do
      bprint("\\Line%s%s", v(x,ymax+.1), v(x,ymin-.1))
    end
    bprint("%%")
    return out()
  end

--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "edrxpict.lua"
= beginpicture(v(-1,-2), v(3,5))
= pictgrid()
= pictgrid(v(-2,-1), v(3,4))

-- \expr{pictsizepos(v(-1.2,-2.2), v(3.2,5.2))}
-- \expr{pictaxes   (v(-1,  -2),   v(3,  5))}

--]]




-- «pictpgrid» (to ".pictpgrid")
-- This is similar do pictgrid, but uses the function p.
pictpgrid = function (xmin, ymin, xmax, ymax, e)
    e = e or 0
    local bprint, out = makebprint()
    for y=ymin,ymax do
      bprint("\\Line%s%s", p(xmin-e, y), p(xmax+e, y))
    end
    for x=xmin,xmax do
      bprint("\\Line%s%s", p(x, ymin-e), p(x, ymax+e))
    end
    return out()
  end

--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "edrxpict.lua"
p = function (a, b) return O + a*uu + b*vv end
O, uu, vv = v(1, 1), v(0.1, 0), v(0, 0.1)
= pictpgrid(1, 2, 3, 5)

--]]





-- «Piecewise» (to ".Piecewise")
--
Piecewise = Class {
  new = function (str)
      return Piecewise{points={}, pictlines={}, pictdots={}}:add(str)
    end,
  type = "Piecewise",
  __tostring = function (pw) return pw:pointstrs() end,
  __index = {
    npoints = function (pw) return #pw.points end,
    tostring = function (pw) return pw:pointstrs() end,
    pointstrs = function (pws, sep)
        local strs = {}
        for i=1,#pw.points do
          local p = pw.points[i]
          local conn,x,y,oc = p.conn or "  ", p.x, p.y, p.oc or ""
          table.insert(strs, format("%s(%s,%s)%s", conn, x, y, oc))
        end
        return table.concat(strs, sep or " ")
      end,
    --
    -- Add points and segments.
    -- Example: pw:add("(0,1)o--(1,1)o (1,2)c (1,3)o--(2,3)c--(3,2)--(4,2)c")
    add = function (pw, str)
        local pat = "([- ]*)%(([-%d.]+),([-%d.]+)%)([oc]?)"
        for conn,x,y,oc in str:gmatch(pat) do
          conn, x, y = (conn:match"-" and "--"), x+0, y+0
          table.insert(pw.points, {conn=conn, x=x, y=y, oc=oc})
          -- pw:pushpoint(conn, x, y, oc)
        end
        return pw
      end,
    --
    -- Express a piecewise function as a Lua function.
    condstbl = function (pw)
        local conds = {}
        for i=1,#pw.points do
          local P0,P1,P2 = pw.points[i], pw.points[i+1], pw.points[i+2]
          local p0,p1,p2 = P0, P1 or {}, P2 or {}
          local x0,y0,oc0       = p0.x, p0.y, p0.oc
          local x1,y1,oc1,conn1 = p1.x, p1.y, p1.oc, p1.conn
          local x2,y2,oc2,conn2 = p2.x, p2.y, p2.oc, p2.conn
          -- PP(oc0, conn1)
          if oc0 ~= "o" then
            local cond = format("(%s == x)          and %s", x0, y0)
            table.insert(conds, cond)
          end
          if conn1 then
            local cond = format("(%s < x and x < %s)", x0, x1)
            if y1 == y0 then
              cond = format("%s and %s", cond, y0)
            else
              cond = format("%s and (%s + (x - %s)/(%s - %s) * (%s - %s))",
                             cond,   y0,       x0,  x1,  x0,    y1,  y0     )
            end
            table.insert(conds, cond)
          end
        end
        return conds
      end,
    conds = function (pw) return table.concat(pw:condstbl(), "  or\n") end,
    fun0 = function (pw) return "function (x) return (\n"..pw:conds().."\n) end" end,
    fun = function (pw) return expr(pw:fun0()) end,
    --
    -- Get lines and open/closed points, for drawing.
    getj = function (pw, i)
        return (pw.points[i+1] and pw.points[i+1].conn and pw:getj(i+1)) or i
      end,
    getijs = function (pw)
        local i, j, ijs = 1, pw:getj(1), {}
        while true do
          if i < j then table.insert(ijs, {i, j}) end
          i = j + 1
          j = pw:getj(i)
          if pw:npoints() < i then return ijs end
        end
      end,
    getpoint = function (pw, i) return v(pw.points[i].x, pw.points[i].y) end,
    getpoints = function (pw, i, j)
        local ps = Points {}
        for k=i,j do ps:add(pw:getpoint(k)) end
        return ps
      end,
    topict = function (pw)
        cmds = {}
        for _,ij in ipairs(pw:getijs()) do
          table.insert(cmds, pw:getpoints(ij[1], ij[2]):line())
        end
        for i,p in ipairs(pw.points) do
          if p.oc == "o" then
            table.insert(cmds, formatt("\\put%s{\\opendot}", pw:getpoint(i)))
          elseif p.oc == "c" then
            table.insert(cmds, formatt("\\put%s{\\closeddot}", pw:getpoint(i)))
          end
        end
        return table.concat(cmds, "\n")
      end,
  },
}

pictpiecewise = function (str)
    return Piecewise.new(str):topict()
  end


--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "edrxpict.lua"
str = "(0,1)o--(1,1)o (1,2)c (1,3)o--(2,3)c--(3,2)--(4,2)c"
pw = Piecewise.new(str)
PPV(pw.points)
= pw
= pw:pointstrs(" ")
= pw:conds()
= pw:fun0()
= pw:fun()
= pw:fun()(1)
= pw:fun()(2)
= pw:fun()(2.5)

* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "edrxpict.lua"
str = "(0,1)o--(1,1)o (1,2)c (1,3)o--(2,3)c--(3,2)--(4,2)c"
pw = Piecewise.new(str)
PPV(pw:getijs())
= pw:topict()
= pictpiecewise(str)

--]]







-- «pictFxy» (to ".pictFxy")
-- (find-angg "LUA/lua50init.lua" "eval-and-L")
-- (find-angg "LUA/lua50init.lua" "lambda")
pictFxy = function (Fstr)
    local p1, p2 = pictp1, pictp2
    local xmin, xmax = min(p1[1], p2[1]), max(p1[1], p2[1])
    local ymin, ymax = min(p1[2], p2[2]), max(p1[2], p2[2])
    local bprint, out = makebprint()
    local F = lambda ("x,y => "..Fstr)
    for y=ymax,ymin,-1 do
      for x=xmin,xmax do
        print(x, y)
        bprint("\\put%s{\\tcell{%s}}", v(x,y), F(x,y))
      end
    end
    -- bprint("%%")
    return out()
  end

--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "edrxpict.lua"
= beginpicture(v(-1,-2), v(3,4))
= beginpicture(v(-1,-2), v(3,4), .7)
= pictFxy("x")

= pictaxes(v(-2,-1), v(3,4))

-- \expr{pictsizepos(v(-1.2,-2.2), v(3.2,5.2))}
-- \expr{pictaxes   (v(-1,  -2),   v(3,  5))}

--]]




-- «pict2evector» (to ".pict2evector")
-- (find-LATEX "edrxgac2.tex" "pict-Vector")
-- (find-pict2epage 6 "slope")
-- (find-pict2etext 6 "slope")
-- (find-es "tex" "pict2e-vector")
pict2evector = function (x0, y0, x1, y1)
    local dx, dy = x1 - x0, y1 - y0
    local f = function (dx, dy, len)
        return format("\\put(%.3f,%.3f){\\vector(%.3f,%.3f){%.3f}}",
          x0, y0, dx, dy, len)
      end
    if dx > 0 then return f(1, dy/dx, dx) end
    if dx < 0 then return f(-1, -dy/dx, -dx) end
    if dx == 0 and dy > 0 then return f(0, 1, dy) end
    if dx == 0 and dy < 0 then return f(0, -1, -dy) end
    error()
  end



--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "edrxpict.lua"
testvector = function (OO, vv, a,b,step)
    local bprint, out = makebprint()
    for angle=a,b,step do bprint("\\Vector%s%s", OO, OO+vv:rot(angle)) end
    return out()
  end
= testvector(v(2,1), v(2,0), 0,180,11)
= testvector(v(2,1), v(2,0), 0,360,22)

sysco = function (OO, xx, yy, OOtext, xxtext, yytext, vtextdist, Otextdist)
    local bprint, out = makebprint()
    local xxpos = OO + xx/2 + xx:rotright():unit(vtextdist)
    local yypos = OO + yy/2 + yy:rotright():unit(vtextdist)
    local OOpos = OO + (-xx-yy):unit(Otextdist or vtextdist)
    local f = function (str) return (str:gsub("!", "\\")) end
    bprint("\\Vector%s%s", OO, OO+xx)
    bprint("\\Vector%s%s", OO, OO+yy)
    bprint("\\put%s{\\cell{%s}}", OOpos, f(OOtext))
    bprint("\\put%s{\\cell{%s}}", xxpos, f(xxtext))
    bprint("\\put%s{\\cell{%s}}", yypos, f(yytext))
    return out()
  end

= sysco(v(0,0), v(1,0), v(0,1), "O", "x", "y", 0.5)
= sysco(v(0,0), v(1,0), v(0,1), "O_{xy}", "!xx", "!yy", 0.5)
= sysco(v(10,10), v(10,0), v(0,10), "O_{xy}", "!xx", "!yy", 0.5)
= sysco(v(10,10), v(10,0), v(0,10), "O_{xy}", "!xx", "!yy", 0.5, 0.5^0.5)

OO = v(2,1)
vv = v(3,0)
for a=0,360,20 do printt("\\Vector%s%s", OO, OO+vv:rot(a)) end

vv = v(3,4)
= vv:unit()
= vv:unit(10)
= vv
= vv:rot(90)
= vv:rot(-90)


-- (find-LATEXfile "edrxtikz.lua" "V.__index.norm")

--]]




-- Two-column graphs
-- (find-dn6 "picture.lua" "copyopts")
-- (find-dn6 "picture.lua" "LPicture")
-- (find-LATEX "edrxtikz.lua" "Line")

-- «TCG» (to ".TCG")
TCG = Class {
  type = "TCG",
  new  = function (opts, def, l, r, lrarrows, rlarrows)
      local dims = opts                   -- was opts.dims
      local lp = LPicture.new(opts)
      lp.def = def
      local tcg = {lp=lp,   dh=dims.dh, dv=dims.dv, eh=dims.eh, ev=dims.ev,
                   l=l+0, r=r+0, lrarrows=lrarrows, rlarrows=rlarrows}
      return TCG(tcg)
    end,
  __tostring = function (tcg) return tcg:tostring() end,
  __index = {
    tostring = function (tcg)
        return format("(%s, %s, %q, %q)", tcg.l, tcg.r, tcg.lrarrows, tcg.rlarrows)
      end,
    tolatex = function (tcg) return tcg.lp:tolatex() end,
    L = function (tcg, y) return v(0,      tcg.dv*y) end,
    R = function (tcg, y) return v(tcg.dh, tcg.dv*y) end,
    arrow = function (tcg, A, B, e)
        tcg.lp:addtex(Line.newAB(A, B, e, 1-e):pictv())
        return tcg
      end,
    lrs = function (tcg)
        for y=1,tcg.l do tcg.lp:put(tcg:L(y), y.."\\_") end
        for y=1,tcg.r do tcg.lp:put(tcg:R(y), "\\_"..y) end
        return tcg
      end,
    bus = function (tcg)
        for y=1,tcg.l do tcg.lp:put(tcg:L(y), "\\bullet") end
        for y=1,tcg.r do tcg.lp:put(tcg:R(y), "\\bullet") end
        return tcg
      end,
    strs = function (tcg, strsl, strsr)
        if type(strsl) == "string" then strsl = split(strsl) end
        if type(strsr) == "string" then strsr = split(strsr) end
        for y,str in ipairs(strsl) do tcg.lp:put(tcg:L(y), str) end
        for y,str in ipairs(strsr) do tcg.lp:put(tcg:R(y), str) end
        return tcg
      end,
    cs = function (tcg, charsl, charsr)
        return tcg:strs(split(charsl, "."), split(charsr, "."))
      end,
    cq = function (tcg, charsl, charsr)
        local lc   = function (y) return charsl:sub(y, y) end
        local rc   = function (y) return charsr:sub(y, y) end
        local lstr = function (y) return lc(y)=="L" and y.."\\_" or lc(y) end
        local rstr = function (y) return rc(y)=="R" and "\\_"..y or rc(y) end
        for y=1,#charsl do tcg.lp:put(tcg:L(y), lstr(y)) end
        for y=1,#charsr do tcg.lp:put(tcg:R(y), rstr(y)) end
        return tcg
      end,
    vs = function (tcg)
        for y=1,tcg.l-1 do tcg:arrow(tcg:L(y+1), tcg:L(y), tcg.ev) end
        for y=1,tcg.r-1 do tcg:arrow(tcg:R(y+1), tcg:R(y), tcg.ev) end
        return tcg
      end,
    hs = function (tcg)
        for l,r in tcg.lrarrows:gmatch("(%d)(%d)") do
          tcg:arrow(tcg:L(l), tcg:R(r), tcg.eh)
        end
        for l,r in tcg.rlarrows:gmatch("(%d)(%d)") do
          tcg:arrow(tcg:R(r), tcg:L(l), tcg.eh)
        end
        return tcg
      end,
    print  = function (tcg) print(tcg); return tcg end,
    lprint = function (tcg) print(tcg:tolatex()); return tcg end,
    output = function (tcg) output(tcg:tolatex()); return tcg end,
  },
}

-- «TCG-tests» (to ".TCG-tests")
--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "edrxpict.lua"
-- dims = {dv=2, dh=3, ev=0.32, eh=0.2}
opts = {scale="10pt", dims={dv=2, dh=3, ev=0.32, eh=0.2}}
opts = {scale="10pt",       dv=2, dh=3, ev=0.32, eh=0.2 }
tcg = TCG.new(opts, "foo", 4, 6, "12", "23 34"):lrs():vs():hs():lprint()
= tcg
= tcg:tolatex()

--]]




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

--]]








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