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
   Syntax
(Moderators: fry, REAS)
   sexy syntax
« Previous topic | Next topic »

Pages: 1 2 
   Author  Topic: sexy syntax  (Read 1102 times)
Koenie

170825270170825270koeniedesign WWW Email
Re: sexy syntax
« Reply #15 on: Jun 19th, 2004, 2:57pm »

Hey Mario,
 
Nice recode, but it could be even better Check this out:
Code:
int w[],m,c;
int[] col={#202000,#FF0000,#FF6600,#FFBB00,#FFFF00};
void setup(){
  size(500,300);
  w=new int[m=width*height];
  for(int i=-1;++i<(c=1);w[(int)random(m)]=1)
  background(32,32,0);
}
void loop(){
  for(int i=0;++i<m;)
  if(w[i]>0&&!(w[i]==4&&w[(i-1+m)%m]==4&&w[(i+1)%m]==4&&w[(i+width)%m]==4&&w[(i-width+m)%m]==4)){
    int r=(i+(int)random(3)-1+((int)random(3)-1)*width+m)%m;
    if(w[r]<4){c++;pixels[r]=col[++w[r]];}
  }if(c>=m*4)setup();
}

Pretty sexy, eh?
 
By the way, if you run this piece of code, you'll notice the 'blob' grows faster in southern direction than in northern direction, anyone have a clue why?
 
Koenie
« Last Edit: Jun 19th, 2004, 3:00pm by Koenie »  

http://koeniedesign.com
mflux

fluxhavoc WWW
Re: sexy syntax
« Reply #16 on: Jun 19th, 2004, 9:31pm »

Hey Koenie
that sounds like maybe your ints are rounding/truncating numbers...
 
mattgilbert

tangramkid WWW Email
Re: linked lists (this is way off topic &#8211; so
« Reply #17 on: Jun 19th, 2004, 10:42pm »

thanks mflux for the linked list stuff.
 
This is getting way off topic (very sorry), especially since this code of mine is not the least bit sexy, but i was playing around with linked lists and made this little sketch that lets you see the performance difference between linked lists and arrays in this certain situation. If anyone's curious (this is probably old hat for many of you), run this in Processing and you'll get the debug messages showing the difference in speed. It's not fast enough to notice just by watching it (only around 2-3% improvement for me). I'm sure other applications would benefit much more from linked lists.
 
Code:

//
// An illustration of the performance difference between  
// operating on an array and operating on a linked list.
//
// Not that this sort of pixel manipulation is what you  
// would use a linked list for, but just as an example.
//
// We're actually comparing  
// *2 array calls* against  
// *1 array call and 1 step in the list*
// since both modes access pixels[]
//  
// For me, the linked list part is something like 2-3% faster.
//
// Matt Gilbert June 19, 2004
//
 
 
// the first element the linked list
PixObject listStarter;
 
// the array
PixObject[] poArray;
 
int millisecondsStart;
boolean doList;
 
void setup() {  
  size(100, 100);
  //make the linked list
  listStarter = new PixObject(0);
  PixObject currentPO = listStarter;
  for(int i = 1; i < width*height; i++){
    PixObject newPO = new PixObject(0);
    currentPO.next = newPO;
    currentPO = currentPO.next;
  }
  //make the array
  poArray = new PixObject[width*height];
  for(int i = 0; i < poArray.length; i++){
    poArray[i] = new PixObject(0);
  }
}  
 
void loop() {  
 
  if(doList){ //see the list in action
    PixObject currentPO = listStarter;
    for(int i = 0; i < pixels.length; i++){
 currentPO.c = (currentPO.c + 1) % 255;
 pixels[i] = currentPO.c;
 currentPO = currentPO.next;
    }
    if(listStarter.c == 0) millisecondsStart = millis();
    if(listStarter.c == 254) println(millis() - millisecondsStart);
  }  
   
  else { //see the array in action
    for(int i = 0; i < pixels.length; i++){
 poArray[i].c = (poArray[i].c + 1) % 255;
 pixels[i] = poArray[i].c;
    }
    if(poArray[0].c == 0) millisecondsStart = millis();
    if(poArray[0].c == 254) println(millis() - millisecondsStart);
  }
}  
 
void keyPressed() {  
//toggle between using the list and using the array  
//by hitting any key.
  doList = !doList;  
  if(doList) println("now using the linked list");
  else println("now using the array");
}
 
 
class PixObject {
  int c;
  PixObject next;
  PixObject(int c){
    this.c = c;
  }
}

 
Quasimondo

WWW Email
Re: sexy syntax
« Reply #18 on: Jun 20th, 2004, 1:13am »

on Jun 19th, 2004, 2:57pm, Koenie wrote:
Pretty sexy, eh
 
By the way, if you run this piece of code, you'll notice the 'blob' grows faster in southern direction than in northern direction, anyone have a clue why
 
Koenie

 
Yeah now we're talking!
 
One reason why the growth is inconsitant is that I completely ignored that the left and right wrapping is creating a jump in the rows: if a pixels wraps from the right edge to to left it ends up one line above and in the other direction it end up one line below. Not sexy...
 
To fix this I think it would need some really unsexy conditionals. Maybe the two-dimensional arrays would come in handy here...
 

Mario Klingemann | Quasimondo | Incubator | côdeazur
Koenie

170825270170825270koeniedesign WWW Email
Re: sexy syntax
« Reply #19 on: Jun 20th, 2004, 9:20pm »

on Jun 20th, 2004, 1:13am, Quasimondo wrote:

 
Yeah now we're talking!
 
One reason why the growth is inconsitant is that I completely ignored that the left and right wrapping is creating a jump in the rows: if a pixels wraps from the right edge to to left it ends up one line above and in the other direction it end up one line below. Not sexy...
 
To fix this I think it would need some really unsexy conditionals. Maybe the two-dimensional arrays would come in handy here...

 
I don't think the screenwrapping is the problem with that, because I double checked it with the old two-dimensional array version, and it does the same thing, so there has to be another reason, maybe the thing mflux is saying. What do rounding/truncating numbers mean
 
Koenie
 

http://koeniedesign.com
Quasimondo

WWW Email
Re: sexy syntax
« Reply #20 on: Jun 20th, 2004, 9:59pm »

If screenwrapping is not the problem then maybe it's the random() function. Usually you would expect random(3) to return values up to 3 but never 3 itself. Unfortunately Processing sometimes returns 3. This could be the cause for the slight bias.
 

Mario Klingemann | Quasimondo | Incubator | côdeazur
Koenie

170825270170825270koeniedesign WWW Email
Re: sexy syntax
« Reply #21 on: Jun 21st, 2004, 10:56am »

I don't think that's the problem, because I tried Java's Math.random() too and it gives the same problem. If you run this code, you'll get a good view of the inconsistant growth:
Code:
int w[],m,c;
int[] col={#202000,#FF0000,#FF6600,#FFBB00,#FFFF00};
void setup(){
  size(500,300);
  w=new int[m=width*height];
  for(int i=-1;++i<(c=1);w[m/2+250]=1){}
  background(32,32,0);
}
void loop(){
  for(int i=0;++i<m;)
  if(w[i]>0&&!(w[i]==4&&w[(i-1+m)%m]==4&&w[(i+1)%m]==4&&w[(i+width)%m]==4&&w[(i-width+m)%m]==4)){
    int r=(i+(int)(Math.random()*3)-1+((int)(Math.random()*3)-1)*width+m)%m;
    if(w[r]<4){c++;pixels[r]=col[++w[r]];}
  }if(c>=m*4)setup();
   
  ellipseMode(CENTER_RADIUS); noFill();
  for(int i=0;i++<15;ellipse(width/2,height/2,i*20-10,i*20-10)){}
}

 
Koenie
 

http://koeniedesign.com
TomC

WWW
Re: sexy syntax
« Reply #22 on: Jun 21st, 2004, 11:34am »

It's because you're going forward through the array, so changes are propogating down.
 
You need a buffer, so that each state is calculated from the last state...  I called my buffer w2.
 
Code:

int w[],w2[],m,c;  
int[] col={#202000,#FF0000,#FF6600,#FFBB00,#FFFF00};  
void setup(){  
  size(500,300);  
  w=new int[m=width*height];  
  w2=new int[m];  
  for(int i=-1;++i<(c=1);w[m/2+250]=1){}  
  background(32,32,0);  
}  
void loop(){  
  System.arraycopy(w,0,w2,0,m);
  for(int i=0;++i<m;)  
  if(w2[i]>0&&!(w2[i]==4&&w2[(i-1+m)%m]==4&&w2[(i+1)%m]==4&&w2[(i+width)%m]==4&&w2[(i-width+m)%m]==4)){  
    int r=(i+(int)(Math.random()*3)-1+((int)(Math.random()*3)-1)*width+m)%m;  
    if(w[r]<4){c++;pixels[r]=col[++w[r]];}  
  }if(c>=m*4)setup();  
   
  ellipseMode(CENTER_RADIUS); noFill();  
  for(int i=0;i++<15;ellipse(width/2,height/2,i*20-10,i*20-10)){}  
}  

 
Incidentally, this code is totally unsexy to debug.  It doesn't go any slower if you put spaces in and use meaningful names!
 
My 2p towards what makes stuff sexy is parameterisation and generality.  Take this code as an example - what if I want a palette with more than 5 colours?  Couldn't the 4s that are scattered around be replaced with something like int p = col.length-1?  Then I can change the palette very simply, and make it as long as I like
 
Koenie

170825270170825270koeniedesign WWW Email
Re: sexy syntax
« Reply #23 on: Jun 21st, 2004, 3:39pm »

How is this:
Code:
int w[],w2[],m,c,s;
int[] col={#202000,#FF0000,#FF6600,#FFBB00,#FFFF00};
void setup(){
  size(500,300);
  w=new int[m=width*height];
  w2=new int[m];
  s=col.length-1;
  for(int i=-1;++i<(c=2);w[(int)random(m)]=1){}
  background(32,32,0);
}
void loop(){
  System.arraycopy(w,0,w2,0,m);
  for(int i=0;++i<m;)
  if(w2[i]>0&&!(w2[i]==s&&w[(i-1+m)%m]==s&&w2[(i+1)%m]==s&&w2[(i+width)%m]==s&&w2[(i-width+m)%m]==s)){
    int r=(i+(int)random(3)-1+((int)random(3)-1)*width+m)%m;
    if(w[r]<s){c++;pixels[r]=col[++w[r]];}
  }if(c>=m*s)setup();
}

 
This code has the ability to adapt to any color palette you give it, and thanks to TomC, it now grows consistent. Thanks Mario and TomC for helping me out with this code. Now let's continue this topic as it was started, about sexy code.
 
Koenie
 

http://koeniedesign.com
rgovostes

rgovostes
Re: sexy syntax
« Reply #24 on: Jun 21st, 2004, 11:14pm »

Doing some framerate comparisons, the latest edition of Growth gets from 85 to 65 fps.  The original (quoted here by Quasimondo) gets from 72 to 68 or so (these are, of course, affected by the printing of framerates and such).
 
Oh, and shouldn't
Code:
background(32,32,0);

be something like
Code:
background(red(col[0]), green(col[0]), blue(col[0]));

(or better yet, with bit operations and all). This would allow the palette to be customized more, correct?
« Last Edit: Jun 22nd, 2004, 12:26am by rgovostes »  
Koenie

170825270170825270koeniedesign WWW Email
Re: sexy syntax
« Reply #25 on: Jun 22nd, 2004, 9:03pm »

on Jun 21st, 2004, 11:14pm, rgovostes wrote:
Doing some framerate comparisons, the latest edition of Growth gets from 85 to 65 fps.  The original (quoted here by Quasimondo) gets from 72 to 68 or so (these are, of course, affected by the printing of framerates and such).
 
Oh, and shouldn't
Code:
background(32,32,0);

be something like
Code:
background(red(col[0]), green(col[0]), blue(col[0]));

(or better yet, with bit operations and all). This would allow the palette to be customized more, correct

 
Actually, the background() could be left out, because all the pixels are drawn anyway... I don't know why I left it in there, well whatever
 
Koenie
 

http://koeniedesign.com
Euskadi


Re: sexy syntax
« Reply #26 on: Jun 29th, 2004, 3:51am »

especially fun are linked lists (double-linked)....
so each object has a pointer to the thing in front of it and the thing behind it.
 
so, you can go forward through the list, or backwards through the list, or insert into the middle of the list, or remove items from the middle.
 
fun stuff. implemented in more up-to-date versions of java.... or, build your own.  i usually use the normal version of linked lists, with a few things added to allow inserts based on multiple keys. but anyway.
 
Pages: 1 2 

« Previous topic | Next topic »