2 USING: kernel quotations arrays sequences math math.ranges fry
3 opengl opengl.gl ui.render ui.gadgets.cartesian processing.shapes
8 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
12 ! <plot> [ sin ] add-function gadget.
15 ! [ sin ] red function boa add-function
16 ! [ cos ] blue function boa add-function
20 ! Use the arrow keys to move around.
22 ! Use 'a' and 'z' keys to zoom in and out.
24 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
26 TUPLE: plot < cartesian functions points ;
28 : init-plot ( plot -- plot )
33 : <plot> ( -- plot ) plot new init-plot ;
35 : step-size ( plot -- step-size )
36 [ [ x-max>> ] [ x-min>> ] bi - ] [ points>> ] bi / ;
38 : plot-range ( plot -- range )
39 [ x-min>> ] [ x-max>> ] [ step-size ] tri <range> ;
41 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
43 TUPLE: function function color ;
45 GENERIC: plot-function ( plot object -- plot )
47 M: callable plot-function ( plot quotation -- plot )
48 [ dup plot-range ] dip '[ dup @ 2array ] map line-strip ;
50 M: function plot-function ( plot function -- plot )
51 dup color>> dup [ >stroke-color ] [ drop ] if
52 [ dup plot-range ] dip function>> '[ dup @ 2array ] map line-strip ;
54 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
56 : plot-functions ( plot -- plot ) dup functions>> [ plot-function ] each ;
58 : draw-axis ( plot -- plot )
60 [ [ x-min>> ] [ drop 0 ] bi 2array ]
61 [ [ x-max>> ] [ drop 0 ] bi 2array ] bi line*
63 [ [ drop 0 ] [ y-min>> ] bi 2array ]
64 [ [ drop 0 ] [ y-max>> ] bi 2array ] bi line* ;
66 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
68 USING: ui.gadgets.slate ;
70 M: plot draw-slate ( plot -- plot )
77 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
79 : add-function ( plot function -- plot )
80 over functions>> swap suffix >>functions ;
82 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
84 : x-span ( plot -- span ) [ x-max>> ] [ x-min>> ] bi - ;
85 : y-span ( plot -- span ) [ y-max>> ] [ y-min>> ] bi - ;
87 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
89 USING: ui.gestures ui.gadgets ;
91 : left ( plot -- plot )
92 dup [ x-min>> ] [ x-span 1/10 * ] bi - >>x-min
93 dup [ x-max>> ] [ x-span 1/10 * ] bi - >>x-max
96 : right ( plot -- plot )
97 dup [ x-min>> ] [ x-span 1/10 * ] bi + >>x-min
98 dup [ x-max>> ] [ x-span 1/10 * ] bi + >>x-max
101 : down ( plot -- plot )
102 dup [ y-min>> ] [ y-span 1/10 * ] bi - >>y-min
103 dup [ y-max>> ] [ y-span 1/10 * ] bi - >>y-max
106 : up ( plot -- plot )
107 dup [ y-min>> ] [ y-span 1/10 * ] bi + >>y-min
108 dup [ y-max>> ] [ y-span 1/10 * ] bi + >>y-max
111 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
113 : zoom-in-horizontal ( plot -- plot )
114 dup [ x-min>> ] [ x-span 1/10 * ] bi + >>x-min
115 dup [ x-max>> ] [ x-span 1/10 * ] bi - >>x-max ;
117 : zoom-in-vertical ( plot -- plot )
118 dup [ y-min>> ] [ y-span 1/10 * ] bi + >>y-min
119 dup [ y-max>> ] [ y-span 1/10 * ] bi - >>y-max ;
121 : zoom-in ( plot -- plot )
126 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
128 : zoom-out-horizontal ( plot -- plot )
129 dup [ x-min>> ] [ x-span 1/10 * ] bi - >>x-min
130 dup [ x-max>> ] [ x-span 1/10 * ] bi + >>x-max ;
132 : zoom-out-vertical ( plot -- plot )
133 dup [ y-min>> ] [ y-span 1/10 * ] bi - >>y-min
134 dup [ y-max>> ] [ y-span 1/10 * ] bi + >>y-max ;
136 : zoom-out ( plot -- plot )
141 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
145 { T{ mouse-enter } [ request-focus ] }
146 { T{ key-down f f "LEFT" } [ left drop ] }
147 { T{ key-down f f "RIGHT" } [ right drop ] }
148 { T{ key-down f f "DOWN" } [ down drop ] }
149 { T{ key-down f f "UP" } [ up drop ] }
150 { T{ key-down f f "a" } [ zoom-in drop ] }
151 { T{ key-down f f "z" } [ zoom-out drop ] }