charts: make clip-data handle y coords as well
Implementation is efficient: the minmax for y is only called after trimming the data on the x bounds. If the entire data set is outside the x range, handling is terminated early.char-rename
parent
f767a9bca5
commit
72c1ccba96
|
@ -3,6 +3,12 @@
|
||||||
USING: tools.test charts ;
|
USING: tools.test charts ;
|
||||||
IN: charts.tests
|
IN: charts.tests
|
||||||
|
|
||||||
|
{ { } }
|
||||||
|
[ { } { } clip-data ] unit-test
|
||||||
|
|
||||||
|
{ { } }
|
||||||
|
[ { { 0 1 } { 0 5 } } { } clip-data ] unit-test
|
||||||
|
|
||||||
! Adjustment after search is required in both directions.
|
! Adjustment after search is required in both directions.
|
||||||
{
|
{
|
||||||
{
|
{
|
||||||
|
@ -11,7 +17,7 @@ IN: charts.tests
|
||||||
{ 5 9 } { 5 10 } { 5 11 } { 5 12 }
|
{ 5 9 } { 5 10 } { 5 11 } { 5 12 }
|
||||||
}
|
}
|
||||||
} [
|
} [
|
||||||
{ 1 5 }
|
{ { 1 5 } { 0 14 } }
|
||||||
{
|
{
|
||||||
{ 0 1 } { 0 2 }
|
{ 0 1 } { 0 2 }
|
||||||
{ 1 3 } { 1 4 } { 1 5 }
|
{ 1 3 } { 1 4 } { 1 5 }
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
! Copyright (C) 2016-2017 Alexander Ilin.
|
! Copyright (C) 2016-2017 Alexander Ilin.
|
||||||
|
|
||||||
USING: accessors binary-search colors.constants kernel locals
|
USING: accessors arrays binary-search colors.constants
|
||||||
math math.order opengl opengl.gl sequences
|
combinators.short-circuit kernel locals math math.order
|
||||||
|
math.rectangles math.statistics opengl opengl.gl sequences
|
||||||
specialized-arrays.instances.alien.c-types.float ui.gadgets
|
specialized-arrays.instances.alien.c-types.float ui.gadgets
|
||||||
ui.render ;
|
ui.render ;
|
||||||
IN: charts
|
IN: charts
|
||||||
|
@ -36,21 +37,50 @@ M: chart pref-dim* drop { 300 300 } ;
|
||||||
: adjusted-head ( index elt seq -- seq' )
|
: adjusted-head ( index elt seq -- seq' )
|
||||||
[ finder find-from drop ] keep swap [ head ] when* ;
|
[ finder find-from drop ] keep swap [ head ] when* ;
|
||||||
|
|
||||||
:: in-bounds? ( bounds data -- ? )
|
! : data-rect ( data -- rect )
|
||||||
bounds first data last first < not
|
! [ [ first first ] [ last first ] bi ] keep
|
||||||
bounds second data first first > not
|
! [ second ] map minmax swapd
|
||||||
and ;
|
! 2array [ 2array ] dip <extent-rect> ;
|
||||||
|
|
||||||
PRIVATE>
|
: first-in-bounds? ( min,max pairs -- ? )
|
||||||
|
{
|
||||||
|
[ [ first ] dip last first > not ]
|
||||||
|
[ [ second ] dip first first < not ]
|
||||||
|
} 2&& ;
|
||||||
|
|
||||||
: clip-data ( bounds data -- data' )
|
: second-in-bounds? ( min,max pairs -- ? )
|
||||||
2dup in-bounds? [
|
[ second ] map minmax 2array
|
||||||
|
{
|
||||||
|
[ [ first ] dip second > not ]
|
||||||
|
[ [ second ] dip first < not ]
|
||||||
|
} 2&& ;
|
||||||
|
|
||||||
|
! : pairs-in-bounds? ( bounds pairs -- ? )
|
||||||
|
! {
|
||||||
|
! [ [ first ] dip first-in-bounds? ]
|
||||||
|
! [ [ second ] dip second-in-bounds? ]
|
||||||
|
! } 2&& ;
|
||||||
|
|
||||||
|
: clip-by-first ( min,max pairs -- pairs' )
|
||||||
|
2dup first-in-bounds? [
|
||||||
[ dup first ] dip [ search-index ] keep adjusted-tail
|
[ dup first ] dip [ search-index ] keep adjusted-tail
|
||||||
[ second ] dip [ search-index ] keep adjusted-head
|
[ second ] dip [ search-index ] keep adjusted-head
|
||||||
] [
|
] [
|
||||||
2drop { } clone
|
2drop { } clone
|
||||||
] if ;
|
] if ;
|
||||||
|
|
||||||
|
PRIVATE>
|
||||||
|
|
||||||
|
! bounds: { { first-min first-max } { second-min second-max } }
|
||||||
|
: clip-data ( bounds data -- data' )
|
||||||
|
dup empty? [ nip ] [
|
||||||
|
dupd [ first ] dip clip-by-first
|
||||||
|
dup empty? [ nip ] [
|
||||||
|
[ second ] dip [ second-in-bounds? ] keep swap
|
||||||
|
[ drop { } clone ] unless
|
||||||
|
] if
|
||||||
|
] if ;
|
||||||
|
|
||||||
! Return the bottom-left and top-right corners of the visible area.
|
! Return the bottom-left and top-right corners of the visible area.
|
||||||
: chart-axes ( chart -- seq )
|
: chart-axes ( chart -- seq )
|
||||||
drop { { 0 300 } { 300 0 } } ;
|
drop { { 0 300 } { 300 0 } } ;
|
||||||
|
|
Loading…
Reference in New Issue