Browse Source

Color lines according to depth, add bounds

master
orwell96 3 years ago
parent
commit
97c4e91d3a
  1. 77
      helpers.lua
  2. 63
      main.lua

77
helpers.lua

@ -353,79 +353,18 @@ end
local hext = { [0]="0",[1]="1",[2]="2",[3]="3",[4]="4",[5]="5",[6]="6",[7]="7",[8]="8",[9]="9",[10]="A",[11]="B",[12]="C",[13]="D",[14]="E",[15]="F"}
local dect = { ["0"]=0,["1"]=1,["2"]=2,["3"]=3,["4"]=4,["5"]=5,["6"]=6,["7"]=7,["8"]=8,["9"]=9,["A"]=10,["B"]=11,["C"]=12,["D"]=13,["E"]=14,["F"]=15}
local f = atfloor
local function hex(i)
local x=i+32768
local c4 = x % 16
x = f(x / 16)
local c3 = x % 16
x = f(x / 16)
-- Hex functions changed for 8bit
function advtrains.hex(i)
local x=math.floor(i)
local c2 = x % 16
x = f(x / 16)
x = math.floor(x / 16)
local c1 = x % 16
return (hext[c1]) .. (hext[c2]) .. (hext[c3]) .. (hext[c4])
return (hext[c1]) .. (hext[c2])
end
local function c(s,i) return dect[string.sub(s,i,i)] end
local function dec(s)
return (c(s,1)*4096 + c(s,2)*256 + c(s,3)*16 + c(s,4))-32768
end
-- Takes a position vector and outputs a encoded value suitable as table index
-- This is essentially a hexadecimal representation of the position (+32768)
-- Order (YYY)YXXXXZZZZ
function advtrains.encode_pos(pos)
return hex(pos.y) .. hex(pos.x) .. hex(pos.z)
end
-- decodes a position encoded with encode_pos
function advtrains.decode_pos(pts)
local stry = string.sub(pts, 1,4)
local strx = string.sub(pts, 5,8)
local strz = string.sub(pts, 9,12)
return vector.new(dec(strx), dec(stry), dec(strz))
end
--[[ Benchmarking code
local tdt = {}
local tlt = {}
local tet = {}
for i=1,1000000 do
tdt[i] = vector.new(math.random(-65536, 65535), math.random(-65536, 65535), math.random(-65536, 65535))
if i%1000 == 0 then
tlt[#tlt+1] = tdt[i]
end
end
local t1=os.clock()
for i=1,1000000 do
local pe = advtrains.encode_pos(tdt[i])
local pb = advtrains.decode_pos(pe)
tet[pe] = i
end
for i,v in ipairs(tlt) do
local lk = tet[advtrains.encode_pos(v)]
end
atdebug("endec",os.clock()-t1,"s")
tet = {}
t1=os.clock()
for i=1,1000000 do
local pe = minetest.pos_to_string(tdt[i])
local pb = minetest.string_to_pos(pe)
tet[pe] = i
function advtrains.dec(s)
return (c(s,1)*16 + c(s,2))
end
for i,v in ipairs(tlt) do
local lk = tet[minetest.pos_to_string(v)]
end
atdebug("pts",os.clock()-t1,"s")
--Results:
--2018-11-29 16:57:08: ACTION[Main]: [advtrains]endec 1.786451 s
--2018-11-29 16:57:10: ACTION[Main]: [advtrains]pts 2.566377 s
]]

63
main.lua

@ -5,13 +5,24 @@
local maxc = 5000
-- embed an image called "world.png"
local wimg = true
local wimg = false
-- image file resolution (not world resolution!)
local wimresx = 3000
local wimresy = 3000
-- one pixel is ... nodes
local wimscale = 4
-- y ranges and line colors
-- Minimum y level drawn
local drminy = -30
-- Color of min y level
local colminy = {r=0, g=0, b=255}
-- Maximum y level drawn
local drmaxy = 70
-- Color of max y level
local colmaxy = {r=255, g=0, b=0}
datapath = (arg[1] or "").."/"
@ -111,7 +122,7 @@ svgfile:write('viewBox="'..(-maxc)..' '..(-maxc)..' '..(2*maxc)..' '..(2*maxc)..
svgfile:write([[
<circle cx="0" cy="0" r="2" stroke="red" stroke-width="1" />
<circle cx="0" cy="0" r="5" stroke="red" stroke-width="1" />
]])
if wimg then
@ -183,16 +194,58 @@ end
plcnt = 0
local function hexcolor(clr)
return "#"..advtrains.hex(clr.r)..advtrains.hex(clr.g)..advtrains.hex(clr.b)
end
local function cfactor(ry)
local y = ry - (ry%4)
local fac = (y-drminy)/(drmaxy-drminy)
return fac
end
local function pl_header(fac)
local color = {
r = colminy.r + (colmaxy.r-colminy.r)*fac,
g = colminy.g + (colmaxy.g-colminy.g)*fac,
b = colminy.b + (colmaxy.b-colminy.b)*fac,
}
local c = hexcolor(color)
return '<polyline style="fill:none;stroke:'..c..';stroke-width:1" points="'
end
local function polyline_write(pl)
local str = {'<polyline style="fill:none;stroke:black;stroke-width:1" points="'}
local p1y = cfactor(pl[1].y)
local str = {pl_header(p1y)}
local i
local e
local lastcf = p1y
local lastldir = {x=0, y=0}
for i=1,#pl do
e = pl[i]
-- Note that we mirror y, so positive z is up
table.insert(str, e.x .. "," .. -(e.z) .. " ")
local cf = cfactor(e.y)
if cf ~= lastcf then
if lastcf <= 1 and lastcf >= 0 then
-- insert final point
-- Note that we mirror y, so positive z is up
table.insert(str, e.x .. "," .. -(e.z) .. " ")
table.insert(str, '" />\n')
plcnt = plcnt + 1
end
if cf <= 1 and cf >= 0 then
table.insert(str, pl_header(cf))
end
end
if cf <= 1 and cf >= 0 then
-- Note that we mirror y, so positive z is up
table.insert(str, e.x .. "," .. -(e.z) .. " ")
end
lastcf = cf
end
table.insert(str, '" />\n')

Loading…
Cancel
Save