  | 
    
 
  
    
    
      
        
          
         Author | 
        
         Topic: Langtons ant and setPixel() problems...  (Read 498 times) | 
       
     
     | 
   
 
  
    
    
      
        
        
          
            
            gkoshra 
 
                  
      
             | 
            
            
            
              
                
                Langtons ant and setPixel() problems... 
                «  on: Feb 20th, 2003, 3:03pm » | 
                
                
 
                 | 
               
             
             
            
            Hi, i'm pretty new to programming, most of the stuff i've done is fairly basic actionscripting, but i'm pretty keen to learn proce55ing as it seems like it's going to solve a lot of the issues I have with flash.      So, I thought i'd start simple and make a program to show this -> http://mathworld.wolfram.com/LangtonsAnt.html      It's a really simple concept, so I bashed together this code (which is horrible and inelegant I know, but I thought I could refine it into functions etc once it works)....         int antx = 200;   int anty = 200;   int direction = 0;   int wb = 0;      void setup()   {     size(400, 400);     framerate(10);     background(#FFFFFF);   }      void loop()   {     //  color col = color(wb,wb,wb);     //  setPixel(antx,anty,col);        if(getPixel(antx,anty)==(-1)){       //do stuff because it's a white pixel       direction--;       if(direction<0)       direction=3;       wb=0;     }        if(getPixel(antx,anty)!=(-1)){       //do stuff because it's a black pixel       direction++;       if(direction>3)       direction=0;       wb=255;        }        if((direction)==0)     anty--;     if((direction)==1)     antx++;     if((direction)==2)     anty++;     if((direction)==3)     antx--;        color col = color(wb,wb,wb);     point(antx,anty);      }               I've scouted about this forum and the reference docs to try and figure out what i'm doing wrong, and have come to the conclusion I need to have all the pixels in an array, is this right? If so, how do I make it work?      Thanks.   
            
             | 
           
            | 
            
            
            
             | 
           
         
         | 
       
     
     | 
   
 
  
    
    
      
        
        
          
            
            gkoshra 
 
                  
      
             | 
            
            
            
              
                
                Re: Langtons ant and setPixel() problems... 
                « Reply #1 on: Feb 20th, 2003, 3:35pm » | 
                
                
 
                 | 
               
             
             
            
            I've just spotted a mistake, the last line is supposed to read...      setPixel(antx,anty,col);            
            
             | 
           
            | 
            
            
            
             | 
           
         
         | 
       
     
     | 
   
 
  
    
    
      
        
        
          
            
            REAS 
 
 
        
      
             | 
            
            
            
              
                
                Re: Langtons ant and setPixel() problems... 
                « Reply #2 on: Feb 20th, 2003, 4:49pm » | 
                
                
 
                 | 
               
             
             
            
            // color col = color(wb,wb,wb);    // setPixel(antx,anty, col);       you can also use:        stroke(wb);   point(antx, anty); 
            
             | 
           
            | 
            
            
            
             | 
           
         
         | 
       
     
     | 
   
 
  
    
    
      
        
        
          
            
            gkoshra 
 
                  
      
             | 
            
            
            
              
                
                Re: Langtons ant and setPixel() problems... 
                « Reply #3 on: Feb 20th, 2003, 5:56pm » | 
                
                
 
                 | 
               
             
             
            
            Cheers for the info. I think that the problem lies with the rest of my program rather than just setting the pixels now, as even when I use point() to plot my colour, nothing still happens apart from a blob going round and round in circles.      *** thinks for a while ***      I think i've finally clicked that the reason I only have one blob at a time on screen is that I don't ever put the positions of the other blobs into an array to keep them there, is this correct? 
            
             | 
           
            | 
            
            
            
             | 
           
         
         | 
       
     
     | 
   
 
  
    
    
      
        
        
          
            
            gkoshra 
 
                  
      
             | 
            
            
            
              
                
                Re: Langtons ant and setPixel() problems... 
                « Reply #4 on: Feb 21st, 2003, 3:09pm » | 
                
                
 
                 | 
               
             
             
            
            Me again, I finally figured out the problem (with the help of a programmer friend!) was that I was moving before I set the pixel I had left, so here's the new revised (and pretty much completely changed) code...      int antx = 100;   int anty = 100;   int direction = 0;   boolean wb = false;   boolean[][] mp = new boolean[200][200];   boolean wbset = false;      void setup()   {     size(200, 200);     background(#FFFFFF);     for (int x = 0; x < width; x++)     for (int y = 0; y < height; y++){       mp[x][y] = false;     }   }      void checkAnt (int x, int y, int col){        if(col==-1){       direction--;       if(direction<0)    direction=3;       wb=true;     }        if(col!=-1){       direction++;       if(direction>3)    direction=0;      wb=false;     }            mySetPixel(antx,anty,wb);          if((direction)==0)     anty--;     if((direction)==1)     antx++;     if((direction)==2)     anty++;     if((direction)==3)     antx--;             }         void refresh() {     for (int x = 0; x < width; x++)     for (int y = 0; y < height; y++)     setPixel(x, y, mp[x][y] ? color(0,0,0) : color(255,255,255));      }         void mySetPixel(int x, int y, boolean set)   {     mp[x][y] = set;     setPixel(x, y, set ? color(0,0,0) : color(255,255,255));   }         boolean myGetPixel(int x, int y)   {     return mp[x][y];   }         void loop()   {     refresh();     checkAnt(antx,anty,getPixel(antx,anty));      }   
            
             | 
           
            | 
            
            
            
             | 
           
         
         | 
       
     
     | 
   
 
  
    
    
      
        
        
          
            
            gkoshra 
 
                  
      
             | 
            
            
            
              
                
                I know it seems like i'm talking to myself but.... 
                « Reply #5 on: Feb 24th, 2003, 9:54am » | 
                
                
 
                 | 
               
             
             
            
            I realised that if you use noBackground(); then the screen doesn't get cleared every frame and I don't need to put all the pixels into an array and redraw it every frame! Anyway, it's not very exciting, but I did some nicer things which i'll post somewhere else.            int antx = 100;   int anty = 100;   int direction = 0;   boolean wb = false;      void setup()   {     size(200, 200);     noBackground();   }      void checkAnt (int x, int y, int col){        if(antx>199){     antx=0;     }     if(antx<0){     antx=199;     }          if(anty>199){     anty=0;     }     if(anty<0){     anty=199;     }          if(col==-1){       direction--;       if(direction<0)    direction=3;       wb=true;     }        if(col!=-1){       direction++;       if(direction>3)    direction=0;      wb=false;     }           mySetPixel(antx,anty,wb);            if((direction)==0)     anty--;     if((direction)==1)     antx++;     if((direction)==2)     anty++;     if((direction)==3)     antx--;        }         void mySetPixel(int x, int y, boolean set)   {     setPixel(x, y, set ? color(0,0,0) : color(255,255,255));   }            void loop()   {     checkAnt(antx,anty,getPixel(antx,anty));   }   
            
             | 
           
            | 
            
            
            
             | 
           
         
         | 
       
     
     | 
   
 
 
 |