Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    Regular Coder
    Join Date
    May 2003
    Location
    34° 54' N 82° 13' W
    Posts
    996
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Using for(); in GD to achieve repeating windows

    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 Code:
    <?php

        Header
    ("Content-Type: image/png");
        
        
    $image = array(1135688);
        
        
    $img ImageCreate($image[0], $image[1]) or die("Image WIDTHxHEIGHT could not be created.");
        
    $colors = array(    'white' => imagecolorallocate($img255255255),
                    
    'black' => imagecolorallocate($img000),
                    
    'red' => imagecolorallocate($img25500),
                    
    'yellow' => imagecolorallocate($img2552550),
                    
    'green' => imagecolorallocate($img01280),
                    
    'blue' => imagecolorallocate($img0128255),
                    
    'purple' => imagecolorallocate($img1280128),
                    
    'skyblue' => imagecolorallocate($img128191255),
                    
    'brown' => imagecolorallocate($img984949),
                    
    'grey' => imagecolorallocate($img128128128),
                    
    'lightgrey' => imagecolorallocate($img192192192),
                    
    'lightbrown' => imagecolorallocate($img128640));
                    
    /////////////// SKY - BACKGROUND COLOR /////////////////////////////////////////////////////////////////////////////////////////////////////////////

        
    ImageFill($img00$colors['skyblue']); 
        
    /////////////// IMAGE BORDER ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        
    ImageRectangle($img001134687$colors['black']);
        
    /////////////// SUN //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        
    Imagefilledellipse($img8080150150$colors['yellow']);
        
    /////////////// GRASS ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        
    ImageFilledEllipse($img5007001000100$colors['green']);
        
    /////////////// BAWLS BUILDING //////////////////////////////////////////////////////////////////////////////////////////////////////////

        
    ImageFilledRectangle($img50175200686$colors['lightgrey']);
        
    ImageRectangle($img50175200688$colors['black']);
                
                
    ImageFilledRectangle($img6022585250$colors['blue']); /* WINDOW */
                
    ImageRectangle($img6022585250$colors['black']);
        
            
    ImageString($img575180" B A W L S "$colors['brown']);
            
    ImageString($img576181" B A W L S  "$colors['brown']);
            
            
    ImageString($img558195" A M E R I C A "$colors['brown']);
            
    ImageString($img559196" A M E R I C A "$colors['brown']);
            
        
    ImageFilledRectangle($img400175100100$colors['yellow']);
        
    /////////////// RASMUS HOUSE //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            
        
    ImageFilledRectangle($img408550620650$colors['lightbrown']);
        
    ImageRectangle($img408550620650$colors['brown']);
        
            
    ImageString($img5445550"THE RASMUS HOUSE"$colors['white']);
            
    ImageString($img2480561"Fine Dining"$colors['white']);
            
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

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

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

    Thanks to all
    Stevie Peele
    Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
    #dev - any programming,etc. question
    #design - design discussion and critque
    #central - general chat
    Come join us!

  • #2
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    PHP Code:
    $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 */ 
        
    }

    Last edited by raf; 05-25-2004 at 01:24 AM.
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #3
    Regular Coder
    Join Date
    May 2003
    Location
    34° 54' N 82° 13' W
    Posts
    996
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hmm. That did not seem to work.
    Stevie Peele
    Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
    #dev - any programming,etc. question
    #design - design discussion and critque
    #central - general chat
    Come join us!

  • #4
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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>
    Last edited by raf; 05-25-2004 at 12:23 AM.
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #5
    Regular Coder
    Join Date
    May 2003
    Location
    34° 54' N 82° 13' W
    Posts
    996
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well, it displays no error, just nothing where the windows should be.
    Stevie Peele
    Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
    #dev - any programming,etc. question
    #design - design discussion and critque
    #central - general chat
    Come join us!

  • #6
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    man, o man. 2 error in a snippet of 3 lines ...
    x+side needed to be x+$side, of course
    PHP Code:
    $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 */  
        


    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #7
    Regular Coder
    Join Date
    May 2003
    Location
    34° 54' N 82° 13' W
    Posts
    996
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    Stevie Peele
    Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
    #dev - any programming,etc. question
    #design - design discussion and critque
    #central - general chat
    Come join us!

  • #8
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    PHP Code:
    $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 */   
        
    }  

    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #9
    Regular Coder
    Join Date
    May 2003
    Location
    34° 54' N 82° 13' W
    Posts
    996
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks.

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

    I appreciate all your help.

    Regards,

    Stevie
    Stevie Peele
    Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
    #dev - any programming,etc. question
    #design - design discussion and critque
    #central - general chat
    Come join us!

  • #10
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #11
    Regular Coder
    Join Date
    Jul 2002
    Posts
    698
    Thanks
    0
    Thanked 0 Times in 0 Posts
    you sure you can write that , after 150 ?

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

  • #12
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Garadon
    you sure you can write that , after 150 ?

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

    Not sure if the parser will like it though (what a desaster thread : 3 errors + having my coordinates upside down)
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •