module Shape
( SvgShape
, toSvg
, Transformable
, translate
, translatePoints
, rotate
, mirror
, offset
, Mergable
, merge
, optimize
) where
import qualified Text.Blaze.Svg11 as S
import Point (Point)
class SvgShape s where
toSvg :: s -> S.Svg
class Transformable s where
translate :: Point -> s -> s
translatePoints :: [Point] -> s -> [s]
translatePoints ps s = map (\p -> translate p s) ps
rotate :: Point -> Float -> s -> s
mirror :: Point -> Point -> s -> s
offset :: Point -> Bool -> s -> s
class Mergable s where
merge :: s -> s -> Float -> Maybe s
optimize :: [s] -> Float -> [s]
optimize ms = optimize' ms []
optimize' :: (Mergable m) => [m] -> [m] -> Float -> [m]
optimize' [] acc _ = acc
optimize' (m:ms) acc epsilon = optimize' remainder (mn : acc) epsilon
where (mn, remainder) = mergeCandidate m ms [] epsilon
mergeCandidate :: (Mergable m) => m -> [m] -> [m] -> Float -> (m, [m])
mergeCandidate m [] acc _ = (m, acc)
mergeCandidate m (n:ns) acc epsilon = case merge m n epsilon of
Just mn -> mergeCandidate mn ns acc epsilon
Nothing -> mergeCandidate m ns (n : acc) epsilon