plot-gtk-ui-0.0.1.0: lib/Graphics/Rendering/Plot/Gtk/UI.hs
-----------------------------------------------------------------------------
-- |
-- Module : Graphics.Rendering.Plot.Gtk.UI
-- Copyright : (c) Sumit Sahrawat
-- License : GPL-2
--
-- Maintainer : sumit.sahrawat.apm13@iitbhu.ac.in
-- Stability : provisional
-- Portability : portable
--
-- Quick plotting for functions + dynamic plotting of functions
--
-- 'plotStatic' can be used to plot functions of the form /f(x)=.../
--
-- 'plotDynamic' can be used to plot functions of the form /f(x, a, b) =
-- .../ where /a/ and /b/ are additional parameters. Sliders are
-- provided for manipulating the values of /a/ and /b/, and the plot is
-- animated in real-time.
--
--------------------------------------------------------------------------------
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeFamilies #-}
module Graphics.Rendering.Plot.Gtk.UI
( plotStatic
, plotDynamic
) where
--------------------------------------------------------------------------------
-- Standard Libraries
import Data.IORef (newIORef)
--------------------------------------------------------------------------------
-- Other Libraries
import Data.Vector.Fixed (Dim, S, Vector, Z)
import qualified Data.Vector.Fixed as V
import Data.Vector.Fixed.Unboxed (Vec)
--------------------------------------------------------------------------------
-- Custom Modules
import Paths_plot_gtk_ui (getDataFileName)
import Graphics.Rendering.Plot.Gtk.UI.PlotWindow
import Graphics.Rendering.Plot.Gtk.UI.Settings
--------------------------------------------------------------------------------
defSet :: FigureSettings
defSet = defaultFigureSettings
--------------------------------------------------------------------------------
-- | Quickly create AdjustmentSettings
argRange :: (Double, Double) -- ^ Lower and upper range
-> AdjustmentSettings
argRange (x, y) = aSetRange ((x + y) / 2) x y
--------------------------------------------------------------------------------
-- | Plots functions of multiple arguments, provided all arguments
-- have specified ranges.
plot :: ([Double] -> Double -> Double) -- ^ Function
-> [(Double, Double)] -- ^ Ranges for all but the primary arg
-> FigureSettings -- ^ Should contain the primary range
-> IO ()
plot func rangeTail fset = do
iofset <- newIORef fset
plotGlade <- getDataFileName "plot-window.glade"
plotWindow plotGlade iofset (map argRange rangeTail) func
--------------------------------------------------------------------------------
-- | Plot functions that don't depend on extra parameters
plotStatic :: (Double -> Double) -- ^ Function to plot
-> (Double, Double) -- ^ Range for abscissa (horizontal axis)
-> IO ()
plotStatic func r =
let f :: Vec (S Z) Double -> Double
f = func . V.head
in plotDynamic f ((V.convert . V.Only $ r) :: Vec (S Z) (Double, Double))
--------------------------------------------------------------------------------
-- | Plot functions that depend on extra parameters
plotDynamic :: (Vector u a, Vector v (a, a),
a ~ Double, Dim u ~ Dim v, Dim v ~ S n, V.Arity n)
=> (u Double -> Double)
-> v (Double, Double)
-> IO ()
plotDynamic func ranges = let (x:xs) = V.toList ranges
f zs z = func . V.fromList $ (z:zs)
in plot f xs defSet { xRange = Just x }
--------------------------------------------------------------------------------