FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » having trouble using gd imageline in foreach loop
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
having trouble using gd imageline in foreach loop [message #172803] Thu, 03 March 2011 00:08 Go to previous message
Lwangaman is currently offline  Lwangaman
Messages: 4
Registered: March 2011
Karma:
Junior Member
I am trying to take the first 15 numbers of the fibonacci sequence and use them to draw a squarish spiral.
I have an array with the first 15 numbers $fibsequence.
I am looping through this array so that a line correponding to the fibonacci value will be drawn. I am alternating "directions": north, south, east, west. I am starting from the center of the image.

For some reason I am only getting one straight line going east instead of the four alternating directions.

Here is my code:

<?php

$fibsequence=[1,1,2,3,5,8,13,21,34,55,89,144,233,377,610];
$fibseries = $fibsequence;
$imagewidth = array_pop($fibseries); // will be 610
$imageheight = array_pop($fibseries); // will be 377
$im = imagecreatetruecolor($imagewidth,$imageheight)
or die('Cannot Initialize new GD image stream');
$background_color = imagecolorallocate($im, 224, 234, 234);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagefill($im, 0, 0, $background_color);

$direction = "north";
$x1 = bcdiv($imagewidth,2); // image center on x axis
$y1 = bcdiv($imageheight,2); // image center on y axis
foreach($fibsequence as $fibvalue){
if($direction=="north"){ $x2 = $x1; $y2 = bcsub($y1,$fibvalue); $direction="west"; }
if($direction=="west"){ $x2 = bcsub($x1,$fibvalue); $y2 = $y1; $direction="south"; }
if($direction=="south"){ $x2 = $x1; $y2 = bcadd($y1,$fibvalue); $direction="east"; }
if($direction=="east"){ $x2 = bcadd($x1,$fibvalue); $y2 = $y1; $direction="north"; }
imageline($im,$x1,$y1,$x2,$y2,$text_color);
imagestring($im,1,$x2,$y2,$fibvalue,$text_color);
$x1 = $x2; // actual x ending point becomes next x starting point
$y1 = $y2; // actual y ending point becomes next y starting point
}
ob_start();
imagepng($im);
$fibspiral = ob_get_contents();
ob_end_clean();
echo "<img src='data:image/png;base64,".(base64_encode($fibspiral))."' />";
?>

How come I am getting a straight line? If I try doing 15 consecutive imageline()'s instead it works fine:

imageline($im,305,189,305,188,$text_color); // y2=y1-1
imageline($im,305,188,304,188,$text_color); // x2=x1-1
imageline($im,304,188,304,190,$text_color); // y2=y1+2
imageline($im,304,190,307,190,$text_color); // x2=x1+3
imageline($im,307,190,307,185,$text_color); // y2=y1-5
imageline($im,307,185,299,185,$text_color); // x2=x1-8
imageline($im,299,185,299,198,$text_color); // y2=y1+13
imageline($im,299,198,320,198,$text_color); // x2=x1+21
imageline($im,320,198,320,164,$text_color); // y2=y1-34
imageline($im,320,164,265,164,$text_color); // x2=x1-55
imageline($im,265,164,265,253,$text_color); // y2=y1+89
imageline($im,265,253,409,253,$text_color); // x2=x1+144
imageline($im,409,253,409,20,$text_color); // y2=y1-233
imageline($im,409,20,32,20,$text_color); // x2=x1-377
imageline($im,32,20,32,630,$text_color); // y2=y1+610
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: strange behaviour in assignment expr.
Next Topic: cookies and Firefox 3.6.15 - ugh
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Tue Nov 26 23:31:28 GMT 2024

Total time taken to generate the page: 0.03982 seconds