我正在研究这个解决方案 - PHP Find Coordinates between two points
我尝试了代码并且有效。虽然代码得到了对角线的坐标列表。
$p1 = array(
'x' => 0,
'y' => 0
);
$p2 = array(
'x' => 10,
'y' => 10
);
// Work out distances
$pxd = $p2['x'] - $p1['x'];
$pyd = $p2['y'] - $p1['y'];
// Find out steps
$steps = max($p1['x'], $p1['y'], $p2['x'], $p2['y']);
$coords = array();
for ($i = 0; $i < $steps; ++ $i) {
$coords[] = array(
'x' => round($p1['x'] += $pxd / $steps),
'y' => round($p1['y'] += $pyd / $steps)
);
}
print_r($coords);
在上面的代码中,一个得到10个坐标,即
Array ( [0] => Array ( [x] => 1 [y] => 1 ) [1] => Array ( [x] => 2 [y] => 2 ) [2] => Array ( [x] => 3 [y] => 3 ) [3] => Array ( [x] => 4 [y] => 4 ) [4] => Array ( [x] => 5 [y] => 5 ) [5] => Array ( [x] => 6 [y] => 6 ) [6] => Array ( [x] => 7 [y] => 7 ) [7] => Array ( [x] => 8 [y] => 8 ) [8] => Array ( [x] => 9 [y] => 9 ) [9] => Array ( [x] => 10 [y] => 10 ) )
以上是对角线。
我想获得100个坐标,即(1,1),(1,2),(1,3),(1,4)......(7,10),( 8,10),)(9,10),(10,10)。
我不确定如何以数学方式解决这个问题。
答案 0 :(得分:0)
你需要两个循环;一个用于&#39; x&#39;轴,一个用于&#39; y&#39;轴
更像是(未经测试的):
for ($x = $p1['x']; $x < $p2['x']; ++ $x) {
for ($y = $p1['y']; $y < $p2['y']; ++ $y) {
$coords[] = array(
'x' => round($x),
'y' => round($y)
);
}
}
答案 1 :(得分:0)
你应该使用嵌套for循环。
for($i=1;$i<=10;$i++){
for($j=1;$j<=10;$j++){
$coords[] = array('x'=>$i,'y'=>$j);
}
}