FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Programs
(Moderators: fry, REAS)
   Help with modulo???
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: Help with modulo???  (Read 638 times)
dabomb


Help with modulo???
« on: Sep 1st, 2004, 11:56pm »

hi everyone....
 
I'm trying to write a program that uses loops to create a repetitive pattern, then assigns the color white or black to the fill, depending on position. The goal is to create a checkerboard.  I can get the loops to work, and I know I need to use "modulo" to assign the color, and somehow assign a remainder of "1" black and "0" white, or vice versa.  I need to establish if the remainder is even or odd. I"m not sure how to do this.  I'm pretty new to programming in general, and new to processing.  
 
This is what I have thus far:
 
size (400, 400);
 
for(int i=0; i<=400; i=i+20) {
    for(int j=0; j<=400; j=j+20)  
    {  
     if(i%40==2) {  
    fill(0);      
 } else  {  
    fill(255);
    }  
    rect (i, j, 20, 20);
    }
}
 
I know this program will only result in white squares with black strokes. I have been messing with the "if/else" loop and can get it to make alternating lines black, but  not checker board.  Can someone point out what I am missing?
 
Best,
DaBomb
 
Euskadi


Re: Help with modulo???
« Reply #1 on: Sep 2nd, 2004, 4:23am »

Try this... only one small change... I'll let you find it.
 
size (400, 400);  
 
for(int i=0; i<=400; i=i+20) {  
    for(int j=0; j<=400; j=j+20)  
    {  
     if((i+j)%40==0) {  
    fill(0);  
 } else  {    
    fill(255);  
    }  
    rect (i, j, 20, 20);  
    }  
}  
 
 
Pages: 1 

« Previous topic | Next topic »