PHP增量使用0.01

时间:2015-01-05 10:49:10

标签: php

我有以下代码

$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
for ($x = $p1['x']; $x < $p2['x']; ++ $x) {
    for ($y = $p1['y']; $y < $p2['y']; ++ $y) {
        $coords['gps'] = array(
           'x' => $x,
           'y' => $y
        );
 echo $coords['gps']['x'] .', '.$coords['gps']['y'] . '</br>';
        }

}

以上在所有坐标中增加1。对于0,0到10,10的范围,我想增加0.001而不是1。 0.001,0.02,0.03,0.04等......

当我被困住时,我该怎么做呢

3 个答案:

答案 0 :(得分:0)

试试这个

// Find out steps
for ($x = $p1['x']; $x < $p2['x']; $x += 0.001) {
    for ($y = $p1['y']; $y < $p2['y']; $y += 0.001) {
        $coords['gps'] = array(
           'x' => $x,
           'y' => $y
        );
        echo $coords['gps']['x'] .', '.$coords['gps']['y'] . '</br>';
    }
}

答案 1 :(得分:0)

将增量步长从1更改为0.001:

for ($x = $p1['x']; $x < $p2['x']; $x += 0.001) {
    for ($y = $p1['y']; $y < $p2['y']; $y += 0.001) {
        // do your stuff here

答案 2 :(得分:0)

这应该适合你:

(但这段时间需要很长时间)

<?php

    $p1 = array(
        'x' => 0,
        'y' => 0
    );

    $p2 = array(
        'x' => 10,
        'y' => 10
    );

    $interval = 0.001;


    for ($count = $p1['x']; $count < $p2['x']; $count+= $interval) {
        for ($innerCount = $p1['y']; $innerCount < $p2['y']; $innerCount+= $interval) {
            $coords['gps']['x'] = $count;
            $coords['gps']['y'] = $innerCount;
            echo sprintf("%6.3f", $coords['gps']['x']) . ", " . sprintf("%6.3f", $coords['gps']['y']) . "</br>";
        }

    }

?>

输出:

0.000, 0.000
0.000, 0.001
0.000, 0.002
0.000, 0.003
0.000, 0.004
0.000, 0.005
0.000, 0.006
0.000, 0.007
0.000, 0.008
0.000, 0.009
0.000, 0.010
//...