Make Gif animation more nice, TODO: IMPROVE CODE

This commit is contained in:
hasufell 2014-10-13 02:58:18 +02:00
parent 4936023c14
commit 87393af71a
No known key found for this signature in database
GPG Key ID: 220CD1C5BDEED020
3 changed files with 62 additions and 33 deletions

View File

@ -102,23 +102,16 @@ grahamGetCH vs =
-- |Compute all steps of the graham scan algorithm to allow -- |Compute all steps of the graham scan algorithm to allow
-- visualizing it. -- visualizing it.
grahamGetCHSteps :: [PT] -> [[PT]] -- Whether the upper or lower hull is computed depends on the input.
grahamGetCHSteps vs = grahamGetCHSteps :: Int -> [PT] -> [PT] -> [[PT]]
(++) (rmdups . reverse . g ((* 2) . length $ vs) lH $ lHRest) grahamGetCHSteps c xs' ys'
(rmdups . init . reverse . g ((* 2) . length $ vs) uH $ uHRest) | c >= 0 = scanH 0 xs' ys' : grahamGetCHSteps (c - 1) xs' ys'
where
sortedXY = fmap p2 . sortLex . fmap unp2 $ vs
(lH, lHRest) = first reverse . splitAt 3 $ sortedXY
(uH, uHRest) = first reverse . splitAt 3 . reverse $ sortedXY
g c xs' ys'
| c >= 0 = scanH 0 xs' ys' : g (c - 1) xs' ys'
| otherwise = [] | otherwise = []
where where
scanH c' hs@(x:y:z:xs) (r':rs') scanH c' hs@(x:y:z:xs) (r':rs')
| c' >= c = hs | c' >= c = hs
| notcw z y x = scanH (c' + 1) (r':hs) rs' | notcw z y x = scanH (c' + 1) (r':hs) rs'
| otherwise = scanH (c' + 1) (x:z:xs) (r':rs') | otherwise = scanH (c' + 1) (x:z:xs) (r':rs')
scanH _ [x,y] [] = [y,x]
scanH c' hs@(x:y:z:xs) [] scanH c' hs@(x:y:z:xs) []
| c' >= c = hs | c' >= c = hs
| notcw z y x = hs | notcw z y x = hs
@ -127,3 +120,23 @@ grahamGetCHSteps vs =
| c' >= c = hs | c' >= c = hs
| otherwise = scanH (c' + 1) (r':hs) rs' | otherwise = scanH (c' + 1) (r':hs) rs'
scanH _ xs _ = xs scanH _ xs _ = xs
grahamGetUpperHullSteps :: [PT] -> [[PT]]
grahamGetUpperHullSteps vs =
(++) [getLastX 2 sortedXY] . rmdups . init . reverse . grahamGetCHSteps ((* 2) . length $ vs) uH $
uHRest
where
sortedXY = fmap p2 . sortLex . fmap unp2 $ vs
(uH, uHRest) = first reverse . splitAt 3 . reverse $ sortedXY
grahamGetLowerHullSteps :: [PT] -> [[PT]]
grahamGetLowerHullSteps vs =
(++) [take 2 sortedXY] . rmdups . reverse . grahamGetCHSteps ((* 2) . length $ vs) lH $ lHRest
where
sortedXY = fmap p2 . sortLex . fmap unp2 $ vs
(lH, lHRest) = first reverse . splitAt 3 $ sortedXY
{- - (++) (rmdups . reverse . g ((* 2) . length $ vs) lH $ lHRest) -}
{- - (rmdups . init . reverse . g ((* 2) . length $ vs) uH $ uHRest) -}

View File

@ -18,11 +18,16 @@ gifDiag p xs =
fmap (\x -> x <> nonChDiag) . fmap (\x -> x <> nonChDiag) .
flip (++) flip (++)
[mkDiag (convexHullPointsText `mappend` [mkDiag (convexHullPointsText `mappend`
convexHullLines `mappend`
convexHullPoints) convexHullPoints)
p xs] $ p xs <> lastUpperHull <> lastLowerHull] $
(convexHullLinesInterval p xs) (lowerHullList ++
((<> lastLowerHull) <$>
upperHullList))
where where
upperHullList = convexHullLinesIntervalUpper p xs
lastUpperHull = last upperHullList
lowerHullList = convexHullLinesIntervalLower p xs
lastLowerHull = last lowerHullList
-- add the x-axis and the other default stuff -- add the x-axis and the other default stuff
nonChDiag = nonChDiag =
mconcat . mconcat .

View File

@ -85,19 +85,30 @@ convexHullLines = Diag chl
vtf = filter (inRange (dX p) (dY p)) vt vtf = filter (inRange (dX p) (dY p)) vt
-- |Same as showConvexHullLines, except that it returns an array convexHullLinesIntervalLower :: DiagProp -> [PT] -> [Diagram Cairo R2]
-- of diagrams with each step of the algorithm. convexHullLinesIntervalLower p xs =
-- Unfortunately this is very difficult to implement as a Diag (TODO). fmap mkChDiag (grahamGetLowerHullSteps xs)
convexHullLinesInterval :: DiagProp -> [PT] -> [Diagram Cairo R2]
convexHullLinesInterval p xs =
fmap mkChDiag (grahamGetCHSteps xs)
where where
mkChDiag vt = mkChDiag vt =
(strokeTrail . (strokeTrail .
fromVertices $ fromVertices $
vtf) # vtf) #
moveTo (head vtf) # moveTo (head vtf) #
lc red lc orange
where
vtf = filter (inRange (dX p) (dY p)) vt
convexHullLinesIntervalUpper :: DiagProp -> [PT] -> [Diagram Cairo R2]
convexHullLinesIntervalUpper p xs =
fmap mkChDiag (grahamGetUpperHullSteps xs)
where
mkChDiag vt =
(strokeTrail .
fromVertices $
vtf) #
moveTo (head vtf) #
lc purple
where where
vtf = filter (inRange (dX p) (dY p)) vt vtf = filter (inRange (dX p) (dY p)) vt