Skip to content

Instantly share code, notes, and snippets.

@folkertdev
Created September 6, 2017 18:57
Show Gist options
  • Save folkertdev/c0bd23ef0406b93daa50d77099c728ef to your computer and use it in GitHub Desktop.
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
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