将中点添加到数组中,并在下一次迭代中使用它们


Add midpoints into array and use them in the next iteration

这听起来很基本,但我无法理解:
我有两个点表示X:Y坐标
在第一次迭代中,我想把两者的中点加到数组中
在下一次迭代中,我想把第一个和第二个的中点,然后把第二个和第三个的中点加到数组中。。。等等…
(Im将随机量添加到y轴以进行中点位移)例如
0:10,10:10
第一次迭代
0:10、5:13、10:10
第二次迭代
0:10、2.5:12、5:13、7.5:15、10:10
等等。

这是我在尝试至少让一些东西发挥作用后得到的代码:

<?php
 header('Content-type: image/png');
  $png_image = imagecreate(1024, 1024);
  imagecolorallocate($png_image, 15, 142, 210);
  $black = imagecolorallocate($png_image, 0, 0, 0);
    imagesetthickness($png_image, 10);
$iterations = 5;
$noise = 10;
$points = array("0:512","1023:512");
for($iteration=0; $iteration < $iterations; $iteration++){
        $new_array = array();
        ksort($points);
    for($i = 0; $i < sizeof($points)-1 ;$i++){
        $previous = array("X" => split(":", $points[$i])[0], "Y" => split(":", $points[$i])[1] );
        $next  = array("X" => split(":", $points[$i+1])[0], "Y" => split(":", $points[$i+1])[1] );
        $midpoint = ($previous["X"] + $next["X"])/2;
        $midheight = (($previous["Y"] + $next["Y"])/2)+rand(0,$noise);
        $npoint="$midpoint:$midheight";

        array_push($new_array, $npoint);
    }
    $points =  array_merge($points, $new_array);
}

ksort($points);
for($i=0;$i < sizeof($points)-1;$i++){
        $previous = array("X" => split(":", $points[$i])[0], "Y" => split(":", $points[$i])[1] );
        $current  = array("X" => split(":", $points[$i+1])[0], "Y" => split(":", $points[$i+1])[1] );
     // imageline($png_image, (int)$previous["X"], (int)$previous["Y"], (int)$current["X"], (int)$current["Y"], $black);
    // imagefilledellipse ( $png_image ,(int)$previous["X"], (int)$previous["Y"] , 8, 8 , $black );
    imagestring ($png_image , 4 , (int)$previous["X"], (int)$previous["Y"] , $i , $black);
}

 imagepng($png_image);
 imagedestroy($png_image);
?>

出于某种原因,它有时会在相同的x上添加点,但y值不同。

编辑:尝试使用功能

$iterations = 5;
$noise = 10;
$points = array("0:512","1023:512");
$cit = 0;
function divide($pointArray){
    global $noise, $cit, $iterations;  
    $arrayLength = sizeof($pointArray);
    $tempArray = $pointArray;
    for($i = 0; $i < $arrayLength-1 ;$i++){
        $currentPoint = array("X" => split(":", $pointArray[$i])[0], "Y" => split(":", $pointArray[$i])[1] );
        $nextPoint  = array("X" => split(":", $pointArray[$i+1])[0], "Y" => split(":", $pointArray[$i+1])[1] );
        $midpoint = ($currentPoint["X"] + $nextPoint["X"])/2;
        $midheight = (($currentPoint["Y"] + $nextPoint["Y"])/2)+rand(0,$noise);
        $npoint="$midpoint:$midheight";
        array_splice( $tempArray, $i+$i, 0, $npoint );
        $cit++;
        if($cit < $iterations){
            divide($tempArray);
        }else{
            return $tempArray;
        }

主要问题是在执行过程中可能会出现错误或警告,这些错误或警告将作为图像的一部分输出,这将使图像无效。

因此,在开发时,应该删除header语句,以确保发现所有错误消息。事实上,当我用你的代码这样做时,我得到:

E_DEPRECATED:类型8192--函数split()已弃用

所以,你首先需要解决这个问题。当我们谈到该代码时,我建议您不要使用"X:Y"格式:以这种方式存储坐标是非常低效的。在每次迭代中,您都要将该字符串拆包到X、Y坐标中,然后将其转换回字符串。为什么不从一开始就将数组值存储在X、Y数值坐标中?

这是建议的代码:

<?php
$png_image = imagecreate(1024, 1024);
imagecolorallocate($png_image, 15, 142, 210);
$black = imagecolorallocate($png_image, 0, 0, 0);
imagesetthickness($png_image, 10);
$iterations = 5;
$noise = 10;
// Don't use string format "X:Y" for doing manipulations. 
// If you still need that format afterwards, do that conversion later.
$points = array(
    array(
        "X" => 0,
        "Y" => 512
    ),
    array(
        "X" => 1023,
        "Y" => 512
    )
);
for($iteration=0; $iteration < $iterations; $iteration++){
    $new_array = array($points[0]);
    for($i = 0; $i < sizeof($points)-1; $i++){
        $previous = $points[$i];
        $next  = $points[$i+1];
        $midpoint = array(
            "X" => ($previous["X"] + $next["X"])/2,
            "Y" => ($previous["Y"] + $next["Y"])/2 + rand(0,$noise)
        );
        array_push($new_array, $midpoint);
        array_push($new_array, $next);
    }
    $points =  $new_array;
}
for($i=0; $i < sizeof($points)-1; $i++){
    $previous = $points[$i];
    $next  = $points[$i+1];
    imagestring ($png_image , 4 , (int)$previous["X"], (int)$previous["Y"] , $i , $black);
}
// Put header statement in comments for as long as you have errors:
header('Content-type: image/png');
imagepng($png_image);
imagedestroy($png_image);
?>

我对php了解不多,但让我尝试帮助了解一般逻辑。代码示例是javascript,但我可以尝试解释一下。

var midPointDisplace = function(pt1, pt2, depth, accum){
    // Create the mid point.
    var pt3 = [(pt1[0] + pt2[0]) / 2 + (Math.random()-0.5), (pt1[1] + pt2[1]) / 2 + (Math.random()-0.5)]; 
    if (depth == 0){
        // if the recursion depth reaches 0. Push the midpoint in to the accumulator array.
        accum.push(pt3);
    }else{
        // Otherwise, recurse down.
        midPointDisplace(pt1, pt3, depth - 1, accum); // Ask the next depth in the recursion to generate the points between pt1 (previous point) and pt3 (current mid point).
        accum.push(pt3); // Push/append the current midpoint
        midPointDisplace(pt3, pt2, depth - 1, accum); // Ask the next depth in the recursion to generate the points between pt3 and pt2 (the next point).
    }
}

上面是逻辑的主要"主力",本质上,它的任务是生成pt1和pt2之间的所有中点,并在必要时递归。上面的循环可以初始化如下。

var initPoint = function(pt1, pt2, depth, accum){
    accum.push(pt1); // Push the first point.
    midPointDisplace(pt1, pt2 ,depth, accum); // Generate all the mid points.
    accum.push(pt2); // Push the last point.
}

应该将"accum"属性作为引用传递,以允许递归中的每个步骤"构建它"。函数返回后,accum将包含您想要的内容。

"depth"属性本质上说明了要执行中点生成的次数。