cga/Diagram.hs

64 lines
2.0 KiB
Haskell
Raw Normal View History

2014-09-30 22:05:29 +00:00
module Diagram where
import Defaults
2014-09-30 22:05:29 +00:00
import Diagrams.Prelude
import Diagrams.Backend.Cairo
import Meshparser
import Util
instance Def DiagProp where
def = defaultProp
2014-10-01 21:02:43 +00:00
-- |Holds the properties for a Diagram, like thickness of 2d points etc.
data DiagProp = MkProp {
2014-10-05 16:09:24 +00:00
-- |The thickness of the dots.
t :: Double,
-- |The dimensions of the x-axis.
dX :: (Double, Double),
-- |The dimensions of the y-axis.
dY :: (Double, Double)
}
-- |The default properties of the Diagram.
defaultProp :: DiagProp
2014-10-05 16:09:24 +00:00
defaultProp = MkProp 2 (0,500) (0,500)
2014-09-30 22:05:29 +00:00
-- |Create the Diagram from the VTable.
diagFromVTable :: DiagProp -> VTable -> Diagram Cairo R2
2014-10-02 12:30:08 +00:00
diagFromVTable prop vt
2014-10-05 16:09:24 +00:00
= position (zip (map mkPoint . filter (inRange (dX prop) (dY prop)) $ vt)
(repeat dot)) # moveTo (p2(xOffset, yOffset))
2014-10-05 19:33:22 +00:00
`atop` hrule (xuD - xlD) # moveTo (p2(0, yOffset))
`atop` vrule (yuD - ylD) # moveTo (p2(xOffset, 0))
`atop` emptyRect (xuD - xlD + 50) (yuD - ylD + 50)
2014-10-02 11:13:58 +00:00
where dot = (circle $
t prop :: Diagram Cairo R2) # fc black
2014-10-02 11:13:58 +00:00
mkPoint (x,y) = p2 (x,y)
2014-10-05 16:09:24 +00:00
xlD = fst $ dX prop
xuD = snd $ dX prop
ylD = fst $ dY prop
yuD = snd $ dY prop
-- 'Diagrams' sets (0,0) to be in the middle of the
-- drawing area, so we need to shift it depending
-- on the given dimensions.
xOffset = (negate xlD / 2) - (xuD / 2)
yOffset = (negate ylD / 2) - (yuD / 2)
2014-09-30 22:05:29 +00:00
2014-10-05 13:50:52 +00:00
2014-10-05 13:57:31 +00:00
-- |Create the Diagram from a String which is supposed to be the contents
-- of an obj file.
diagFromString :: DiagProp -> String -> Diagram Cairo R2
diagFromString prop mesh
= diagFromVTable prop .
meshToArr $
mesh
-- |Create a white rectangle with the given width and height.
emptyRect :: Double -> Double -> Diagram Cairo R2
emptyRect x y = rect x y # lwG 0.00 # bg white