Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Create Staircase with Nested Loops

nested loop staircase

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1 StainedSilva

StainedSilva

    CC Lurker

  • New Member
  • Pip
  • 6 posts

Posted 12 March 2015 - 04:16 AM

Hi there,

 

I have just finished a chapter on loops and have encountered one Im battling to do..create a staircase using code.

 

I need to create blocks that add onto the end of each other while moving down one level. So the first level with be one block, second level 2 block etc, until the 6th level has 6 blocks..

 

I managed to get the first block, but it doesn't create any more, Im having a hard time figuring out why ! Please help!

 

 

 public partial class Form1 : Form

    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            for (int i = 1; i <= 6; i++)
            {
                for (int j = 1; j < 6; j++)
                {
                    Graphics paper;
                    paper = pictureBox1.CreateGraphics();
                    paper.Clear(Color.White);
                    Pen myPen = new Pen(Color.Black);
 
                    paper.DrawRectangle(myPen, i, j, 10, 10);
                }
            }
            }
        }
    }


#2 lespauled

lespauled

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1360 posts

Posted 12 March 2015 - 06:49 AM

Without compiling and testing it for myself, I think it is, in fact, writing all of the squares, but you're not seeing them.

 

Just trace through your code, you will see that for each instance, you are clearing the picturebox, thus erasing everything that you did prior to that call.

 

First thing you should do is put all the declarations BEFORE the loops.

 

But even after that, you will not get the result you're expecting, because they 2nd and 3rd parameters are the X and Y coordinates of the top left corner of the rectangle to be drawn.  Since you are increment them by 1, you wont see a step, but more likely a thick line.  You have to do some multiplication to get what you're expecting.

 

Hint: Declare a variable with the width, like:

 

int dimension = 10;

 

and set both loops from 0 to 5


My Blog: http://forum.codecal...699-blog-77241/
"Women and Music: I'm always amazed by other people's choices." - David Lee Roth

#3 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts

Posted 14 March 2015 - 12:46 AM

Adding to lespauled comment, this is the initialization he was talking about:

 

    private void pictureBox1_Click(object sender, EventArgs e)
        {
 

                    Graphics paper;                   
                    paper = pictureBox1.CreateGraphics();
 
                    paper.Clear(Color.White);
                    Pen myPen = new Pen(Color.Black);
 

            for (int i = 1; i <= 6; i++)
            {
                for (int j = 1; j < 6; j++)
                {
                    paper.DrawRectangle(myPen, i, j, 10, 10);
                }
            }
            }

 

Now, what do you do with that graphic? Because you also need to refresh the picturebox image if you want to see the results.



#4 StainedSilva

StainedSilva

    CC Lurker

  • New Member
  • Pip
  • 6 posts

Posted 14 March 2015 - 04:10 AM

Oh okay thanks for clearing that up for me. Im trying to make a staircase made of blocks. 6 rows by six rows. First row has one block the second under that has two and so on. Unfortunately I just get the one block.
Oh okay thanks for clearing that up for me. Im trying to make a staircase made of blocks. 6 rows by six rows. First row has one block the second under that has two and so on. Unfortunately I just get the one block.

#5 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts

Posted 15 March 2015 - 12:58 AM

Oh, you might be accomplishing what you want, just not seeing it because every block starts just 1 pixel from the other, so, it's like they are all the same, try something like this:

 

paper.DrawRectangle(myPen, (i*20), (j*20), 10, 10);

 

That will be good for showing you what the program is doing. Then you will have to adjust the distance until you like the result. And of course, there's still the centering to deal with.



#6 lespauled

lespauled

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1360 posts

Posted 16 March 2015 - 10:44 AM

To be honest, I don't really see a need for the second loop.  The width calc can be done using the outer loop.


My Blog: http://forum.codecal...699-blog-77241/
"Women and Music: I'm always amazed by other people's choices." - David Lee Roth

#7 StainedSilva

StainedSilva

    CC Lurker

  • New Member
  • Pip
  • 6 posts

Posted 16 March 2015 - 08:25 PM

So I changed the code as Blackrabbit showed and I still only get one block.. I also dont think I need a nested loop for this an maybe only need one loop..

#8 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts

Posted 18 March 2015 - 02:35 AM

This is a working proof of concept working code:

 

            Graphics paper;
            paper = pictureBox1.CreateGraphics();

            paper.Clear(Color.White);
            Pen myPen = new Pen(Color.Black);
            
            for (int i = 0; i <3; i++)
            {
                paper.DrawRectangle(myPen, 100 + (i * 20), i * 20, 10, 10);
                paper.DrawRectangle(myPen, 100 - (i * 20), i * 20, 10, 10);
            }

 

That makes a stair of the kind you use to reach the lightbulb. It's up to you to learn how the match works and make the one you want.






Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download