ALGO: fix grahamGetCHSteps

This commit is contained in:
hasufell 2014-10-12 03:00:13 +02:00
parent 15479e3c58
commit b75f531533
No known key found for this signature in database
GPG Key ID: 220CD1C5BDEED020
2 changed files with 13 additions and 7 deletions

View File

@ -51,10 +51,8 @@ grahamGetCH vs =
-- visualizing it.
grahamGetCHSteps :: [PT] -> [[PT]]
grahamGetCHSteps vs =
(++) (reverse . g (length vs) lH $ lHRest) .
fmap (\x -> (last . reverse . g (length vs) lH $ lHRest)
++ x) $
(init . reverse . g (length vs) uH $ uHRest)
(++) (rmdups . reverse . g (length vs) lH $ lHRest)
(rmdups . init . reverse . g (length vs) uH $ uHRest)
where
sortedXY = fmap p2 . sortLex . fmap unp2 $ vs
(lH, lHRest) = first reverse . splitAt 2 $ sortedXY
@ -64,12 +62,12 @@ grahamGetCHSteps vs =
| otherwise = []
where
scan c' (y:z:xs) (x:ys)
| c' >= c = reverse (y:z:xs)
| c' >= c = y:z:xs
| ccw z y x = scan (c' + 1) (x:y:z:xs) ys
| otherwise = scan (c' + 1) (x:z:xs) ys
scan _ [x,y] [] = [y,x]
scan c' (x:y:z:xs) []
| c' >= c = reverse (x:y:z:xs)
| c' >= c = x:y:z:xs
| ccw z y x = x:y:z:xs
| otherwise = scan (c' + 1) (x:z:xs) []
scan _ xs _ = reverse xs
scan _ xs _ = xs

View File

@ -42,6 +42,14 @@ tailInit xs
| otherwise = []
-- |Remove duplicates from a list.
rmdups :: (Ord a) => [a] -> [a]
rmdups =
foldl (\seen x -> if x `elem` seen
then seen
else seen ++ [x]) []
-- |Apply a function to the first element of a tuple.
first :: (a -> b) -> (a,c) -> (b,c)
first f (x,y) = (f x, y)