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
Switch to threaded view of this topic Create a new topic Submit Reply
having trouble using gd imageline in foreach loop [message #172803] Thu, 03 March 2011 00:08 Go to next message
Lwangaman is currently offline  Lwangaman
Messages: 4
Registered: March 2011
Karma: 0
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
Re: having trouble using gd imageline in foreach loop [message #172804 is a reply to message #172803] Thu, 03 March 2011 01:12 Go to previous messageGo to next message
spambait is currently offline  spambait
Messages: 35
Registered: September 2010
Karma: 0
Member
In article <296f0959-18d8-4ec5-a4b8-094172863787(at)glegroupsg2000goo(dot)googlegroups(dot)com>, comp(dot)lang(dot)php(at)googlegroups(dot)com wrote:
> 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.

Yeah, I bet you are.
>
> Here is my code:
[...]

And here is your bug.
> if($direction=="north"){ $x2 = $x1; $y2 = bcsub($y1,$fibvalue); $direction="west"; }

If $direction was "north" before executing that statement, what value does it
have now? And what will happen when you execute the next statement?

> 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"; }

No matter whether $direction was "north", "south", "east", or "west" *before*
executing this block of four statements, it's guaranteed to be "north" *after*
executing them.
Re: having trouble using gd imageline in foreach loop [message #172805 is a reply to message #172803] Thu, 03 March 2011 02:00 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On 03/03/11 00:08, Lwangaman wrote:

> $direction = "north";

// start, direction is north

> foreach($fibsequence as $fibvalue){

// direction is north, so match this if clause

> if($direction=="north"){ $x2 = $x1; $y2 = bcsub($y1,$fibvalue); $direction="west"; }

// now direction is west, so natch this if clause

> if($direction=="west"){ $x2 = bcsub($x1,$fibvalue); $y2 = $y1; $direction="south"; }

// now direction is south, so match this if clause

> if($direction=="south"){ $x2 = $x1; $y2 = bcadd($y1,$fibvalue); $direction="east"; }

// now direction is east, so match this if clause

> if($direction=="east"){ $x2 = bcadd($x1,$fibvalue); $y2 = $y1; $direction="north"; }

// now direction is north, and we're going to start the loop again

> }

Your if tree should use else if (or switch / case, but that's another
argument):

if ($dir == "n") { /* do the north line, set $dir to w */ }
else if ($dir == "w") { /* do the west line, set $dir to s */ }
else if ($dir == "s") { /* do the south line, set $dir to e */ }
else if ($dir == "e") { /* do the east line, set $dir to n */ }

Rgds

Denis McMahon
Re: having trouble using gd imageline in foreach loop [message #172806 is a reply to message #172803] Thu, 03 March 2011 02:51 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On 03/03/11 00:08, Lwangaman wrote:

Oh yes, and:

> $fibsequence=[1,1,2,3,5,8,13,21,34,55,89,144,233,377,610];

should be either:

$fibsequence = array(1,1,2,3,5,8,13,21,34,55,89,144,233,377,610);

or:

$fibsequence[]=1;
$fibsequence[]=1;
$fibsequence[]=2;
$fibsequence[]=3;
$fibsequence[]=5;
$fibsequence[]=8;
$fibsequence[]=13;
$fibsequence[]=21;
$fibsequence[]=34;
$fibsequence[]=55;
$fibsequence[]=55;
$fibsequence[]=89;
$fibsequence[]=144;
$fibsequence[]=233;
$fibsequence[]=377;
$fibsequence[]=610;

see http://uk2.php.net/manual/en/language.types.array.php

Rgds

Denis McMahon
Re: having trouble using gd imageline in foreach loop [message #172808 is a reply to message #172806] Thu, 03 March 2011 10:48 Go to previous messageGo to next message
Lwangaman is currently offline  Lwangaman
Messages: 4
Registered: March 2011
Karma: 0
Junior Member
hahaha ok I get it! ELSEIF. Thanks.
Re: having trouble using gd imageline in foreach loop [message #172856 is a reply to message #172806] Sat, 05 March 2011 19:17 Go to previous message
Lwangaman is currently offline  Lwangaman
Messages: 4
Registered: March 2011
Karma: 0
Junior Member
> Oh yes, and:

>> $fibsequence=[1,1,2,3,5,8,13,21,34,55,89,144,233,377,610];
> should be either:...

Yes in fact I was not using that in my code, I made this mistake in making the example... It should in fact be "$fibsequence = array(1,1,2,3...)" but I am building the array automatically with a fibonacci generator function that I wrote.
  Switch to threaded view of this topic Create a new topic Submit Reply
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: Wed Dec 04 08:51:04 GMT 2024

Total time taken to generate the page: 0.01980 seconds