Php 将中点添加到数组中,并在下一次迭代中使用它们
这听起来很基本,但我无法理解这一点:Php 将中点添加到数组中,并在下一次迭代中使用它们,php,algorithm,loops,gd,Php,Algorithm,Loops,Gd,这听起来很基本,但我无法理解这一点: 我有两个点代表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('Cont
我有两个点代表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;
}
$iterations=5;
$noise=10;
$points=数组(“0:512”、“1023:512”);
$cit=0;
函数除法($pointArray){
全局$noise、$cit、$iterations;
$arrayLength=sizeof($pointArray);
$tempArray=$pointArray;
对于($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;
$MIDHEIGH=($currentPoint[“Y”]+$nextPoint[“Y”])/2)+兰德(0,$noise);
$npoint=“$midpoint:$midheight”;
阵列拼接($tempArray,$i+$i,0,$npoint);
$cit++;
如果($cit<$iterations){
除法($tempArray);
}否则{
返回$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).
}
}
上面是逻辑的主要“工作马”,本质上,它的任务是生成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”属性本质上说明了要执行中间点生成的次数。我对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”属性本质上说明了要执行中点生成的次数。主要问题是,在执行过程中可能会出现错误或警告,这些错误或警告将作为图像的一部分输出,从而使图像无效 因此,在开发过程中,您应该删除
标题
语句,以确保发现所有错误消息。事实上,当我使用您的代码执行此操作时,我得到:
E\u已弃用:类型8192——函数拆分()已弃用
因此,您首先需要解决这个问题。当我们谈到代码时,我建议您不要使用“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);
?>
主要问题是,在执行过程中可能会出现错误或警告,这些错误或警告将作为映像的一部分输出,从而使映像无效
因此,在开发过程中,您应该删除标题
语句,以确保发现所有错误消息。事实上,当我使用您的代码执行此操作时,我得到:
E\u已弃用:类型8192——函数拆分()已弃用
因此,您首先需要解决这个问题。当我们谈到代码时,我建议您不要使用“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);
?>
13是如何排在第一位的iteration@siddheshy值从10开始,一个随机值被添加到iTunes递归中以获得所需的值result@siddhesh在这种情况下,递归比循环有什么好处?这样会减少混淆…13是如何首先出现的iteration@siddheshy值从10开始,为随机值ue被添加到iTunes递归中以实现所需的result@siddhesh在这种情况下,递归与循环相比有什么好处?会有更少的混乱…是的,很抱歉我花了一段时间才响应。没问题。快乐编码:)是的,很抱歉我花了一段时间才响应。没问题。快乐编码:)