2 USING: kernel quotations arrays sequences math math.ranges fry
3 opengl opengl.gl ui.render ui.gadgets.cartesian processing.shapes
10 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
12 ARTICLE: "ui.gadgets.plot" "Plot Gadget"
16 A simple gadget for ploting two dimentional functions.
18 Use the arrow keys to move around.
20 Use 'a' and 'z' keys to zoom in and out. ..
24 <plot> [ sin ] add-function gadget. ..
29 [ sin ] red function boa add-function
30 [ cos ] blue function boa add-function
35 ABOUT: "ui.gadgets.plot"
37 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
39 TUPLE: plot < cartesian functions points ;
41 : init-plot ( plot -- plot )
46 : <plot> ( -- plot ) plot new init-plot ;
48 : step-size ( plot -- step-size )
49 [ [ x-max>> ] [ x-min>> ] bi - ] [ points>> ] bi / ;
51 : plot-range ( plot -- range )
52 [ x-min>> ] [ x-max>> ] [ step-size ] tri <range> ;
54 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
56 TUPLE: function function color ;
58 GENERIC: plot-function ( plot object -- plot )
60 M: callable plot-function ( plot quotation -- plot )
61 [ dup plot-range ] dip '[ dup @ 2array ] map line-strip ;
63 M: function plot-function ( plot function -- plot )
64 dup color>> dup [ >stroke-color ] [ drop ] if
65 [ dup plot-range ] dip function>> '[ dup @ 2array ] map line-strip ;
67 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
69 : plot-functions ( plot -- plot ) dup functions>> [ plot-function ] each ;
71 : draw-axis ( plot -- plot )
73 [ [ x-min>> ] [ drop 0 ] bi 2array ]
74 [ [ x-max>> ] [ drop 0 ] bi 2array ] bi line*
76 [ [ drop 0 ] [ y-min>> ] bi 2array ]
77 [ [ drop 0 ] [ y-max>> ] bi 2array ] bi line* ;
79 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
81 USING: ui.gadgets.slate ;
83 M: plot draw-slate ( plot -- plot )
90 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
92 : add-function ( plot function -- plot )
93 over functions>> swap suffix >>functions ;
95 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
97 : x-span ( plot -- span ) [ x-max>> ] [ x-min>> ] bi - ;
98 : y-span ( plot -- span ) [ y-max>> ] [ y-min>> ] bi - ;
100 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
102 USING: ui.gestures ui.gadgets ;
104 : left ( plot -- plot )
105 dup [ x-min>> ] [ x-span 1/10 * ] bi - >>x-min
106 dup [ x-max>> ] [ x-span 1/10 * ] bi - >>x-max
109 : right ( plot -- plot )
110 dup [ x-min>> ] [ x-span 1/10 * ] bi + >>x-min
111 dup [ x-max>> ] [ x-span 1/10 * ] bi + >>x-max
114 : down ( plot -- plot )
115 dup [ y-min>> ] [ y-span 1/10 * ] bi - >>y-min
116 dup [ y-max>> ] [ y-span 1/10 * ] bi - >>y-max
119 : up ( plot -- plot )
120 dup [ y-min>> ] [ y-span 1/10 * ] bi + >>y-min
121 dup [ y-max>> ] [ y-span 1/10 * ] bi + >>y-max
124 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
126 : zoom-in-horizontal ( plot -- plot )
127 dup [ x-min>> ] [ x-span 1/10 * ] bi + >>x-min
128 dup [ x-max>> ] [ x-span 1/10 * ] bi - >>x-max ;
130 : zoom-in-vertical ( plot -- plot )
131 dup [ y-min>> ] [ y-span 1/10 * ] bi + >>y-min
132 dup [ y-max>> ] [ y-span 1/10 * ] bi - >>y-max ;
134 : zoom-in ( plot -- plot )
139 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
141 : zoom-out-horizontal ( plot -- plot )
142 dup [ x-min>> ] [ x-span 1/10 * ] bi - >>x-min
143 dup [ x-max>> ] [ x-span 1/10 * ] bi + >>x-max ;
145 : zoom-out-vertical ( plot -- plot )
146 dup [ y-min>> ] [ y-span 1/10 * ] bi - >>y-min
147 dup [ y-max>> ] [ y-span 1/10 * ] bi + >>y-max ;
149 : zoom-out ( plot -- plot )
154 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
158 { T{ mouse-enter } [ request-focus ] }
159 { T{ key-down f f "LEFT" } [ left drop ] }
160 { T{ key-down f f "RIGHT" } [ right drop ] }
161 { T{ key-down f f "DOWN" } [ down drop ] }
162 { T{ key-down f f "UP" } [ up drop ] }
163 { T{ key-down f f "a" } [ zoom-in drop ] }
164 { T{ key-down f f "z" } [ zoom-out drop ] }