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)
   Global values
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: Global values  (Read 413 times)
Charles Hinshaw
Guest
Email
Global values
« on: Jan 26th, 2004, 9:30pm »

Maybe someone can help here.
 
I need to access an array in my loop();
The array isn't global because the depth of the array isn't determined until half way through setup() -- (it is based on the number of pixels on the screen and on the values of those pixels.)
 
I did a search in this section, but nothing turned up.
Is there a way to get my array to be accessable inside of the loop? I would like to get it working so that I can figure out how badly I messed up the rest of the code...
 
Thanks,
 
Charles Hinshaw
direction + repertoire
[r]evolve, US
 
Charles Hinshaw
Guest
Email
Re: Global values
« Reply #1 on: Jan 26th, 2004, 9:39pm »

This may help (or completely confuse people, as it is my first attempt at building something in processing) --
Basically, I need to access the array "p[]" inside of the loop(), BUT the array can't be be declared until I know the total number of particles to create.
 
Also, this is very slow... maybe because I am working with between 25,000 and 5,000,000 particles, depending on the depth of the images that I have used, but if anyone can give me some advice as to how to speed it up, that would be GREAT. Thanks.
 
 
// User Control Variables
String imagename = "dust.jpg"; // Initial Image File in DATA  Directory
 
// ------------------------------------------------------------------------ -------------------
int psum = 0;
void setup() // SET UP SOFTWARE
{
  // create the Processing environment
  size(200,200);
  framerate(30);
  smooth();
  background(255);
 
  // load an image from the user variable "imagename"
  BImage startimage;
  startimage = loadImage(imagename);
  image(startimage, 0, 0);
 
  // convert loaded image to greyscale and spit it upon the screen
  color c1 = get(0,0);
  color c2 = get(0,0);
  float bright = brightness(c1);
  for (int x = 0; x < width; x++){
    for (int y = 0; y < height; y++){
 colorMode(HSB);
 c1 = get(x,y);
 bright = brightness(c1);
 colorMode(RGB);
 c2 = color(bright, bright, bright);
 set (x,y,c2);
    }
  }
 
  // create density array -  white = 0 and black = 255.
  int[][] density = new int[width][height];
  for (int x = 0; x < width; x++){
    for (int y = 0; y < height; y++){
 c1 = get(x,y) & 0xff;
 c1 = 255-c1;
 density[x][y] = c1;
    }
  }
 
  // determine total particles to create;
  int particlesum = 0;
  for (int x = 0; x < width; x++){
    for (int y = 0; y < height; y++){
 particlesum += density[x][y];
    }
  }
  psum = particlesum;
 
  // create the particle field
  background(255);
  particle p[] = new particle[particlesum];
  int z = 0;
  for (int x = 0; x < width; x++){
    for (int y = 0; y < height; y++){
 if(density[x][y] != 0){
   int thisdensity = density[x][y];
   for(int particlebirth = 0; particlebirth < thisdensity; particlebirth++){
     p[particlebirth] = new particle(x,y,z);
     p[particlebirth].update();
   }
 }
    }
  }
}
// ------------------------------------------------------------------------ -------------------
void loop() // MAIN SOFTWARE LOOP
{
  for (int i = 0; i < psum; i++){
    p[i].x += random(-3, 3);
    p[i].y += random(-3, 3);
    p[i].z += random(-3, 3);
    p[i].update();
  }
}
// ------------------------------------------------------------------------ -------------------
class particle {
  int xloc, yloc, zloc;
  particle(int x, int y, int z) {
    xloc = x;
    yloc = y;
    zloc = z;
  }
  void update() {
    push();
    noStroke();
    fill(0,1);
    translate (xloc, yloc, zloc);
    box (1,1,1);
    pop();
  }
}
 
kevinP

Email
Re: Global values
« Reply #2 on: Jan 26th, 2004, 9:56pm »

Hi Charles,
 
Maybe I have part of the answer...
 
To make a variable initialized in setup() accessible to loop() you can declare it beforehand and then initialize it in setup().
 
Code:

Star[] myStars;
 
setup()
{
  //..
  myStars = new Star[1000];
}

 
But I have my own question to go with this... what if one doesn't know how big an array one needs until the time of "user interaction"? Does one then use "java.util.ArrayList"?
« Last Edit: Jan 26th, 2004, 9:58pm by kevinP »  

Kevin Pfeiffer
kevinP

Email
Re: Global values
« Reply #3 on: Jan 26th, 2004, 10:46pm »

Here's one other thing I see (possibly)...
 
You wrote:
Code:

