Skip to content

Commit

Permalink
Better distribution around the globe
Browse files Browse the repository at this point in the history
  • Loading branch information
inxilpro committed Nov 18, 2024
1 parent 799f467 commit b0c4203
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
19 changes: 13 additions & 6 deletions app/Http/Controllers/World/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Models\ExternalGroup;
use App\Models\Group;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;

class HomeController
Expand All @@ -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()),
),
]);
}
Expand Down Expand Up @@ -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);
Expand Down
9 changes: 8 additions & 1 deletion resources/js/globe.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit b0c4203

Please sign in to comment.