将中点添加到数组中并在下一次迭代中使用它们
Add midpoints into array and use them in the next iteration
这听起来很基础,但我无法理解:
我有两个点代表 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;
}
我对 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).
}
}
以上是主要的"workhorse"逻辑,本质上就是生成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" 属性 应作为引用传递,以允许递归到 "build it up" 的每个步骤。在函数return之后,accum会包含你想要的。
"depth" 属性 实质上说明了您想要生成中点的次数。
主要问题是您可能会在执行过程中出现错误或警告,这些错误或警告将作为图像的一部分输出,这将使您的图像无效。
因此在开发时,您应该删除 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);
?>
这听起来很基础,但我无法理解:
我有两个点代表 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;
}
我对 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).
}
}
以上是主要的"workhorse"逻辑,本质上就是生成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" 属性 应作为引用传递,以允许递归到 "build it up" 的每个步骤。在函数return之后,accum会包含你想要的。
"depth" 属性 实质上说明了您想要生成中点的次数。
主要问题是您可能会在执行过程中出现错误或警告,这些错误或警告将作为图像的一部分输出,这将使您的图像无效。
因此在开发时,您应该删除 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);
?>