` "FuncGraph.awl": 2D graphs of ramdom functions ` include "winter.awl"; BgColor = White(); AxisColor = Black(); GridColor = Grey(\x80); PlotColor = Magenta(); [GridX GridY] = [0.5 0.5]; [StepX StepY] = [8 8]; !! [Widget] GraphPlot (Function Label deltaX deltaY) : [originX originY] = (Title = "Graph: " +$ Label) { ! #on_open = ([originX originY] = (-Width * deltaX / 2, -Height * deltaY / 2)), ! plot_area (rangeX rangeY) : [x y axis_x axis_y] = graphics!!{ ` clear area ` fill_color (BgColor):: fill ((rangeX[0], rangeY[0]), (rangeX[1], rangeY[1])); ` draw axis(es) ` plot_color (AxisColor):: { if (inside (axis_x = int (-originX / deltaX), rangeX)):: line ((axis_x, rangeY[0]), (axis_x, rangeY[1])); if (inside (axis_y = int (-originY / deltaY), rangeY)):: line ((rangeX[0], axis_y), (rangeX[1], axis_y)); }; ` draw grid points ` ! gridX(x) = (originX + x * deltaX) / GridX; ! gridY(y) = (originY + y * deltaY) / GridY; plot_color (GridColor):: for_inc (x, gridX(rangeX[0]) .. gridX(rangeX[1]), for_inc (y, gridY(rangeY[0]) .. gridY(rangeY[1]), plot ((x*GridX - originX) / deltaX, (y*GridY - originY) / deltaY) ) ); ` draw graph proper ` plot_color (PlotColor):: for_inc (x, rangeX, { y = ((Function ! (originX + x * deltaX)) - originY) / deltaY; y = axis_y + (axis_y - y); if (inside (y, rangeY)):: plot (x, y); }); }, ` plot_area ` ! #on_paint (L_T R_B) = plot_area ((L_T[0], R_B[0]), (L_T[1], R_B[1])), ` scroll graph view to [relX relY] origin ` ! scroll (relX relY) = { originX = -relX * deltaX; originY = -relY * deltaY; with_widget^ plot_area ([0 Width], [0 Height]); }, ` shift graph view by [stepX stepY] pixels ` ! shift (stepX stepY) = { originX =+: stepX * deltaX; originY =+: stepY * deltaY; with_widget^ plot_area ([0 Width], [0 Height]); }, ` (un)zoom graph view by [scaleX scaleY] factor ` ! zoom (scaleX scaleY) = { originX =*: scaleX; originY =*: scaleY; deltaX =*: scaleX; deltaY =*: scaleY; with_widget^ plot_area ([0 Width], [0 Height]); }, ! click_at (atX atY) = alert_box ("Clicked at:", "(x=" +$ (originX + atX * deltaX) +$ ", y=" +$ (- originY - atY * deltaY) +$ ")"), ! #on_key (action key) = { if (action > 0):: switch (key):: ( KB_Left:: shift ( StepX, 0), KB_Right:: shift (-StepX, 0), KB_Up:: shift (0, StepY), KB_Down:: shift (0, -StepY), KB_Home:: scroll (0, 0), KB_End:: scroll (0, Height), KB_PgUp:: scroll (Width, 0), KB_PgDn:: scroll (Width, Height), KB_Center:: scroll (Width % 2, Height % 2), 107:: zoom (3/4, 3/4), 109:: zoom (4/3, 4/3), ); }, ! #on_mouse_move (State From To) = if (MB_State(State) == MB_Right && #From && #To):: shift (From[0] - To[0], From[1] - To[1]), ! #on_mouse_click (action Button Point) = if (action > 0 && MB_Code(Button) == MB_Left):: click_at (Point) }; ` GraphPlot ` WinSession ( (GraphPlot ((), !(x) = (sin(x)), "sin(x)", 0.05, 0.05), [0 0], [200 200]), (GraphPlot ((), !(x) = (x*x), "x^2", 0.02, 0.02), [0 200], [200 200]), (GraphPlot ((), !(x) = (exp(x)), "exp(x)", 0.03, 0.03), [200 0], [200 200]), (GraphPlot ((), !(x) = (atan(x)), "atan(x)", 0.04, 0.04), [200 200], [200 200]), );