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_
   Topics & Contributions
   Beyond Categories
(Moderator: REAS)
   function return variable to another function?
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: function return variable to another function?  (Read 3501 times)
whiteflea


function return variable to another function?
« on: Apr 5th, 2005, 12:36am »

Beginner problem!
I like to return a value from 1 function to another function:
 
Code:

void setup() {
  size(400, 400);
  background(0);
  stroke(255);
  line(0,height-1,width,height-1);
}
 
void loop() {
  initialize();
  paint(xPos);
}
 
void initialize(){
  int xPos = int(random(width));
  return xPos; // <----This is not working!
};
 
void paint(int value) {
 //do something here
}

 
What is my beginner Problem?
 
THX
« Last Edit: Apr 5th, 2005, 2:18am by whiteflea »  



==================================
Software is mindcontrol - get some
==================================
Shut up or I will replace you with
a very small processing script


st33d

WWW Email
Re: function return variable to another function?
« Reply #1 on: Apr 5th, 2005, 1:09am »

Code:

int xPos(){ // <-- is this what you want?
  return int(random(width));
};

You simply refer to this new variable as xPos(). I don't think your problems end there though. The screen is drawn only once per loop. You need to use background(value) to clear it for animated stuff. I ran your code with my function and it seemed to put a black bar at the bottom (which rect() can do) and slowly remove it.
 

I could murder a pint.
whiteflea


Re: function return variable to another function?
« Reply #2 on: Apr 5th, 2005, 2:17am »

Well I like to make a DLA
http://classes.yale.edu/Fractals/Panorama/Physics/DLA/DLA.html
but not from the center, from top-down like an falling snowflake.
 
So I need to make a groundline:
Code:

...
stroke(255);  
line(0,height-1,width,height-1);
...

 
In the loop section the steps are something like this:
1. initialize
2. check if lower row is empty.
if empty then paint, else stick
 
3 if stick start over, else go to step 2
 
then I like to start from a random top-point:
Code:

 void initialize(){
  int xPos = int(random(width));
  return xPos; // <----This is not working!
};

 
then I need to paint the pixel:
Code:

 void paint(int value) {  
  //painting the pixel here...
};

 
Im a beginner and I miss something of your code.
where do I have to put in your code? in my initialize function? Or do I have to make something like an global var?
 
THX
« Last Edit: Apr 5th, 2005, 2:17am by whiteflea »  



==================================
Software is mindcontrol - get some
==================================
Shut up or I will replace you with
a very small processing script


st33d

WWW Email
Re: function return variable to another function?
« Reply #3 on: Apr 5th, 2005, 8:04pm »

That's a very interesting link and happens to relate exactly to the work on my website. I didn't know it had a name or a basis in theory. It's not quite beginner stuff though, as the code for the simplest example is a bit of a push if you're new to programming but seeing as this is my field I've worked up a head start for you. I intend to see if I can apply this to my own work.
 
In order to familiarise yourself with what I've done you need to look up these things (the forum or a search for Java code or C++ code with the key word "tutorial" helps):
 
Object oriented programming
Arrays
The switch statement
 
I've also placed the int fuction in the code so you can see it being used.
 
Code:

//an array is a variable with compartments
//this initalises the array
wanderer [] w;
 
void setup(){
  size (100,100);
  background(255);
  //seed
  stroke(0);
  point(50,50);
  //this sets how many compartments
  w = new wanderer[200];
  //these next few lines initialise the wanderer variables - or objects
  for(int i = 0; i < w.length; i++){
    //set start along left or right edge for now
    //you can figure out different start positions for yourself
    w[i] = new wanderer(leftorright());
  }
}
 
void loop(){
  for(int i = 0; i < w.length; i++){
    w[i].move();
    w[i].check();
    w[i].draw();
  }
}
 
//a class is like a mini program that acts like a variable
class wanderer{
  //class globals
  int x,y;
  //class setup/initialisation
  wanderer(int xt){
    x = xt;//variable transfered from initialisation line
    y = ini();
  }
  //class methods
  void move(){
    int d = int(random(4));
    //a switch is like a stack of if statements that act on one variable
    //each case needs to end in break to exit the switch
    switch(d){
 case 0:
 x++;
 if (x > width-2){
   x = leftorright();
 }
 break;
 case 1:
 y++;
 if (y > height-2){
   x = leftorright();
 }
 break;
 case 2:
 if (x < 1){
   x = leftorright();
 }
 x--;
 break;
 case 3:
 y--;
 if (y < 1){
   x = leftorright();
 }
 break;
    }
  }
  void check(){
    //check for a black pixel nearby
    //if yes, put this wanderer back at the start position
    color black = #000000;
    stroke(black);
    if (get(x+1,y) == black){
 point(x,y);
 x = leftorright();
 y = ini();
    }
    if (get(x-1,y) == black){
 point(x,y);
 x = leftorright();
 y = ini();
    }
    if (get(x,y+1) == black){
 point(x,y);
 x = leftorright();
 y = ini();
    }
    if (get(x,y-1) == black){
 point(x,y);
 x = leftorright();
 y = ini();
    }
  }
  void draw(){
    stroke(240);
    point(x,y);
  }
}//end of class
 
//global methods
int ini(){
  return int(random(height));
}
/*
it's just like a normal function but you state the returning
variable type instead of void. That's what void means, return
no information.
*/
int leftorright(){
  return int(random(2)) > 0 ? width-2 : 1;
}
/*
this last is a little more tricky, I've listed the forum link on it below as I've only just learnt it myself but it will help if you're starting things on the edge and is less confusing than it looks. It basically says make a random integer of 1 or 0, if that integer is greater than one then (?) return width-2, else (:) return 1.
*/

http://processing.org/discourse/yabb/board_Programs_action_disp_lay_num_1112423782.html
 

I could murder a pint.
Pages: 1 

« Previous topic | Next topic »