我不熟悉解决这个问题的软件库。但如果你在 2D 空间中讨论,那么我想到了一些数学:
您可以使用此计算找到 2D 空间中任意 2 个点的距离:
距离 = sqrt( (X2 - X1)^2 + (Y2 - Y1 )^2 )
其中 ^2 表示由 2 供电。
假设你有一个 Point 对象数组(这里我为 Points 定义了一个简单的类),这样你就可以找出哪些点是相邻的:
class Point {
protected $_x = 0;
protected $_y = 0;
public function __construct($x,$y) {
$this->_x = $x;
$this->_y = $y;
}
public function getX() {
return $this->_x;
}
public function getY() {
return $this->_y;
}
public function getDistanceFrom($x,$y) {
$distance = sqrt( pow($x - $this->_x , 2) + pow($y - $this->_y , 2) );
return $distance;
}
public function isCloseTo($point=null,$threshold=10) {
$distance = $this->getDistanceFrom($point->getX(), $point->getY() );
if ( abs($distance) <= $threshold ) return true;
return false;
}
public function addNeighbor($point) {
array_push($this->_neighbors,$point);
return count($this->_neighbors);
}
public function getNeighbors() {
return $this->_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->isCloseTo($point,$threshold) {
$myPoint->addNeighbor($point);
}
}
$nearbyPointsList = $myPoint->getNeighbors();
编辑:对不起,我忘记了线性距离公式。 X 和 Y 轴距离值都应该以 2 为幂,然后它们的总和的 sqrt 就是结果。代码现已更正。