RBMZ3SYOLDBQNQUZRBKMSUZXCOJXHQX57HQMZKAS7ZASRPTQA6NAC
tick_spec = function(lo, hi)
-- given an interval of numbers, return a nice set of ticks to draw for the interval
-- the ticks will try to be at multiples of 10, or some multiple of a power of 10
-- we don't want to overwhelm the viewer, so we'll aim for between 2 and 4 ticks
-- the ticks will be specified as a low and high value, and a delta between consecutive ticks
local anchorlo, scale = approximate(lo, order_of_magnitude(hi-lo))
local anchorhi = approximate_up(hi, order_of_magnitude(hi-lo))
-- print('---', lo, hi, order_of_magnitude(hi-lo), anchorlo, anchorhi, scale)
while (anchorhi-anchorlo)/scale < 4 do
-- print('a', anchorlo, anchorhi, scale, (anchorhi-anchorlo)/scale)
scale = scale/2
end
while (anchorhi-anchorlo)/scale > 8 do
-- print('b', scale, (anchorhi-anchorlo)/scale)
scale = scale*2
end
-- print('c', scale, (anchorhi-anchorlo)/scale)
while vy(anchorlo-scale) > Menu_bar_height+20 do
anchorlo = anchorlo-scale
end
while vy(anchorhi+scale) < App.screen.height-20 do
anchorhi = anchorhi+scale
end
-- print('d', scale, (anchorhi-anchorlo)/scale)
return anchorlo, anchorhi, scale
end
approximate_up = function(n, zeros)
-- turn n into a number with n zeros
-- step 1: scale down
for i=1,zeros do
n = n/10
end
n = math.ceil(n)
if n == 0 then n = 1 end
-- step 2: scale back up
local magnitude = 1
for i=1,zeros do
n = n*10
magnitude = magnitude*10
end
return n, magnitude
end
test_approximate_up = function()
check_eq(approximate_up(12, 1), 20, 'easy case')
-- when scale down ends up at 0
check_eq(approximate_up(2, 1), 10, 'below 5')
check_eq(approximate_up(7, 1), 10, 'above 5')
end
surface_bounds = function(nodes)
local minx,miny,maxx,maxy
for _,node in pairs(nodes) do
if minx == nil or node.x < minx then
minx = node.x
end
if maxx == nil or node.x > maxx then
maxx = node.x
end
if miny == nil or node.y < miny then
miny = node.y
end
if maxy == nil or node.y > maxy then
maxy = node.y
end
end
return minx,miny,maxx+600,maxy+600
end
load_from_iterator = function(f)
local result = {}
local i,line,drawing = 0, ''
while true do
local line = f()
if line == nil then break end
table.insert(result, line)
end
if #result == 0 then
table.insert(result, '')
end
return result
end
approximate = function(n, zeros)
-- turn n into a number with n zeros
for i=1,zeros do
n = n/10
end
n= math.floor(n)
local magnitude = 1
for i=1,zeros do
n = n*10
magnitude = magnitude*10
end
return n, magnitude
end