|
Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
-- This file:
-- http://angg.twu.net/LUA/Cabos1.lua.html
-- http://angg.twu.net/LUA/Cabos1.lua
-- (find-angg "LUA/Cabos1.lua")
-- Author: Eduardo Ochs <eduardoochs@gmail.com>
--
-- (defun e () (interactive) (find-angg "LUA/Cabos1.lua"))
-- Supersedes: (find-anggfile "LUA/CabosNaDiagonal.lua")
-- See: (find-angg "GNUPLOT/")
-- See: (find-angg "GNUPLOT/piramide-1.dem")
-- See: (find-angg "GNUPLOT/piramide-2.dem")
-- «.XyGrid» (to "XyGrid")
-- «.XyGrid-tests» (to "XyGrid-tests")
-- «.CabosNaDiagonal» (to "CabosNaDiagonal")
-- «.CabosNaDiagonal-tests» (to "CabosNaDiagonal-tests")
isint = function (x) return math.floor(x) == x end
ishalf = function (x) return isint(x + 0.5) end
split2d = function (bigstr)
local bigarr = VTable {}
for i,line in ipairs(splitlines(bigstr)) do
local sline = split(line)
if #sline > 0 then table.insert(bigarr, HTable(sline)) end
end
return bigarr
end
-- __ __ ____ _ _
-- \ \/ / _ / ___|_ __(_) __| |
-- \ / | | | | _| '__| |/ _` |
-- / \ |_| | |_| | | | | (_| |
-- /_/\_\__, |\____|_| |_|\__,_|
-- |___/
--
-- «XyGrid» (to ".XyGrid")
--
XyGrid = Class {
type = "XyGrid",
from = function (bigstr, step)
local bigarr = split2d(bigstr)
if step == 0.5 then
local maxx = (#bigarr[1]-1)/2
local maxy = (#bigarr -1)/2
return XyGrid {bigarr=bigarr, maxx=maxx, maxy=maxy, step=step}
else
local maxx = #bigarr[1]-1
local maxy = #bigarr -1
return XyGrid {bigarr=bigarr, maxx=maxx, maxy=maxy, step=step}
end
end,
expand0 = function (bigstr, prefix)
return XyGrid.from(bigstr, 1):alttostring((prefix or "").." ")
end,
expand = function (bigstr, prefix)
prefix = prefix or ""
local body = XyGrid.expand0(bigstr, prefix)
return prefix.."= CabosNaDiagonal.from [[\n"..body.."\n"..prefix.."]]"
end,
expandg = function (bigstr)
return XyGrid.expand(bigstr, "#: ")
end,
__tostring = function (gr)
return gr:tostring()
end,
__index = {
xtocol = function (gr, x) return 1 + x/gr.step end,
ytorow = function (gr, y) return #gr.bigarr - y/gr.step end,
get = function (gr, x, y) return gr.bigarr[gr:ytorow(y)][gr:xtocol(x)] end,
z = function (gr, x, y) return tonumber(gr:get(x,y)) end,
xs = function (gr, step) return HTable(seq(0, gr.maxx, step or gr.step)) end,
ys = function (gr, step) return VTable(seq(gr.maxy, 0, -(step or gr.step))) end,
zs = function (c)
local ytozs = function (y)
return HTable(map(function (x) return c:z(x,y) end, c:xs(1)))
end
return VTable(map(ytozs, c:ys(1)))
end,
maxz = function (c)
local arrmax = function (arr) return foldl1(max, arr) end
return arrmax(map(arrmax, c:zs()))
end,
--
ytostring = function (gr, y, step)
local xtostr = function (x) return gr:get(x,y) end
return mapconcat(xtostr, gr:xs(step), " ")
end,
tostring = function (gr, step)
local ytos = function (y) return gr:ytostring(y, step) end
return mapconcat(ytos, gr:ys(step), "\n")
end,
--
altget = function (gr, x, y)
if isint(x) and isint (y) then return gr:get(x,y) end
if isint(x) and ishalf(y) then return "|" end
if ishalf(x) and isint(y) then return "-" end
local nw,ne = gr:z(x-0.5, y+0.5), gr:z(x+0.5, y+0.5)
local sw,se = gr:z(x-0.5, y-0.5), gr:z(x+0.5, y-0.5)
if ne-nw == se-sw then return "." else return "?" end
end,
alttostring = function (gr, prefix)
local xs,ys = gr:xs(0.5), gr:ys(0.5)
local altytostring = function (y)
return (prefix or "")
.. mapconcat(function (x) return gr:altget(x, y) end, xs, " ")
end
return mapconcat(altytostring, ys, "\n")
end,
},
}
-- «XyGrid-tests» (to ".XyGrid-tests")
--[==[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "Cabos1.lua"
bigstr = [[
10 - 11 - 12
| . | \ |
13 - 14 - 16
]]
= split2d(bigstr)
gr = XyGrid.from(bigstr, 0.5)
= gr:xs()
= gr:ys()
= gr:get(0,0)
= gr:get(1,0)
= gr:zs()
= gr:maxz()
= gr:tostring()
= gr:tostring(1)
= gr
= gr:alttostring()
= XyGrid.expand [[
0 1 2
3 4 6
]]
= XyGrid.expandg [[
0 1 2
3 4 6
]]
--]==]
-- ____ _
-- / ___|__ _| |__ ___ ___
-- | | / _` | '_ \ / _ \/ __|
-- | |__| (_| | |_) | (_) \__ \ _ _ _
-- \____\__,_|_.__/ \___/|___/ (_) (_) (_)
--
-- «CabosNaDiagonal» (to ".CabosNaDiagonal")
--
CabosNaDiagonal = Class {
type = "CabosNaDiagonal",
from = function (bigstr)
local xygrid = XyGrid.from(bigstr, 0.5)
return CabosNaDiagonal { bigstr=bigstr, xygrid=xygrid }
end,
__tostring = function (c) return c:gnuplotbody() end,
__index = {
maxx = function (c) return c.xygrid.maxx end,
maxy = function (c) return c.xygrid.maxy end,
maxz = function (c) return c.xygrid:maxz() end,
get = function (c, x, y) return c.xygrid:get(x, y) end,
z = function (c, x, y)
return c.xygrid:z(x, y)
end,
xyz = function (c, x, y)
return format("%s,%s,%s", x, y, c:z(x, y))
end,
vertices = function (c, x, y)
local nw, ne = c:xyz(x, y+1), c:xyz(x+1, y+1)
local sw, se = c:xyz(x, y), c:xyz(x+1, y)
middle = c:get(x+0.5, y+0.5)
return nw, ne, sw, se, middle
end,
square = function (c, nw, ne, sw, se)
return format("%s to %s to %s to %s to %s", sw, se, ne, nw, sw)
end,
triangles = function (c, nw, ne, sw, se, middle)
if middle == "/" then
return format("%s to %s to %s to %s", sw, ne, nw, sw),
format("%s to %s to %s to %s", sw, se, ne, sw)
elseif middle == "\\" then
return format("%s to %s to %s to %s", sw, se, nw, sw),
format("%s to %s to %s to %s", se, ne, nw, se)
else PP("bad middle", middle); error()
end
end,
--
sots0 = function (c, x, y)
local nw, ne, sw, se, middle = c:vertices(x, y)
if middle == "."
then return c:square(nw, ne, sw, se)
else return c:triangles(nw, ne, sw, se, middle)
end
end,
sots1 = function (c)
local sots = {}
for y=c:maxy()-1,0,-1 do
for x=0,c:maxx()-1 do
local o1,o2 = c:sots0(x, y)
table.insert(sots, o1)
table.insert(sots, o2)
end
end
return sots
end,
sots2 = function (c)
local sots = c:sots1()
local f = function (n)
return format("set obj %d polygon from %s", n, sots[n])
end
local bigstr = mapconcat(f, seq(1, #sots), "\n")
return bigstr..format("\n\nn = %d", #sots)
end,
sots3 = function (c)
local maxx,maxy,maxz = c:maxx(),c:maxy(),c:maxz()
local fmt = "set xrange [0:%d]; set yrange [0:%d]; set zrange [0:%d]"
return c:sots2().."\n"..format(fmt, maxx, maxy, maxz)
end,
gnuplotbody = function (c)
return c:sots3()
end,
},
}
-- «CabosNaDiagonal-tests» (to ".CabosNaDiagonal-tests")
--[==[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "Cabos1.lua"
c = CabosNaDiagonal.from [[
10 - 11 - 12
| . | \ |
13 - 14 - 15
]]
= c:z(0,0)
= c:z(1,0)
= c:z(1,1)
= c:sots0(0,0)
= c:sots0(1,0)
= c:sots2()
= c:sots3()
= c:gnuplotbody()
--]==]
-- Local Variables:
-- coding: utf-8-unix
-- End: