diff --git a/charts-tests.factor b/charts-tests.factor new file mode 100644 index 0000000000..49bcdaa199 --- /dev/null +++ b/charts-tests.factor @@ -0,0 +1,22 @@ +! Copyright (C) 2017 Alexander Ilin. + +USING: tools.test charts ; +IN: charts.tests + +! Adjustment after search is required in both directions. +{ + { + { 1 3 } { 1 4 } { 1 5 } + { 2 6 } { 3 7 } { 4 8 } + { 5 9 } { 5 10 } { 5 11 } { 5 12 } + } +} [ + { 1 5 } + { + { 0 1 } { 0 2 } + { 1 3 } { 1 4 } { 1 5 } + { 2 6 } { 3 7 } { 4 8 } + { 5 9 } { 5 10 } { 5 11 } { 5 12 } + { 6 13 } { 7 14 } + } clip-data +] unit-test diff --git a/charts.factor b/charts.factor index 8b779f99f7..e8fc499c6b 100644 --- a/charts.factor +++ b/charts.factor @@ -1,8 +1,9 @@ ! Copyright (C) 2016-2017 Alexander Ilin. -USING: accessors colors.constants kernel math opengl opengl.gl -sequences specialized-arrays.instances.alien.c-types.float -ui.gadgets ui.render ; +USING: accessors binary-search colors.constants kernel locals +math math.order opengl opengl.gl sequences +specialized-arrays.instances.alien.c-types.float ui.gadgets +ui.render ; IN: charts TUPLE: chart < gadget ; @@ -21,6 +22,35 @@ M: chart pref-dim* drop { 300 300 } ; [ (line-vertices) gl-vertex-pointer GL_LINES 0 ] keep length glDrawArrays ; + ] with search ; + +: finder ( elt seq -- seq quot ) + [ first ] dip [ first = not ] with ; inline + +: adjusted-tail ( index elt seq -- seq' ) + [ finder find-last-from drop ] keep swap [ 1 + tail ] when* ; + +: adjusted-head ( index elt seq -- seq' ) + [ finder find-from drop ] keep swap [ head ] when* ; + +:: in-bounds? ( bounds data -- ? ) + bounds first data last first < not + bounds second data first first > not + and ; + +PRIVATE> + +: clip-data ( bounds data -- data' ) + 2dup in-bounds? [ + [ dup first ] dip [ search-index ] keep adjusted-tail + [ second ] dip [ search-index ] keep adjusted-head + ] [ + 2drop { } clone + ] if ; + ! Return the bottom-left and top-right corners of the visible area. : chart-axes ( chart -- seq ) drop { { 0 300 } { 300 0 } } ;