void loop() // MAIN SOFTWARE LOOP  
 {  
   for (int i = 0; i < psum; i++){  
     p[i].x += random(-3, 3);  
     p[i].y += random(-3, 3);  
     p[i].z += random(-3, 3);  
     p[i].update();  
   }  
 }  
 // ----------------------------------------------------------------
 class particle {  
   int xloc, yloc, zloc;  
   particle(int x, int y, int z) {  
     xloc = x;  
     yloc = y;  
     zloc = z;  
   }  

 
Shouldn't that be:
     p[i].xloc += random(-3, 3);  
     // etc.
 
(Not to mention "class Particle"?)
 
-K
« Last Edit: Jan 26th, 2004, 10:46pm by kevinP »  

Kevin Pfeiffer
Charles Hinshaw
Guest
Email
Re: Global values
« Reply #4 on: Jan 26th, 2004, 10:48pm »

Thanks, Kevin... It seemed to get that aspect working.
 
Ok, in a related issue (?) --
 
here is my refined code:
Code:

// User Control Variables
String imagename = "digidust.jpg"; // Initial Image File in DATA  Directory
 
//Global Variables
particle p[];
int psum = 0;
// ------------------------------------------------------------------------ -------------------
void setup() // SET UP SOFTWARE
{
  // create the Processing environment
  size(200,200);
  framerate(30);
  smooth();
  background(255);
 
  // load an image from the user variable "imagename"
  BImage startimage;
  startimage = loadImage(imagename);
  image(startimage, 0, 0);
 
  // convert loaded image to greyscale and spit it upon the screen
  color c1 = get(0,0);
  color c2 = get(0,0);
  float bright = brightness(c1);
  for (int x = 0; x < width; x++){
    for (int y = 0; y < height; y++){
 colorMode(HSB);
 c1 = get(x,y);
 bright = brightness(c1);
 colorMode(RGB);
 c2 = color(bright, bright, bright);
 set (x,y,c2);
    }
  }
 
  // create density array -  white = 0 and black = 255.
  int[][] density = new int[width][height];
  for (int x = 0; x < width; x++){
    for (int y = 0; y < height; y++){
 int colors = get(x,y) & 0xff;
 colors = 100-int(colors/2.55);
 //c1 = 255-c1;
 density[x][y] = colors;
    }
  }
 
  // determine total particles to create;
  int particlesum = 0;
  for (int x = 0; x < width; x++){
    for (int y = 0; y < height; y++){
 particlesum += density[x][y];
    }
  }
  psum = particlesum;
 
  // create the particle field
  background(255);
  particle p[] = new particle[particlesum];
  int z = 0;
  for (int x = 0; x < width; x++){
    for (int y = 0; y < height; y++){
 if(density[x][y] != 0){
   int thisdensity = density[x][y];
   for(int particlebirth = 0; particlebirth < thisdensity; particlebirth++){
     p[particlebirth] = new particle(x,y,z);
     p[particlebirth].update();
   }
 }
    }
  }
}
// ------------------------------------------------------------------------ -------------------
void loop() // MAIN SOFTWARE LOOP
{
  int newx = 0;
  int newy = 0;
  int newz = 0;
  for (int i = 0; i < psum; i++){
    newx = int(p[i].xloc + random(-3, 3));
    newy = int(p[i].yloc + random(-3, 3));
    newz = int(p[i].zloc + random(-3, 3));
    p[i] = new particle(newx,newy,newz);
    p[i].update();
  }
}
// ------------------------------------------------------------------------ -------------------
class particle {
  int xloc, yloc, zloc;
  particle(int x, int y, int z) {
    xloc = x;
    yloc = y;
    zloc = z;
  }
  void update() {
    color c = color(0,0,0,1);
    stroke(c);
    point(xloc, yloc, zloc);
  }
}

 
Everything displays fine from the setup(), but I get a null pointer exception when I get to the for loop in the loop().
 
Does anybody know why?
 
What I am trying to do is use brownian motion to jiggle my particles in 3 dimensions.
 
Also -- if anybody has time to scan through and give me a mini-lecture on optimization, it would be very well received.
 
Thanks,
 
Charles Hinshaw
direction + repertoire
[r]evolve, US
 
http://www.everydayrevolution.com
 
Charles Hinshaw
Guest
Email
Re: Global values
« Reply #5 on: Jan 27th, 2004, 12:15am »

I have posted the most current related code in a new thread since I no longer believe this to be a global/local issue.
 
It is in the Syntax section too, if you can help.
 
thanks,
 
Charles Hinshaw
 
Pages: 1 

« Previous topic | Next topic »