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)
   void, functions and syntax
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: void, functions and syntax  (Read 250 times)
st33d

WWW Email
void, functions and syntax
« on: Jun 6th, 2004, 4:14pm »

I'm trying to build a program which uses a series of logic gates to choose what it draws. Which means using functions. I write out my starting variables and then look up how you get functions in a processing program. The examples seem consistent with just sticking void in front of the thing. Okay. So I slap one of the functions I will be using at the end of the list of variables to see if I've got the syntax right. And for some reason I can't stick my variables inside a setup() thing because they don't seem to exist outside it, and I can't state variables at the beginning of a program without it going, "what's all this void lark? I'm not running that." Precisely how am I supposed to structure a program with a fat list of variables to set up and have numerous functions to call. I can't find an example of how to do it.
 
int cycle=100;
int r=12;
int l=20;
int [] x=new int[2];
int [] y=new int[2];
x[0]=100;
y[0]=100;
float theta=0.0;
x[1]=int (cos (theta)*l);
y[1]=int (sin (theta)*l);
String tape="0,1,0,0,0,1,1,1,1,0,0,1,0,0,0,1,1,1,0,0,1,0,1,1,1,0,0,1,0,1,0,0,1, 0,0,1,0,1,1,1,1,0,0,1";
int head[] = splitInts(tape, ',');
int h=-1;
size (400,400);
background (255);
stroke (0);
 
void tapeR () {
  if (h<head.length){
    h++;
  }else{
    h=0;
  }
}
 

I could murder a pint.
st33d

WWW Email
Re: void, functions and syntax
« Reply #1 on: Jun 6th, 2004, 5:28pm »

Okay, I pulled out the definitions for the arrays x[] and y[] and put them in a setup() along with the stage definitions and it liked it. But does that mean that those variables aren't going to be accessible? And why can't I leave the set up for the values for my arrays with out the program going schitz?
 

I could murder a pint.
TomC

WWW
Re: void, functions and syntax
« Reply #2 on: Jun 6th, 2004, 6:00pm »

I don't think you can initialise the members of an array outside of a function.  The way you have initialised the other variables is fine.
 
So where you have:
 
Code:

x[0]=100;  
y[0]=100;  

 
and:
Code:

x[1]=int (cos (theta)*l);  
y[1]=int (sin (theta)*l);  

 
You should be doing that inside of setup().
 
To move to the mode which uses setup(), you'll also want to move these function calls inside setup:
 
Code:

size (400,400);  
background (255);  
stroke (0);  

 
And you'll either want a draw() function (for still images) or a loop() function (for moving/interactive applets).
 
Hope that helps.
 
PS You can use code blocks on the forum to make your code easier to read... like the following tags but with square brackets instead:  
 
Code:

<code> // your code here </code>

 
 
st33d

WWW Email
Re: void, functions and syntax
« Reply #3 on: Jun 6th, 2004, 7:15pm »

Cheers. It works fine now, I'm especially pleased that you've told me about the draw() function. The plotter went on a memory gobbling rampage the first few times I ran it with loop().
I am curious though as to why the plotter seems to structure the drawing around a diagonal across the page. If anyone has that much time on their hands I'd love to hear why it's so.
Code:

int cycle=1000; //iterations
int r=8; //directions to plot
int l=10; //initial line length
int [] x=new int[2];
int [] y=new int[2];
float theta=0.0; //initial line angle
String tape="1,1,1,0,0,1,1,1,1,0,0,1,0,0,0,1,1,1,0,0,1,0,1,1,1,0,0,1,0,1,0,0,1, 0,0,1,0,1,1,1,1,0,0,1";
int head[] = splitInts(tape, ','); //instruction array
int h=-1; //tape head position
void setup(){
  size (400,400);
  background (255);
  stroke (0);
  x[0]=100;
  y[0]=100;
  x[1]=int (cos (theta)*l)+x[0];
  y[1]=int (sin (theta)*l)+y[0];
}
void draw (){
  for (int i=0;i<cycle;i++) {
    tapeR();
    if (head[h]==1){
 l++;
    }else{
 l--;
    }
    transOrDraw();
    if (x[1]>width){
 x[1]-=width;
 x[0]-=width;
    }
    if (x[1]<0){
 x[1]+=width;
 x[0]+=width;
    }
    if (y[1]>height){
 y[1]-=height;
 y[0]-=height;
    }
    if (y[1]<0){
 y[1]+=height;
 y[0]+=height;
    }
  }
}
//functions
void transOrDraw(){
  tapeR();
  if (head[h]==1){
    turnOrTrans();
  }else{
    pointOrLine();
  }
}
void turnOrTrans(){
  tapeR();
  if (head[h]==1){
    //TURN
    tapeR();
    if (head[h]==1){
 theta+=(TWO_PI/r);
    }else{
 theta-=(TWO_PI/r);
    }
  }else{
    x[0]=x[1];
    y[0]=y[1];
    x[1]+=int (cos (theta)*l);
    y[1]+=int(sin (theta)*l);
  }
}
void pointOrLine(){
  tapeR();
  if (head[h]==1){
    point (x[1],y[1]);
    x[0]=x[1];
    y[0]=y[1];
    x[1]+=int (cos (theta)*l);
    y[1]+=int(sin (theta)*l);
  }else{
    StrOrCurv();
  }
}
void StrOrCurv(){
  line (x[0],y[0],y[1],y[1]);
  x[0]=x[1];
  y[0]=y[1];
  x[1]+=int (cos (theta)*l);
  y[1]+=int(sin (theta)*l);
  //awaiting further logic gates to add
}
//tape head
void tapeR () {
  if (h<((head.length)-1)){
    h++;
  }else{
    h=0;
  }
}
 

I could murder a pint.
justo


Re: void, functions and syntax
« Reply #4 on: Jun 6th, 2004, 9:27pm »

you can also initialize the arrays outside of setup, with a few caveats:
 
int[] x = { 100, (int)(cos(theta)*l) };
 
you just have to make sure any functions you call are static...the compiler will complain if theyre not. i'm not sure if processing's cos() is static or not, but Math.cos() is. you could also set x[1] to 20, since cos(0) is 1, unless you want to change the initialization values of theta or l;
 
Pages: 1 

« Previous topic | Next topic »