Skip to content

Instantly share code, notes, and snippets.

@saeedvir
Created December 5, 2024 04:28
Show Gist options
  • Save saeedvir/76d415f365f6befdd164bcce8093b8af to your computer and use it in GitHub Desktop.
Save saeedvir/76d415f365f6befdd164bcce8093b8af to your computer and use it in GitHub Desktop.
how with php Get the density of points in the coordinate plane?
<?php
$points = [
[3, 4], [7, 9], [12, 15], // Coordinates
[18, 5], [20, 25]
];
$grid_size = 10;
$grid_density = [];
foreach ($points as $point) {
$x = floor($point[0] / $grid_size);
$y = floor($point[1] / $grid_size);
$grid_key = "$x,$y";
if (!isset($grid_density[$grid_key])) {
$grid_density[$grid_key] = 0;
}
$grid_density[$grid_key]++;
}
foreach ($grid_density as $grid => $count) {
echo "Grid $grid: $count points\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment