From b0c420333f0e9d2c6d0a119319c989f34e7787c4 Mon Sep 17 00:00:00 2001 From: Chris Morrell Date: Mon, 18 Nov 2024 11:23:00 -0500 Subject: [PATCH] Better distribution around the globe --- app/Http/Controllers/World/HomeController.php | 19 +++++++++++++------ resources/js/globe.js | 9 ++++++++- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/app/Http/Controllers/World/HomeController.php b/app/Http/Controllers/World/HomeController.php index 2d729db..1cf6ebc 100644 --- a/app/Http/Controllers/World/HomeController.php +++ b/app/Http/Controllers/World/HomeController.php @@ -4,6 +4,7 @@ use App\Models\ExternalGroup; use App\Models\Group; +use Illuminate\Support\Collection; use Illuminate\Support\Facades\Cache; class HomeController @@ -14,7 +15,7 @@ public function __invoke() 'points' => Cache::remember( key: 'homepage-points', ttl: now()->addDay(), - callback: fn() => $this->points()->shuffle(), // $this->maximizeDistance($this->points()) + callback: fn() => $this->maximizeDistance($this->points()), ), ]); } @@ -56,16 +57,22 @@ protected function distance($point1, $point2) return $earth * $c; } - function maximizeDistance($points) + function maximizeDistance(Collection $points) { $result = collect([$points->first()]); $remaining = $points->slice(1); while ($remaining->isNotEmpty()) { - $lastPoint = $result->last(); - - // Find the point that's furthest from the last added point - $furthest = $remaining->sortByDesc(fn($point) => $this->distance($lastPoint, $point))->first(); + $furthest = $remaining->sortByDesc(function($point) use ($result) { + $targets = $result->take(-2); + + $distance = $this->distance($targets->pop(), $point); + if ($targets->isNotEmpty()) { + $distance += $this->distance($targets->pop(), $point); + } + + return $distance; + })->first(); $result->push($furthest); $remaining = $remaining->filter(fn($point) => $point !== $furthest); diff --git a/resources/js/globe.js b/resources/js/globe.js index 5f9dcb9..0e08baf 100644 --- a/resources/js/globe.js +++ b/resources/js/globe.js @@ -68,9 +68,16 @@ function ease(k) { } function interpolate(from, to, tick, ticks) { + let diff = to - from; + + if (Math.abs(diff) > 180) { + diff = diff > 0 ? diff - 360 : diff + 360; + } + const k = Math.max(0, Math.min(1, tick / (ticks - 1))); const easing = ease(k); - return from + (to - from) * easing; + + return from + diff * easing; } const SECONDS_BETWEEN_POINTS = 4;