From 59e47aa4df62a8760a254bd409c4ec84187b8005 Mon Sep 17 00:00:00 2001 From: Glyn Owen Hanmer <1295698+glynternet@users.noreply.github.com> Date: Thu, 16 Jan 2025 03:44:49 -0700 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20=20extract=20component=20t?= =?UTF-8?q?hat=20calculates=20x=20and=20y=20points?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- elm/elevationprofile/src/Main.elm | 58 +++++++++++++++++++------------ 1 file changed, 35 insertions(+), 23 deletions(-) diff --git a/elm/elevationprofile/src/Main.elm b/elm/elevationprofile/src/Main.elm index 945f769..b5a5b7f 100644 --- a/elm/elevationprofile/src/Main.elm +++ b/elm/elevationprofile/src/Main.elm @@ -251,12 +251,14 @@ profile profileData = ((profileData |> List.foldl (accumulatePoints - { svgHeight = toFloat svgHeight - , svgWidth = toFloat svgWidth - , maxDistance = maxDistance - , minElevation = minElevation - , maxElevation = maxElevation - } + (xyCalculator + { svgHeight = toFloat svgHeight + , svgWidth = toFloat svgWidth + , maxDistance = maxDistance + , minElevation = minElevation + , maxElevation = maxElevation + } + ) ) ( Nothing, [] ) |> Tuple.second @@ -283,23 +285,21 @@ profile profileData = ] - --- accumulatePoints takes in a config to form an accumulator. --- The accumulator will take a point and form a tuple of (maybePrevPoint, currentLines), where each line in currentLines --- is a formed Svg.Svg msg. --- The maybePrevPoint is a Maybe (String, String) because the number values have been converted in a prior iteration of --- the accumulation loop. +type alias XYCalculator = + { x : Float -> String + , y : Float -> String + } -accumulatePoints : +xyCalculator : { svgWidth : Float , svgHeight : Float , maxDistance : Float , minElevation : Float , maxElevation : Float } - -> (ElevationPoint -> ( Maybe ( String, String ), List (Svg.Svg msg) ) -> ( Maybe ( String, String ), List (Svg.Svg msg) )) -accumulatePoints cfg = + -> XYCalculator +xyCalculator cfg = let elevationRange = cfg.maxElevation - cfg.minElevation @@ -309,21 +309,33 @@ accumulatePoints cfg = svgWidthPerDistanceUnit = cfg.svgWidth / cfg.maxDistance + in + XYCalculator + (\distance -> + String.fromFloat (distance * svgWidthPerDistanceUnit) + ) + (\elevation -> + String.fromFloat (cfg.svgHeight - cfg.svgHeight * normaliseElevation elevation) + ) - calculatePointX = - \distance -> - String.fromFloat (distance * svgWidthPerDistanceUnit) - calculatePointY = - \elevation -> String.fromFloat (cfg.svgHeight - cfg.svgHeight * normaliseElevation elevation) - in + +-- accumulatePoints takes in a config to form an accumulator. +-- The accumulator will take a point and form a tuple of (maybePrevPoint, currentLines), where each line in currentLines +-- is a formed Svg.Svg msg. +-- The maybePrevPoint is a Maybe (String, String) because the number values have been converted in a prior iteration of +-- the accumulation loop. + + +accumulatePoints : XYCalculator -> (ElevationPoint -> ( Maybe ( String, String ), List (Svg.Svg msg) ) -> ( Maybe ( String, String ), List (Svg.Svg msg) )) +accumulatePoints calc = \point ( maybePrevPoint, currLines ) -> let pointX = - calculatePointX point.distance + calc.x point.distance pointY = - calculatePointY point.elevation + calc.y point.elevation in case maybePrevPoint of Nothing ->