Intervals in a for loop

C++, C#, Java, PHP, ect...
Post Reply
User avatar
vitinho444
Posts: 2819
Joined: Mon Mar 21, 2011 4:54 pm

Intervals in a for loop

Post by vitinho444 »

Hey guys, im doing my final project for the Programming Introduction course at college, and i know its like cheating, but i don't want full code just a english explanation on how to do a interval in for loop.

Here's the situation:
I have a image (using a class already made) and i need to add to that image a film effect, that adds 2 black bars on each side of the image, and white squares on it, like this:
Image

But in vertical, so i made a 2 for loops that will create the black bars (painting 1 black pixel from top to bottom) and its working 100%.

Now the white squares, what i did was something like this:

Code: Select all

for(int x = 7; x < black_bar_length - 7; x++)
{
      for(int y = 10; y < img.getHeight(); y++)
      {
              if(y <= 35)
                   //Paint white dot at (x,y)
              else if(y > 45 && y <= 70)
                   //Paint white dot at (x,y)
       }
}
So this is the example for 1 square and the rest all white ofc.

What you may have noticed is that i start at the y=10 (give a little margin) and then go till y = 35 so the square is 25x25.
What i want to do is some other (automatic) way to fill those white squares instead of write down EVERY single interval like i did till i reached 300px, but if the image is longer than that.. no white squares after 300px :P

Can you guys just point me in the right direction?
Thank you ;)
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
User avatar
Xaos
Posts: 940
Joined: Wed Jan 11, 2012 4:01 am

Re: Intervals in a for loop

Post by Xaos »

You can either have two variables, say W and A. W is the variable in the loop getting added to, and A is another variable. Once W reaches 35, paint the square, set it to 0, add one to A, and keep going until the squares are painted. Or you could use modulo (%) to return a remainder and whenever the remainder of i divided by 35 is 0, paint a square.
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: Intervals in a for loop

Post by Xaleph »

Exactly what Xaos is proposing. You can use the modulo operator to check the remainder of 35. If it reaches 0, you need to do the paint.

for(int x = 7; x < black_bar_length - 7; x++)
{
for(int y = 10; y < img.getHeight(); y++)
{
if(y % 35 == 0)
// paint
}
}
User avatar
a_bertrand
Posts: 1536
Joined: Mon Feb 25, 2013 1:46 pm

Re: Intervals in a for loop

Post by a_bertrand »

Pseudo code:

Code: Select all

// Left black bar
FillRect(0,0,ImageWidth/10,ImageHeight,Black);
// Left right bar
FillRect(ImageWidth*9/10,0,ImageWidth/10,ImageHeight,Black);
for(var i=0;i < 5;i++)
{
// Left white area
FillRect(ImageWidth/20,ImageHeight*i*2/10,ImageWidth*1.8/20,ImageHeight/20);
// Right white area
FillRect(ImageWidth*18/20,ImageHeight*i*2/10,ImageWidth*1.8/20,ImageHeight/20);
}
You will have to tweak a bit those values, but certainly don't need 2 loops...
Creator of Dot World Maker
Mad programmer and annoying composer
User avatar
vitinho444
Posts: 2819
Joined: Mon Mar 21, 2011 4:54 pm

Re: Intervals in a for loop

Post by vitinho444 »

Edit:
I did it, sort of.

I focused on the matter for one square only.
So i did 2 basic for loops for the x and y of that square.

Than before that i did another for loop so it draws multiple squares :)

Before all that i did:
int square_numbers = img.getWidth() / 25; //Square width and height is 25px

Then in the for loop:

Code: Select all


for(int i = 0; i < squares + 1; i++)
//draw one square code adding a (i*20) padding so it separates each one.
the "squares + 1" is because i wanna do a square even if it cant be completed, so it gives a nice smooth effect :)

Thank you all for your help, love you guys.
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
Post Reply

Return to “Coding”