charts: add clip-data with test

char-rename
Alexander Iljin 2017-01-06 22:00:31 +03:00 committed by John Benediktsson
parent 66eb038eeb
commit f767a9bca5
2 changed files with 55 additions and 3 deletions

22
charts-tests.factor Normal file
View File

@ -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

View File

@ -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 ;
<PRIVATE
: search-index ( elt seq -- index elt )
[ first <=> ] 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 } } ;