Hello There, Creating Squares Trying To Understand The Code!

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
Scissel
Posts: 13
Joined: June 3rd, 2012, 4:32 pm

Hello There, Creating Squares Trying To Understand The Code!

Post by Scissel » June 3rd, 2012, 4:45 pm

[Lesson 10] (Near The Start Of The Video)

Hello, I'm trying to understand the code and how it specifically draws the shape of the square, I get the method that's used to make the square, (I think) the idea being to draw lines vertically beneath each other to create the square, I just don't understand the code could someone explain exactly what is happening in this small piece of code to create the square.

Thank You!


void Game::ComposeFrame()
{
int BoxY = 100;
while( BoxY < 104 )
{
int BoxX = 100;
while( BoxX < 104 )
{
gfx.PutPixel( BoxX,BoxY,255,255,255);
BoxX++;
}
BoxY++;
}
}

User avatar
LuX
Posts: 1492
Joined: April 22nd, 2012, 12:33 pm
Location: Finland

Re: Hello There, Creating Squares Trying To Understand The C

Post by LuX » June 3rd, 2012, 5:02 pm

It's pretty simple actually:

Code: Select all

int BoxX = 100;	
while( BoxX < 104 )
{
    BoxX++;
}
So what happens here? We first declare a variable, that can be any number, as "100" then we enter our loop. while (BoxX < 104) is what is says, as long as the specified variable is less that 104 it will do what ever is inside it, inside the {}. As you can see inside we have BoxX++; which will add one digit to it each time it passes it, without it, it would be an infinite loop as BoxX would stay at the same value all the time. As soon as BoxX becomes 105 it will simply ignore the "while" loop, since BoxX is not less than 104 anymore, and it passes on.

Same happens with the BoxY, but since BoxY is a loop and inside the loop we have another loop, it will loop the BoxX for each BoxY value, meaning that we scroll now through every BoxY and BoxX value between the specified number. In other words, we enter the height, BoxY, and scroll though each BoxX value on that height, then simply add +1 to Y and move to the next row of X values.

Look again inside the BoxX loop. We have a PutPixel command inside. What happens is, that we scroll through this loop and each time before going to the start of the loop, we draw a pixel.
And so the loop goes through every possible value and puts a pixel there untill the loops have been gone through, before it continues to the next piece of code.

Hope this helps.
ʕ •ᴥ•ʔ

Scissel
Posts: 13
Joined: June 3rd, 2012, 4:32 pm

Re: Hello There, Creating Squares Trying To Understand The C

Post by Scissel » June 4th, 2012, 1:16 pm

Oh thank you, I think understand it now it can appear really convoluted from the format, and I lose my train of though thanks again for taking your time out!

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Hello There, Creating Squares Trying To Understand The C

Post by chili » June 4th, 2012, 3:11 pm

LuX to the rescue! :lol:
Chili

Post Reply