module Graphics.PS.Query ( startPt, endPt
, mkValid
, approx, close ) where
import Graphics.PS.Pt
import Graphics.PS.Bezier
import Graphics.PS.Path
startPt :: Path -> Maybe Pt
startPt (MoveTo p) = Just p
startPt (Join p _) = startPt p
startPt _ = Nothing
startPt' :: Path -> Maybe Pt
startPt' (MoveTo p) = Just p
startPt' (LineTo p) = Just p
startPt' (CurveTo p _ _) = Just p
startPt' (Join p _) = startPt' p
startPt' _ = Nothing
mkValid :: Path -> Path
mkValid p = f (startPt' p)
where f (Just q) = MoveTo q +++ p
f Nothing = p
endPt :: Path -> Maybe Pt
endPt (MoveTo p) = Just p
endPt (LineTo p) = Just p
endPt (CurveTo _ _ p) = Just p
endPt (Join _ p) = endPt p
endPt _ = Nothing
close :: Path -> Path
close p = f (startPt p)
where f (Just q) = p +++ LineTo q
f Nothing = p
approx :: Double -> Path -> Path
approx n (Join a (CurveTo p q r)) =
let is = [0, (1.0/n) .. 1.0]
f (Just p1) p2 p3 p4 = map (bezier4 p1 p2 p3 p4) is
f Nothing _ _ _ = []
in a +++ line (f (endPt a) p q r)
approx _ p = p