Loading...
Logo
Processing Forum


I've been trying to make a grid using the line() as aposed to using rect() which i see mostly:

the thing which is baffling me is why this code doesn't work at all... it just gives a blank screen :



void setup(){

size(400,400);

smooth();

}



void draw(){

for(int i = 4; i > height; i++){


line( i, 0, i, height);


}


}


this obviously only gives the vertical lines... it looks like it should work, i can't figure out why it doesn't. I'm probably missing something obvious but i just can't see it. 



if someone can shed some light on this I would appreciate it.

Replies(3)

this seems to work fine:

int w,h;

void setup(){
  size(400,400);
  smooth();
}


void draw(){
 
  w = width;
  h = height;
  for  (int x=0;x<w;x+=15){
    line(x,0,x,h);

    line(0,x,w,x);

  }

  
}

maybe the for loop just doesn't like the width going directly in there? i don't really no i'm still confused.
" for(int i = 4; i > height; i++){"

How does a for loop works?
First, the first part, init, is run.
Secondly, the second part, test, is checked. If false, the loop is exited immediately. If true, the content of the loop is run.
Last, after executing the content of the loop, if any, the last part is run. Usually it is a simple increment/decrement, but it can be more complex.

So what we have in your case? i is created and set to 4. Then the test is checked: i isn't greater than the height, so the loop is exited immediately, nothing is drawn.

The correct code would be, as you discovered: for (int i = 4; i < height; i += step){
(step is some variable containing the interval you want between the lines, the grid granularity)
Thanks for the response, what a silly mistake on my part I feel like a right idiot!!