gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:55:55 27 4
gpt4 key购买 nike

这听起来很基础,但我无法理解:
我有两个点代表 X:Y 坐标
在第一次迭代中,我想将两者的中点添加到数组中...
在下一次迭代中,我想将第一个和第二个的中点以及第二个和第三个之间的中点添加到数组中……依此类推……
(我向 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 : type 8192 -- Function split() is deprecated

因此,您首先需要解决该问题。当我们谈到该代码时,我建议您不要使用“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 - 将中点添加到数组中并在下一次迭代中使用它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37140910/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com