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.
IndexDiscussionExhibition › cool mistake
Page Index Toggle Pages: 1
cool mistake (Read 2150 times)
cool mistake
May 21st, 2009, 9:17am
 
I stumbled on the following when I accidentally put a straight integer instead of a color. I love how Processing works like that. Anyway, thought it was interesting enough to post. Quote:
int rad = 200; //radius

void setup() {
  size(400,400,P3D);
  noFill();
  background(0);
  loadPixels();
}
void draw() {
  for(int a=0; a<pixels.length; a++) {
    float d = dist(mouseX, mouseY, a%400, (a-a%400)/400);
    //move your mouse
    //a%400 x coord from index, (a-a%400)/400 y coord from index
    if(d <= rad) {
      if(brightness(pixels[a])<255)
      pixels[a] += lerpColor(pixels[a], color(255), 1-d/rad)<<17;
      //pixels[a] = lerpColor(pixels[a], color(255), 1-d/rad);
      //use commented line above instead for something more sane
    }
  }
  updatePixels();
}
void mouseClicked() {
  //click to clear
  for(int a=0; a<pixels.length; a++) {
    pixels[a] = 0;
  }
  updatePixels();
}

Re: cool mistake
Reply #1 - May 21st, 2009, 11:31am
 
I like playing arround with it. thats what i came up with Smiley

float rad = 100; //radius

void setup() {
 size(400,400,P3D);
 noFill();
 background(0);
 loadPixels();
}
void draw() {
 rad = 10+(frameCount%200);
 float x= width/2+sin(frameCount/3)*100;
 float y= height/2+cos(frameCount/3)*100;

 fill(0,20);
 rect(0,0,400,400);
 for(int a=0; a<pixels.length; a+=1) {
   float d = dist(x, y, a%400, (a-a%400)/400);


   if(d <= rad) {
     if(brightness(pixels[a])<255)
       pixels[a] += lerpColor(pixels[a], color(255), 1-d/rad)<< 16;
     pixels[a] = lerpColor(pixels[a], color(255), 1-d/rad*1.2);
     //use commented line above instead for something more sane
   }
 }
 updatePixels();
}
void mouseClicked() {
 //click to clear
 for(int a=0; a<pixels.length; a++) {
   pixels[a] = 0;
 }
 updatePixels();
}
Re: cool mistake
Reply #2 - May 21st, 2009, 11:05pm
 
That's seizuretastic.
Re: cool mistake
Reply #3 - May 22nd, 2009, 9:03am
 
Thats an interesting result Cedric. Not what I expected when glancing at your modifications. Perhaps I should have included an epilepsy warning. Smiley
Re: cool mistake
Reply #4 - May 22nd, 2009, 10:18am
 
Here's a slight variation on the original:

Code:
int rad = 75; //radius
int oldX = 0;
int oldY = 0;
boolean drawOn = false;

void setup() {
 size(400,400,P3D);
 noFill();
 background(0);
 loadPixels();
}
void draw() {
 // fade out the background:
 // (Not sure why background(0,2) doesn't work... it doesn't respect the alpha)
 fill(0,2);
 rect(0,0,width,height);
 
 // Crude way to determine the speed of mouse movement
 float dx = mouseX - oldX;
 float dy = mouseY - oldY;
 float dist = sqrt(dx*dx + dy*dy);
 // As speed increases so does the dist between the old mouse position and the new
 // Only allow drawing when above a certain 'speed':
 if (dist > 10) {
     drawOn = true;
 }
 else {
   drawOn = false;  
 }
 oldX = mouseX;
 oldY = mouseY;
}

// Only draw when the mouse is moving
void mouseMoved() {
 // tests whether the mouse is moving fast enough  
 if (drawOn) {
 for(int a=0; a<pixels.length; a++) {
   float d = dist(mouseX, mouseY, a%400, (a-a%400)/400);
   //move your mouse
   //a%400 x coord from index, (a-a%400)/400 y coord from index
   if(d <= rad) {
     if(brightness(pixels[a])<255)
     pixels[a] += lerpColor(pixels[a], color(255), 1-d/rad)<<17;
     //pixels[a] = lerpColor(pixels[a], color(255), 1-d/rad);
     //use commented line above instead for something more sane
   }
 }
 updatePixels();
 }

}

void mouseClicked() {
//click to clear
for(int a=0; a<pixels.length; a++) {
  pixels[a] = 0;
}
updatePixels();
}
Re: cool mistake
Reply #5 - May 22nd, 2009, 11:31am
 
I kind of like the functionality of drawing only when the mouse velocity is over a certain threshold. Here is a simpler way to achieve that:

Code:

 if (mouseX - pmouseX > 3 || mouseY - pmouseY > 3) {
     drawOn = true;
 }
 else {
   drawOn = false;  
 }

This uses pmouse which automatically stores the previous mouse coordinates. I just check if the mouse has moved more than a certain amount in one direction or (or is the ||) the other by differencing the current and former positions.

I think a background fade doesn't work here because we are manipulating the pixels array. The fading you have added produces some interesting effects almost like refraction. Nice.
Re: cool mistake
Reply #6 - May 28th, 2009, 10:02pm
 
Hey, that is cool.  Tweaked it into a simple "drawing toy" --

http://benhem.com/games/rickdraw/

it has some extra "effect" brushes to play with the way the ripples interfere. (absolute value of the hex, inverse ripples..)

--Ben
Re: cool mistake
Reply #7 - May 31st, 2009, 10:56am
 
I really like how you set up to for loops so it processes only relevant pixels and not the whole array Ben. I was thinking about doing that. All the interaction you added really makes it more interesting too.

I wondered why your strokes seem to fade in. Also does anyone understand why the banding and interference patterns are produced in the first place?

Edited:
I think theres a feedback loop in there but I never figured out exactly what was happening.
Re: cool mistake
Reply #8 - May 31st, 2009, 2:16pm
 
The patterns you get when simply pressing and releasing the mouse button on the same spot intrigue me... different timing yields different patterns. Very nice.

edit: and what about the white pixels that emerge after awhile? Also interesting...
Page Index Toggle Pages: 1