From 860da5f151357831db92a2be64c2edb829b9a953 Mon Sep 17 00:00:00 2001 From: Aki Date: Tue, 6 Apr 2021 23:48:02 +0200 Subject: Refactored center calculation --- derelict.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/derelict.js b/derelict.js index 739bbfa..573e7f9 100644 --- a/derelict.js +++ b/derelict.js @@ -102,12 +102,14 @@ function splitKillmails(clusters, killmail) { return clusters } +function averagePosition(positions) { + const sum = positions.reduce((sum, pos) => sum.add(pos), new THREE.Vector3()) + return sum.divideScalar(positions.length) +} + function processKillmails(grid, killmails, icon) { let clusters = killmails.reduce(splitKillmails, new Array()) - let center = new THREE.Vector3() - let positions = clusters[0].map(km => km.victim.position) - positions.forEach(pos => center.add(new THREE.Vector3(pos.x, pos.y, pos.z))) - center.divideScalar(positions.length) + let center = averagePosition(clusters[0].map(km => km.victim.position)) clusters[0].sort((a, b) => a.killmail_time.localeCompare(b.killmail_time)) let elements = [] clusters[0].forEach(killmail => { -- cgit v1.1