...

View Full Version : Using for(); in GD to achieve repeating windows



SDP2006
05-24-2004, 10:45 PM
So, for my summer bored day project (which will be about everyday), i decided to make a 'PHP City' with GD - lol.

Anyways, im having some trouble. I am trying to accomplish 'windows.' I have on my building one window, but it would be pointless to do 50 different functions over and over to make the 50 windows. I know there is, but I don't know how, to do it with a for(); loop. If someone could help, I would appreciate it!

Code -
<?php

Header("Content-Type: image/png");

$image = array(1135, 688);

$img = ImageCreate($image[0], $image[1]) or die("Image WIDTHxHEIGHT could not be created.");
$colors = array( 'white' => imagecolorallocate($img, 255, 255, 255),
'black' => imagecolorallocate($img, 0, 0, 0),
'red' => imagecolorallocate($img, 255, 0, 0),
'yellow' => imagecolorallocate($img, 255, 255, 0),
'green' => imagecolorallocate($img, 0, 128, 0),
'blue' => imagecolorallocate($img, 0, 128, 255),
'purple' => imagecolorallocate($img, 128, 0, 128),
'skyblue' => imagecolorallocate($img, 128, 191, 255),
'brown' => imagecolorallocate($img, 98, 49, 49),
'grey' => imagecolorallocate($img, 128, 128, 128),
'lightgrey' => imagecolorallocate($img, 192, 192, 192),
'lightbrown' => imagecolorallocate($img, 128, 64, 0));

/////////////// SKY - BACKGROUND COLOR /////////////////////////////////////////////////////////////////////////////////////////////////////////////

ImageFill($img, 0, 0, $colors['skyblue']);

/////////////// IMAGE BORDER ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

ImageRectangle($img, 0, 0, 1134, 687, $colors['black']);

/////////////// SUN //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Imagefilledellipse($img, 80, 80, 150, 150, $colors['yellow']);

/////////////// GRASS ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

ImageFilledEllipse($img, 500, 700, 1000, 100, $colors['green']);

/////////////// BAWLS BUILDING //////////////////////////////////////////////////////////////////////////////////////////////////////////

ImageFilledRectangle($img, 50, 175, 200, 686, $colors['lightgrey']);
ImageRectangle($img, 50, 175, 200, 688, $colors['black']);

ImageFilledRectangle($img, 60, 225, 85, 250, $colors['blue']); /* WINDOW */
ImageRectangle($img, 60, 225, 85, 250, $colors['black']);

ImageString($img, 5, 75, 180, " B A W L S ", $colors['brown']);
ImageString($img, 5, 76, 181, " B A W L S ", $colors['brown']);

ImageString($img, 5, 58, 195, " A M E R I C A ", $colors['brown']);
ImageString($img, 5, 59, 196, " A M E R I C A ", $colors['brown']);

ImageFilledRectangle($img, 400, 175, 100, 100, $colors['yellow']);

/////////////// RASMUS HOUSE //////////////////////////////////////////////////////////////////////////////////////////////////////////////////

ImageFilledRectangle($img, 408, 550, 620, 650, $colors['lightbrown']);
ImageRectangle($img, 408, 550, 620, 650, $colors['brown']);

ImageString($img, 5, 445, 550, "THE RASMUS HOUSE", $colors['white']);
ImageString($img, 2, 480, 561, "Fine Dining", $colors['white']);

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

ImagePNG($img);
Imagedestroy($img);

?>

On web http://stevie.f2o.org/city.php

Thanks to all
:D

raf
05-25-2004, 12:11 AM
hmm. you'll need a nested for()-loop.

Your first loop will be the rows. (new row of windows, so manipulating the y-coordinat and setting the x back to the startingpoint)
The nested loop, will be the columns (manipulating the x-co÷rdinate).

something like


$side=25;
for ($y=250; $y>= 35; $y -=30){ // so your top row will be at 250 and each row, you'll start 30 lower, leaving a gap of 5 between the rows.
for ($x=60; $x<=120; $x +=30){ // so each window has a gap of 5 on the side as well
ImageFilledRectangle($img, $x, ($y-$side), ($x+side), $y, $colors['blue']); /* WINDOW */
}
}

SDP2006
05-25-2004, 12:19 AM
Hmm. That did not seem to work. :o

raf
05-25-2004, 12:20 AM
Can you be a bit more specific?

<edit>
also, inside the second loop, $x>=120 shold of course be $x<=120
(sloppy copy - paste)
with the current code, the second loop will never start so nothing would happen.
</edit>

SDP2006
05-25-2004, 01:14 AM
Well, it displays no error, just nothing where the windows should be. :o

raf
05-25-2004, 01:30 AM
man, o man. 2 error in a snippet of 3 lines ...
x+side needed to be x+$side, of course


$side=25;
for ($y=250; $y>= 35; $y -=30){ // so your top row will be at 250 and each row, you'll start 30 lower, leaving a gap of 5 between the rows.
for ($x=60; $x<=120; $x +=30){ // so each window has a gap of 5 on the side as well
ImageFilledRectangle($img, $x, ($y-$side), ($x+$side), $y, $colors['blue']); /* WINDOW */
}
}

SDP2006
05-25-2004, 01:42 AM
Thanks so much, raf. <3

http://stevie.f2o.org/city.php

You can see they're not line up. I've tried to line them up but can't seem to do it. Can you help?

For(); loops are rather new to me. So, i am sorry about seeming so 'newbie' about it.

Thanks

raf
05-25-2004, 01:51 AM
hmm. been lately doing a lot of dynamic PDF creation and there the 0,0 is the botom left corner. For GD its the top left corner, so that's why the windows are going 'up' instead of 'down'.

You'll need 4 columns and about 10 rows. So your looking at something like


$side=25;
for ($y=200; $y<= 500; $y +=30){ // so your top row will be at 200 and each row, you'll start 30 lower, leaving a gap of 5 between the rows.
for ($x=60; $x<=150; $x +=30){ // so each window has a gap of 5 on the side as well
ImageFilledRectangle($img, $x, ($y-$side), ($x+$side), $y, $colors['blue']); /* WINDOW */
}
}

SDP2006
05-25-2004, 03:11 AM
Thanks.

Do you know of any resources to where I can master loops?

I appreciate all your help.

Regards,

Stevie

raf
05-25-2004, 08:27 AM
What's wrong with the manual?

All loopstructures are basically very simple.the tricky part is that they are kinda compressed. In VBscript, you'd write

x=60
do while x <= 150
your code
x = x + 30
loop

and in PHP, this become
for ($x=60; $x<= 150,; $x +=30){}


Spo it's just a matter of transfering your logical structure into the right function.

Garadon
05-25-2004, 10:40 AM
you sure you can write that , after 150 :p?

for ($x=60; $x<= 150,; $x +=30){}

raf
05-25-2004, 12:54 PM
you sure you can write that , after 150 :p?

for ($x=60; $x<= 150,; $x +=30){}
Just did. See,
for ($x=60; $x<= 150,; $x +=30){}
did it again :D

Not sure if the parser will like it though (what a desaster thread : 3 errors + having my coordinates upside down)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum