From 790a69252958f653ded0840d7e26b3a9bcf4ade3 Mon Sep 17 00:00:00 2001 From: evlist Date: Tue, 9 May 2023 09:59:10 +0200 Subject: [PATCH] Averaging heights --- src/lib/height.ts | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/lib/height.ts b/src/lib/height.ts index fd056af..3ce6b8f 100644 --- a/src/lib/height.ts +++ b/src/lib/height.ts @@ -1,5 +1,6 @@ export const getHeightStats = (wpts: Wpt[]) => { let previous: number | null = null, + previousAvg: number | null = null, min: number | null = null, max: number | null = null, positive: number = 0, @@ -7,7 +8,7 @@ export const getHeightStats = (wpts: Wpt[]) => { wpts.forEach((wpt) => { if (wpt.ele) { - const ele = Math.round(wpt.ele); + const ele = 10 * Math.round(wpt.ele / 10); if (!max || ele > max) { max = ele; } @@ -15,11 +16,15 @@ export const getHeightStats = (wpts: Wpt[]) => { min = ele; } if (previous) { - if (ele > previous) { - positive += ele - previous; - } else { - negative += previous - ele; + const avg = (ele + previous) / 2; + if (previousAvg) { + if (avg > previousAvg) { + positive += avg - previousAvg; + } else { + negative += avg - previousAvg; + } } + previousAvg = avg; } previous = ele; }