Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'm not familiar with software libraries for this problem. but if you are talking in 2D space, then here is some math that comes to my mind:</p> <p>you can find the distance of any 2 points in the 2D space using this calculation:</p> <p>distance = sqrt( (X2 - X1)^2 + (Y2 - Y1 )^2 )</p> <p>inwhich ^2 means powered by 2.</p> <p>so le'ts say you have an array of Point objects (here I define a simple class for Points), this way you can find out which points are neighbored:</p> <pre><code>class Point { protected $_x = 0; protected $_y = 0; public function __construct($x,$y) { $this-&gt;_x = $x; $this-&gt;_y = $y; } public function getX() { return $this-&gt;_x; } public function getY() { return $this-&gt;_y; } public function getDistanceFrom($x,$y) { $distance = sqrt( pow($x - $this-&gt;_x , 2) + pow($y - $this-&gt;_y , 2) ); return $distance; } public function isCloseTo($point=null,$threshold=10) { $distance = $this-&gt;getDistanceFrom($point-&gt;getX(), $point-&gt;getY() ); if ( abs($distance) &lt;= $threshold ) return true; return false; } public function addNeighbor($point) { array_push($this-&gt;_neighbors,$point); return count($this-&gt;_neighbors); } public function getNeighbors() { return $this-&gt;_neighors; } } $threshold = 100; // the threshold that if 2 points are closer than it, they are called "close" in our application $pointList = array(); /* * here you populate your point objects into the $pointList array. */ // you have your coordinates, right? $myPoint = new Point($myXCoordinate, $myYCoordinate); foreach ($pointList as $point) { if ($myPoint-&gt;isCloseTo($point,$threshold) { $myPoint-&gt;addNeighbor($point); } } $nearbyPointsList = $myPoint-&gt;getNeighbors(); </code></pre> <p>edit: I'm sorry, I had forgotten the linear distance formula. both X and Y axis distance values should be powered by 2 and then the sqrt of their sum is the result. the code is now corrected.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload