We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Find my error, the prof is stumped.
Page Index Toggle Pages: 1
Find my error, the prof is stumped. (Read 787 times)
Find my error, the prof is stumped.
Jan 12th, 2010, 8:15am
 
Just starting to learn processing in Uni. Was trying to compact some lines in to a 'for' statement.



These:
Code:
//Set background color and smooth lines
size(250,250);
smooth();

//Set stroke color to white and create strokes
line(245,0,245,250);
line(240,10,235,250);
line(235,20,225,250);
line(230,30,215,250);
line(225,40,205,250);
line(220,50,195,250);
line(215,60,185,250);
line(210,70,175,250);
line(205,80,165,250);
line(200,90,155,250);
line(195,100,145,250);
line(190,110,135,250);
line(185,120,125,250);
line(180,130,115,250);
line(175,140,105,250);
line(170,150,95,250);
line(165,160,85,250);
line(160,170,75,250);



Into this:
Code:
size(250,250);
smooth();

for(int i = 245; i > 160; i -= 5){
 for(int o = 0; o < 170; o += 10){
   for(int p = 245; p > 75; p -= 10){

line(i, o, p, 250);
   }
 }
}



Prof couldn't help me out, hopefully you can, I just want to create the results of the first code using multiple 'for' statements that are applied to a 'line' statement.

If you preview the first lines of code you will see the outcome I am looking for.

Re: Find my error, the prof is stumped.
Reply #1 - Jan 12th, 2010, 8:39am
 
Wow... not sure if I can help because this seems like a fundamental (ie basic) programming issue. But since you seem to be in a class at a university I'll urge you to stick with it.

With that said you seem deeply confused about what nested for-loops do. Your desired output is:
line(245,0,245,250);
line(240,10,235,250);
line(235,20,225,250);
line(230,30,215,250);

Note that for EACH LINE
x1 drops by 5
y1 increments by 10
x2 drops by 10
y2 stays fixed
This is the standard behavior you want for a SINGLE for-loop.

Your example has 18 line statements so a loop like this should work:
for(i=0;i<18;i++) {
  x1 = 245 - 5*i;
  y1 = 0 + 10*i;
  x2 = 245 - 10*i;
  y2 = 250;
  line(x1,y1,x2,y2);
}

I'll leave it to you and your professor to figure out why/when you would want to use nested for-loops.
Re: Find my error, the prof is stumped.
Reply #2 - Jan 12th, 2010, 8:44am
 
Yes, the logic isn't bad but you do too much loops! Smiley
You can use the increment/decrement operators within a single loop:
Code:
int x1 = 245;
int y1 = 0;
int x2 = 245;
int y2 = 250;
for (int i = 0; i < 18; i++)
{
 line(x1, y1, x2, y2);
 x1 -= 5;
 y1 += 10;
 x2 -= 10;
 // y2 unchanged
}


[EDIT] Ah, JM Davis was faster than me! Smiley
You get two slightly different solutions for the same price! Wink
Re: Find my error, the prof is stumped.
Reply #3 - Jan 12th, 2010, 9:10am
 
Thanks guys, I used this in the end:

Code:
size(250,250);
smooth();

for(int i=0;i<18;i++) {
int q = 245 - 5*i;
int w = 0 + 10*i;
int e = 245 - 10*i;
int r = 250;
line(q,w,e,r);
}


Ended up working quite well, and it should come in handy for my next assignment. I think the way we are learning is strangely simplified so that we don't exactly understand what each function does, we are only taught how to apply it to certain tasks. What you have showed me will definitely help, thanks again.
Re: Find my error, the prof is stumped.
Reply #4 - Jan 12th, 2010, 9:49am
 
General advice: try and use meaningful names for your variables...
We used x and y because they are coordinates. Your qwerty scheme is funny but will get in the way when you will read code later, as it doesn't help understanding it.
I know from experience that naming variables can be hard for a newbie (I was one, a long time ago... Smiley), but it is worth thinking a bit about it.

An exception is i, j, k used for loop indexes, but that's a cultural, historical exception: in Fortran, variables starting with letters from i to n are integer by default, other variables are real... Cheesy
Re: Find my error, the prof is stumped.
Reply #5 - Jan 12th, 2010, 10:46am
 
Thanks for the tips. I am in a design program at Uni so I doubt processing will focused on much, but I learned a bit of VB back in high school and I enjoy coding so I'll probably pursue it on my own time.
Page Index Toggle Pages: 1