Created
December 5, 2024 04:28
-
-
Save saeedvir/76d415f365f6befdd164bcce8093b8af to your computer and use it in GitHub Desktop.
how with php Get the density of points in the coordinate plane?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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