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 tickslocal 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/2endwhile (anchorhi-anchorlo)/scale > 8 do-- print('b', scale, (anchorhi-anchorlo)/scale)scale = scale*2end-- print('c', scale, (anchorhi-anchorlo)/scale)while vy(anchorlo-scale) > Menu_bar_height+20 doanchorlo = anchorlo-scaleendwhile vy(anchorhi+scale) < App.screen.height-20 doanchorhi = anchorhi+scaleend-- print('d', scale, (anchorhi-anchorlo)/scale)return anchorlo, anchorhi, scaleend
approximate_up = function(n, zeros)-- turn n into a number with n zeros-- step 1: scale downfor i=1,zeros don = n/10endn = math.ceil(n)if n == 0 then n = 1 end-- step 2: scale back uplocal magnitude = 1for i=1,zeros don = n*10magnitude = magnitude*10endreturn n, magnitudeend
test_approximate_up = function()check_eq(approximate_up(12, 1), 20, 'easy case')-- when scale down ends up at 0check_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,maxyfor _,node in pairs(nodes) doif minx == nil or node.x < minx thenminx = node.xendif maxx == nil or node.x > maxx thenmaxx = node.xendif miny == nil or node.y < miny thenminy = node.yendif maxy == nil or node.y > maxy thenmaxy = node.yendendreturn minx,miny,maxx+600,maxy+600end
load_from_iterator = function(f)local result = {}local i,line,drawing = 0, ''while true dolocal line = f()if line == nil then break endtable.insert(result, line)endif #result == 0 thentable.insert(result, '')endreturn resultend
approximate = function(n, zeros)-- turn n into a number with n zerosfor i=1,zeros don = n/10endn= math.floor(n)local magnitude = 1for i=1,zeros don = n*10magnitude = magnitude*10endreturn n, magnitudeend