Created
September 6, 2017 18:57
-
-
Save folkertdev/c0bd23ef0406b93daa50d77099c728ef to your computer and use it in GitHub Desktop.
a polymorphic splitting function that splits until the increase of accuracy of splitting again is below some threshold
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Geometry.Split exposing (..) | |
type alias Config a = | |
{ length : a -> Float | |
, split : Float -> a -> ( a, a ) | |
, percentageError : Float | |
} | |
splitIntoChunks : Config a -> a -> List a | |
splitIntoChunks config curve = | |
helper config [ curve ] [] | |
{-| make the split function tail-recursive | |
-} | |
helper : Config a -> List a -> List a -> List a | |
helper config remaining accum = | |
case remaining of | |
[] -> | |
accum | |
curve :: rest -> | |
let | |
( left, right ) = | |
config.split 0.5 curve | |
lessAccurate = | |
config.length curve | |
moreAccurate = | |
config.length left + config.length right | |
average = | |
(lessAccurate + moreAccurate) / 2 | |
in | |
if (average - lessAccurate) / average > config.percentageError then | |
helper config (right :: left :: rest) accum | |
else | |
helper config rest (left :: right :: accum) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment