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.
Page Index Toggle Pages: 1
Variables-Scope (Read 673 times)
Variables-Scope
Mar 31st, 2007, 6:12pm
 
Hi folks,

it seems that i understand not realy the scope of variables ...

When i work with an variable in an if-condition the newly variables created in this if-statement are only local for this condition. (?!)

How can i access the variables created in those if-conditions?



void setup() {
 size(300, 300);
 background(64);
}

void draw() {

stroke(128);
noFill();
arc(150,150,250,250,0,360);
arc(150,150,200,200,0,360);
arc(150,150,150,150,0,360);
arc(150,150,100,100,0,360);
arc(150,150,50,50,0,360);
stroke(255);
line(150,25,150,275);
line(25,150,275,150);

// later the values XPOSF1 and YPOSF1 are catched from a txt-file ... therefor a "String"
String XPOSF1 = "-0.013";
String YPOSF1 = "+0.032";

float xpos = float(XPOSF1);
float ypos = float(YPOSF1);

if ( xpos < 0) { // PosX im Minus
  String posx_string = ""+xpos;
  String posx_string_splitted[] = split(posx_string, '.');
  int coordinate_x = 150-Integer.parseInt(posx_string_splitted[1]);
 
  //println(coordinate_x);
}
if (xpos > 0) { // PosX im Plus
  String posx_string = ""+xpos;
  String posx_string_splitted[] = split(posx_string, '.');
  int coordinate_x = 150+(Integer.parseInt(posx_string_splitted[1])/2);
 
  //println(coordinate_x);
}
if ( ypos < 0) { // YPos im Minus
  String posy_string = ""+ypos;
  String posy_string_splitted[] = split(posy_string, '.');
  int coordinate_y = Integer.parseInt(posy_string_splitted[1])+150;
 
  //println(coordinate_y);
}
if (ypos > 0) { // YPos im Plus
  String posy_string = ""+ypos;
  String posy_string_splitted[] = split(posy_string, '.');
  int coordinate_y = 150-(Integer.parseInt(posy_string_splitted[1])/2);
 
  //println(coordinate_y);
}
stroke(234,0,0);
fill(234,0,0);

// arc can't access coordinate_x or coordinate_y ... coordinate_x should be 137 and coordinate_y 164

arc(coordinate_x,coordinate_y,3,3,0,360);

}
Re: Variables-Scope
Reply #1 - Mar 31st, 2007, 7:57pm
 
You can create variables without assigning them a value, and assign values later:

Code:

//within draw
int coordinate_x,coordinate_y;

if ( xpos < 0) { // PosX im Minus
String posx_string = ""+xpos;
String posx_string_splitted[] = split(posx_string, '.');
coordinate_x = 150-Integer.parseInt(posx_string_splitted[1]);

//println(coordinate_x);
}
//etc etc
Re: Variables-Scope
Reply #2 - Mar 31st, 2007, 8:19pm
 
When i do so i will became an Semantic error ...

Code:

void setup() {
size(300, 300);
background(64);
}

void draw() {
int coordinate_x, coordinate_y;

stroke(128);

String XPOSF1 = "-0.013";
String YPOSF1 = "+0.032";

float xpos = float(XPOSF1);
float ypos = float(YPOSF1);

if ( xpos < 0) { // PosX im Minus
String posx_string = ""+xpos;
String posx_string_splitted[] = split(posx_string, '.');
int coordinate_x = 150-Integer.parseInt(posx_string_splitted[1]);
}
if (xpos > 0) { // PosX im Plus
String posx_string = ""+xpos;
String posx_string_splitted[] = split(posx_string, '.');
int coordinate_x = 150+(Integer.parseInt(posx_string_splitted[1])/2);
}
if ( ypos < 0) { // YPos im Minus
String posy_string = ""+ypos;
String posy_string_splitted[] = split(posy_string, '.');
int coordinate_y = Integer.parseInt(posy_string_splitted[1])+150;
}
if (ypos > 0) { // YPos im Plus
String posy_string = ""+ypos;
String posy_string_splitted[] = split(posy_string, '.');
int coordinate_y = 150-(Integer.parseInt(posy_string_splitted[1])/2);
}

println(coordinate_x + "," + coordinate_y);

}


Quote:
Semantic Error: Duplicate declaration of local variable "coordinate_x". The other occurrence is at location "C:/DOKUME~1/***/LOKALE~1/Temp/build45130.tmp/Temporary_9317_4457.java:7".

Semantic Error: Duplicate declaration of local variable "coordinate_x". The other occurrence is at location "C:/DOKUME~1/***/LOKALE~1/Temp/build45130.tmp/Temporary_9317_4457.java:7".

Semantic Error: Duplicate declaration of local variable "coordinate_y". The other occurrence is at location "C:/DOKUME~1/***/LOKALE~1/Temp/build45130.tmp/Temporary_9317_4457.java:7".


Semantic Error: Duplicate declaration of local variable "coordinate_y". The other occurrence is at location "C:/DOKUME~1/***/LOKALE~1/Temp/build45130.tmp/Temporary_9317_4457.java:7".
Re: Variables-Scope
Reply #3 - Mar 31st, 2007, 9:34pm
 
If you notice, I took the "int" away from the front of the coordinate_x and y from within the "if" statements. You declare coordinate_x as an in outside, then within you assign it a value, not declare it to be an int again.
Re: Variables-Scope
Reply #4 - Mar 31st, 2007, 11:49pm
 
After some reading througt the reference i've choosen another way ...

nf() is a little bit handier in my case (imho)

But thanks anyway, John!!

Code:
void setup() {
size(300, 300);
background(64);
noLoop();
}

void draw() {
stroke(128);
noFill();
arc(150,150,250,250,0,360);
arc(150,150,200,200,0,360);
arc(150,150,150,150,0,360);
arc(150,150,100,100,0,360);
arc(150,150,50,50,0,360);
stroke(255);
line(150,25,150,275);
line(25,150,275,150);

String XPOSF1 = "-0.050";
String YPOSF1 = "+0.050";

float xpos = float(XPOSF1);
float ypos = float(YPOSF1);

if ( xpos < 0 && ypos < 0) { // PosX im Minus und PosY im Minus
String posx_string = nf(xpos,1,3);
String posy_string = nf(ypos,1,3);
int posx_string_splitted[] = int(split(posx_string, ','));
int posy_string_splitted[] = int(split(posy_string, ','));
int coordinate_x = 150-posx_string_splitted[1];
int coordinate_y = 150+posy_string_splitted[1];
draw_point(coordinate_x, coordinate_y);
}

if (xpos > 0 && ypos < 0) { // PosX im Plus und PosY im Minus
String posx_string = nf(xpos,1,3);
String posy_string = nf(ypos,1,3);
int posx_string_splitted[] = int(split(posx_string, ','));
int posy_string_splitted[] = int(split(posy_string, ','));
int coordinate_x = 150+posx_string_splitted[1];
int coordinate_y = 150+posy_string_splitted[1];
draw_point(coordinate_x, coordinate_y);
}

if ( xpos > 0 && ypos > 0) { // PosX im Plus und YPos im Plus
String posx_string = nf(xpos,1,3);
String posy_string = nf(ypos,1,3);
int posx_string_splitted[] = int(split(posx_string, ','));
int posy_string_splitted[] = int(split(posy_string, ','));
int coordinate_x = 150+posx_string_splitted[1];
int coordinate_y = 150-posy_string_splitted[1];
draw_point(coordinate_x, coordinate_y);
}

if (xpos < 0 && ypos > 0) { // PosX im Minus und YPos im Plus
String posx_string = nf(xpos,1,3);
String posy_string = nf(ypos,1,3);
int posx_string_splitted[] = int(split(posx_string, ','));
int posy_string_splitted[] = int(split(posy_string, ','));
int coordinate_x = 150-posx_string_splitted[1];
int coordinate_y = 150-posy_string_splitted[1];
draw_point(coordinate_x, coordinate_y);
}
}

void draw_point(int xloc, int yloc)
{
stroke(255,0,0);
fill(255,0,0);
arc(xloc, yloc,7,7,0,360);
}
Page Index Toggle Pages: 1