2014-10-06 21:14:23 +00:00
|
|
|
{-# OPTIONS_HADDOCK ignore-exports #-}
|
|
|
|
|
2014-10-10 15:40:08 +00:00
|
|
|
module Graphics.Diagram.Plotter where
|
2014-09-30 22:05:29 +00:00
|
|
|
|
2014-10-10 15:40:08 +00:00
|
|
|
import Algebra.Vector
|
|
|
|
import Algebra.VectorTypes
|
|
|
|
import Algorithms.ConvexHull.GrahamScan
|
2014-10-25 01:15:38 +00:00
|
|
|
import Algorithms.PolygonIntersection.Core
|
2014-10-14 19:24:21 +00:00
|
|
|
import Data.Monoid
|
2014-09-30 22:05:29 +00:00
|
|
|
import Diagrams.Backend.Cairo
|
2014-10-14 19:24:21 +00:00
|
|
|
import Diagrams.Prelude hiding ((<>))
|
2014-10-10 15:40:08 +00:00
|
|
|
import Graphics.Diagram.Types
|
2014-10-10 13:40:56 +00:00
|
|
|
|
|
|
|
|
2014-10-08 17:23:40 +00:00
|
|
|
-- |Creates a Diagram that shows the coordinates from the points
|
|
|
|
-- as dots. The points and thickness of the dots can be controlled
|
2014-10-06 19:11:28 +00:00
|
|
|
-- via DiagProp.
|
2014-10-09 15:19:58 +00:00
|
|
|
coordPoints :: Diag
|
2014-10-09 22:42:15 +00:00
|
|
|
coordPoints = Diag cp
|
2014-10-06 19:11:28 +00:00
|
|
|
where
|
2014-10-25 13:40:10 +00:00
|
|
|
cp p (Object vt) = drawP vt p
|
|
|
|
cp p (Objects vts) = drawP (concat vts) p
|
|
|
|
drawP [] _ = mempty
|
|
|
|
drawP vt p =
|
2014-10-11 01:59:21 +00:00
|
|
|
position (zip (filter (inRange (dX p) (dY p)) vt)
|
2014-10-09 22:19:05 +00:00
|
|
|
(repeat dot))
|
|
|
|
where
|
|
|
|
dot = (circle $ t p :: Diagram Cairo R2) # fc black
|
2014-10-06 19:11:28 +00:00
|
|
|
|
|
|
|
|
2014-10-10 13:34:18 +00:00
|
|
|
-- |Creates a Diagram from a point that shows the coordinates
|
|
|
|
-- in text format, such as "(1.0, 2.0)".
|
|
|
|
pointToTextCoord :: PT -> Diagram Cairo R2
|
|
|
|
pointToTextCoord pt =
|
|
|
|
text ("(" ++ show x ++ ", " ++ show y ++ ")") # scale 10
|
|
|
|
where
|
|
|
|
(x, y) = unp2 pt
|
|
|
|
|
|
|
|
|
2014-10-10 13:34:38 +00:00
|
|
|
-- |Show coordinates as text above all points.
|
2014-10-10 13:03:12 +00:00
|
|
|
coordPointsText :: Diag
|
|
|
|
coordPointsText = Diag cpt
|
|
|
|
where
|
2014-10-25 13:40:10 +00:00
|
|
|
cpt p (Object vt) = drawT vt p
|
|
|
|
cpt p (Objects vts) = drawT (concat vts) p
|
|
|
|
drawT [] _ = mempty
|
|
|
|
drawT vt p
|
|
|
|
| ct p =
|
|
|
|
position $
|
|
|
|
zip vtf (pointToTextCoord <$> vtf) # translate (r2 (0, 10))
|
|
|
|
| otherwise = mempty
|
2014-10-10 21:59:02 +00:00
|
|
|
where
|
|
|
|
vtf = filter (inRange (dX p) (dY p)) vt
|
2014-10-10 13:03:12 +00:00
|
|
|
|
|
|
|
|
2014-10-25 01:15:38 +00:00
|
|
|
-- |Draw the lines of the polygon.
|
|
|
|
polyLines :: Diag
|
|
|
|
polyLines = Diag pp
|
|
|
|
where
|
2014-10-25 13:40:10 +00:00
|
|
|
pp _ (Objects []) = mempty
|
|
|
|
pp p (Objects (x:y:_)) =
|
|
|
|
strokePoly x <> strokePoly y
|
2014-10-25 01:15:38 +00:00
|
|
|
where
|
2014-10-25 13:40:10 +00:00
|
|
|
strokePoly x' =
|
|
|
|
(strokeTrail .
|
|
|
|
fromVertices $
|
|
|
|
vtf x' ++ [head . vtf $ x']) #
|
|
|
|
moveTo (head x') #
|
|
|
|
lc black
|
|
|
|
vtf = filter (inRange (dX p) (dY p))
|
|
|
|
pp _ _ = mempty
|
2014-10-25 01:15:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
-- |Show the intersection points of two polygons as red dots.
|
2014-10-25 13:40:10 +00:00
|
|
|
polyIntersection :: Diag
|
|
|
|
polyIntersection = Diag pi'
|
2014-10-25 01:15:38 +00:00
|
|
|
where
|
2014-10-25 13:40:10 +00:00
|
|
|
pi' p (Objects (x:y:_)) = position (zip vtpi (repeat dot))
|
|
|
|
where
|
|
|
|
paF = filter (inRange (dX p) (dY p)) x
|
|
|
|
pbF = filter (inRange (dX p) (dY p)) y
|
|
|
|
dot = (circle $ t p :: Diagram Cairo R2) # fc red # lc red
|
|
|
|
vtpi = intersectionPoints
|
|
|
|
. sortLexPolys
|
|
|
|
$ (sortLexPoly paF, sortLexPoly pbF)
|
|
|
|
pi' _ _ = mempty
|
2014-10-25 01:15:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
-- |Show the intersection points of two polygons as red dots.
|
2014-10-25 13:40:10 +00:00
|
|
|
polyIntersectionText :: Diag
|
|
|
|
polyIntersectionText = Diag pit'
|
2014-10-25 01:15:38 +00:00
|
|
|
where
|
2014-10-25 13:40:10 +00:00
|
|
|
pit' p (Objects (x:y:_))
|
|
|
|
| ct p =
|
|
|
|
position $
|
|
|
|
zip vtpi
|
|
|
|
(pointToTextCoord # fc red <$> vtpi) # translate (r2 (0, 10))
|
|
|
|
| otherwise = mempty
|
|
|
|
where
|
|
|
|
paF = filter (inRange (dX p) (dY p)) x
|
|
|
|
pbF = filter (inRange (dX p) (dY p)) y
|
|
|
|
vtpi = intersectionPoints
|
|
|
|
. sortLexPolys
|
|
|
|
$ (sortLexPoly paF, sortLexPoly pbF)
|
|
|
|
pit' _ _ = mempty
|
2014-10-25 01:15:38 +00:00
|
|
|
|
|
|
|
|
2014-10-08 14:39:46 +00:00
|
|
|
-- |Create a diagram which shows the points of the convex hull.
|
2014-10-13 18:14:50 +00:00
|
|
|
convexHP :: Diag
|
|
|
|
convexHP = Diag chp
|
2014-10-08 14:39:46 +00:00
|
|
|
where
|
2014-10-25 13:40:10 +00:00
|
|
|
chp p (Object vt) =
|
2014-10-14 08:56:06 +00:00
|
|
|
position (zip vtch
|
2014-10-09 22:19:05 +00:00
|
|
|
(repeat dot))
|
|
|
|
where
|
|
|
|
dot = (circle $ t p :: Diagram Cairo R2) # fc red # lc red
|
2014-10-14 08:56:06 +00:00
|
|
|
vtch = grahamCH $ filter (inRange (dX p) (dY p)) vt
|
2014-10-25 13:40:10 +00:00
|
|
|
chp _ _ = mempty
|
2014-10-08 14:39:46 +00:00
|
|
|
|
|
|
|
|
2014-10-10 13:34:38 +00:00
|
|
|
-- |Show coordinates as text above the convex hull points.
|
2014-10-13 18:14:50 +00:00
|
|
|
convexHPText :: Diag
|
|
|
|
convexHPText = Diag chpt
|
2014-10-10 13:03:12 +00:00
|
|
|
where
|
2014-10-25 13:40:10 +00:00
|
|
|
chpt p (Object vt)
|
|
|
|
| ct p =
|
|
|
|
position $
|
|
|
|
zip vtchf
|
|
|
|
(pointToTextCoord <$> vtchf) # translate (r2 (0, 10))
|
|
|
|
| otherwise = mempty
|
2014-10-10 13:03:12 +00:00
|
|
|
where
|
2014-10-13 17:49:53 +00:00
|
|
|
vtchf = grahamCH . filter (inRange (dX p) (dY p)) $ vt
|
2014-10-25 13:40:10 +00:00
|
|
|
chpt _ _ = mempty
|
2014-10-10 13:03:12 +00:00
|
|
|
|
|
|
|
|
2014-10-09 01:10:21 +00:00
|
|
|
-- |Create a diagram which shows the lines along the convex hull
|
|
|
|
-- points.
|
2014-10-13 18:14:50 +00:00
|
|
|
convexHLs :: Diag
|
|
|
|
convexHLs = Diag chl
|
2014-10-08 17:31:55 +00:00
|
|
|
where
|
2014-10-25 13:40:10 +00:00
|
|
|
chl _ (Object []) = mempty
|
|
|
|
chl p (Object vt) =
|
2014-10-09 22:19:05 +00:00
|
|
|
(strokeTrail .
|
|
|
|
fromVertices .
|
2014-10-13 17:49:53 +00:00
|
|
|
flip (++) [head $ grahamCH vtf] .
|
|
|
|
grahamCH $
|
|
|
|
vtf) #
|
|
|
|
moveTo (head $ grahamCH vtf) #
|
2014-10-09 22:19:05 +00:00
|
|
|
lc red
|
2014-10-09 16:51:32 +00:00
|
|
|
where
|
|
|
|
vtf = filter (inRange (dX p) (dY p)) vt
|
2014-10-25 13:40:10 +00:00
|
|
|
chl _ _ = mempty
|
2014-10-08 17:31:55 +00:00
|
|
|
|
|
|
|
|
2014-10-13 18:14:50 +00:00
|
|
|
-- |Create list of diagrama which describe the lines along points of a half
|
|
|
|
-- convex hull, for each iteration of the algorithm. Which half is chosen
|
|
|
|
-- depends on the input.
|
|
|
|
convexHStepsLs :: Colour Double
|
|
|
|
-> ([PT] -> [[PT]])
|
|
|
|
-> DiagProp
|
|
|
|
-> [PT]
|
|
|
|
-> [Diagram Cairo R2]
|
|
|
|
convexHStepsLs col f p xs =
|
2014-10-14 08:56:06 +00:00
|
|
|
fmap mkChDiag (f xs')
|
2014-10-09 22:19:05 +00:00
|
|
|
where
|
2014-10-14 08:56:06 +00:00
|
|
|
xs' = filter (inRange (dX p) (dY p)) xs
|
2014-10-09 22:42:15 +00:00
|
|
|
mkChDiag vt =
|
2014-10-09 22:19:05 +00:00
|
|
|
(strokeTrail .
|
|
|
|
fromVertices $
|
2014-10-14 08:56:06 +00:00
|
|
|
vt) #
|
|
|
|
moveTo (head vt) #
|
2014-10-13 18:14:50 +00:00
|
|
|
lc col
|
2014-10-13 00:58:18 +00:00
|
|
|
|
|
|
|
|
2014-10-13 18:14:50 +00:00
|
|
|
-- |Create list of diagrama which describe the lines along the lower
|
|
|
|
-- convex hull points, for each iteration of the algorithm.
|
|
|
|
convexLHStepsLs :: DiagProp -> [PT] -> [Diagram Cairo R2]
|
|
|
|
convexLHStepsLs = convexHStepsLs orange grahamLHSteps
|
|
|
|
|
|
|
|
|
|
|
|
-- |Create list of diagrama which describe the lines along the upper
|
|
|
|
-- convex hull points, for each iteration of the algorithm.
|
|
|
|
convexUHStepsLs :: DiagProp -> [PT] -> [Diagram Cairo R2]
|
|
|
|
convexUHStepsLs = convexHStepsLs purple grahamUHSteps
|
2014-10-09 01:10:21 +00:00
|
|
|
|
|
|
|
|
2014-10-06 19:11:28 +00:00
|
|
|
-- |Creates a Diagram that shows an XAxis which is bound
|
|
|
|
-- by the dimensions given in xD from DiagProp.
|
2014-10-09 15:19:58 +00:00
|
|
|
xAxis :: Diag
|
2014-10-09 22:19:05 +00:00
|
|
|
xAxis =
|
2014-10-14 19:24:21 +00:00
|
|
|
Diag hRule <>
|
|
|
|
Diag segments <>
|
2014-10-11 01:59:21 +00:00
|
|
|
Diag labels
|
2014-10-06 19:11:28 +00:00
|
|
|
where
|
2014-10-09 22:19:05 +00:00
|
|
|
hRule p _ =
|
2014-10-11 11:49:53 +00:00
|
|
|
arrowAt (p2 (xmin p, if ymin p <= 0 then 0 else ymin p))
|
2014-10-11 01:59:21 +00:00
|
|
|
(r2 (w' p, 0))
|
2014-10-09 22:19:05 +00:00
|
|
|
segments p _ =
|
2014-10-11 01:59:21 +00:00
|
|
|
hcat' (with & sep .~ sqS p)
|
|
|
|
(replicate (floor . (/) (w' p) $ sqS p)
|
|
|
|
(vrule 10)) #
|
2014-10-11 11:49:53 +00:00
|
|
|
moveTo (p2 (xmin p, if ymin p <= 0 then 0 else ymin p))
|
2014-10-09 19:37:45 +00:00
|
|
|
labels p _ =
|
2014-10-09 22:19:05 +00:00
|
|
|
position $
|
|
|
|
zip (mkPoint <$> xs)
|
2014-10-10 21:28:01 +00:00
|
|
|
((\x -> (text . show $ x) # scale 10) <$> xs)
|
2014-10-09 22:19:05 +00:00
|
|
|
where
|
|
|
|
xs :: [Int]
|
2014-10-11 01:59:21 +00:00
|
|
|
xs = take (floor . (/) (w' p) $ sqS p)
|
|
|
|
(iterate (+(floor . sqS $ p)) (floor . xmin $ p))
|
2014-10-11 11:49:53 +00:00
|
|
|
mkPoint x = p2 (fromIntegral x,
|
|
|
|
-15 + (if ymin p <= 0 then 0 else ymin p))
|
2014-10-06 19:11:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
-- |Creates a Diagram that shows an YAxis which is bound
|
|
|
|
-- by the dimensions given in yD from DiagProp.
|
2014-10-09 15:19:58 +00:00
|
|
|
yAxis :: Diag
|
2014-10-09 22:19:05 +00:00
|
|
|
yAxis =
|
2014-10-14 19:24:21 +00:00
|
|
|
Diag vRule <>
|
|
|
|
Diag segments <>
|
2014-10-11 01:59:21 +00:00
|
|
|
Diag labels
|
2014-10-06 19:11:28 +00:00
|
|
|
where
|
2014-10-09 22:19:05 +00:00
|
|
|
vRule p _ =
|
2014-10-11 11:49:53 +00:00
|
|
|
arrowAt (p2 (if xmin p <= 0 then 0 else xmin p, ymin p))
|
2014-10-11 01:59:21 +00:00
|
|
|
(r2 (0, h' p))
|
2014-10-09 22:19:05 +00:00
|
|
|
segments p _ =
|
2014-10-11 01:59:21 +00:00
|
|
|
vcat' (with & sep .~ sqS p)
|
|
|
|
(replicate (floor . (/) (h' p) $ sqS p)
|
|
|
|
(hrule 10)) #
|
2014-10-09 22:19:05 +00:00
|
|
|
alignB #
|
2014-10-11 11:49:53 +00:00
|
|
|
moveTo (p2 (if xmin p <= 0 then 0 else xmin p, ymin p))
|
2014-10-09 19:37:45 +00:00
|
|
|
labels p _ =
|
2014-10-09 22:19:05 +00:00
|
|
|
position $
|
|
|
|
zip (mkPoint <$> ys)
|
2014-10-10 21:28:01 +00:00
|
|
|
((\x -> (text . show $ x) # scale 10) <$> ys)
|
2014-10-09 19:37:45 +00:00
|
|
|
where
|
|
|
|
ys :: [Int]
|
2014-10-11 01:59:21 +00:00
|
|
|
ys = take (floor . (/) (h' p) $ sqS p)
|
|
|
|
(iterate (+(floor . sqS $ p)) (floor . ymin $ p))
|
2014-10-11 11:49:53 +00:00
|
|
|
mkPoint y = p2 (-15 + (if xmin p <= 0 then 0 else xmin p),
|
|
|
|
fromIntegral y)
|
2014-10-06 19:11:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
-- |Creates a Diagram that shows a white rectangle which is a little
|
2014-10-10 15:40:08 +00:00
|
|
|
-- bit bigger than both X and Y axis dimensions from DiagProp.
|
2014-10-09 15:19:58 +00:00
|
|
|
whiteRectB :: Diag
|
2014-10-09 22:42:15 +00:00
|
|
|
whiteRectB = Diag rect'
|
2014-10-06 19:11:28 +00:00
|
|
|
where
|
2014-10-11 01:59:21 +00:00
|
|
|
rect' p _ =
|
2014-10-13 17:58:09 +00:00
|
|
|
whiteRect (w' p + (w' p / 10)) (h' p + (h' p / 10)) #
|
2014-10-11 01:59:21 +00:00
|
|
|
moveTo (p2 (wOff p, hOff p))
|
2014-10-08 17:23:15 +00:00
|
|
|
where
|
2014-10-11 01:59:21 +00:00
|
|
|
|
2014-10-06 19:11:28 +00:00
|
|
|
|
2014-10-05 16:41:41 +00:00
|
|
|
-- |Create a white rectangle with the given width and height.
|
2014-10-06 19:22:32 +00:00
|
|
|
whiteRect :: Double -> Double -> Diagram Cairo R2
|
|
|
|
whiteRect x y = rect x y # lwG 0.00 # bg white
|
2014-10-09 16:45:37 +00:00
|
|
|
|
|
|
|
|
2014-10-09 23:08:53 +00:00
|
|
|
-- |Create a grid across the whole diagram with squares of the
|
|
|
|
-- given size in DiagProp.
|
2014-10-09 16:45:37 +00:00
|
|
|
grid :: Diag
|
2014-10-14 19:24:21 +00:00
|
|
|
grid = Diag xGrid <> Diag yGrid
|
2014-10-09 16:45:37 +00:00
|
|
|
where
|
2014-10-25 13:40:10 +00:00
|
|
|
yGrid p _
|
|
|
|
| gd p =
|
|
|
|
hcat' (with & sep .~ sqS p)
|
|
|
|
(replicate (floor . (/) (w' p) $ sqS p)
|
|
|
|
(vrule $ h' p)) #
|
|
|
|
moveTo (p2 (xmin p, hOff p)) #
|
|
|
|
lw ultraThin
|
|
|
|
| otherwise = mempty
|
|
|
|
xGrid p _
|
|
|
|
| gd p =
|
|
|
|
vcat' (with & sep .~ sqS p)
|
|
|
|
(replicate (floor . (/) (h' p) $ sqS p)
|
|
|
|
(hrule $ w' p)) #
|
|
|
|
alignB #
|
|
|
|
moveTo (p2 (wOff p, ymin p)) #
|
|
|
|
lw ultraThin
|
|
|
|
| otherwise = mempty
|
|
|
|
|
|
|
|
|
|
|
|
plotterBG :: Diag
|
|
|
|
plotterBG = mconcat [xAxis, yAxis, grid, whiteRectB]
|