How to access var value from out side of if
statement. I want to access brightestX
value from outside of if(video.available)
statement.
void draw(){
if (video.available())
{
video.read();
image(video, 0, 0, width, height);
int brightestX = 0;
int brightestY = 0;
float brightestValue = 0;
video.loadPixels();
int index = 0;
for ( int y = 0; y < video.height; y++) {
for ( int x = 0; x < video.width; x++) {
int pixelValue = video.pixels[index];
float pixelBrightness = brightness(pixelValue);
if (pixelBrightness > brightestValue) {
brightestValue = pixelBrightness;
brightestY = y;
brightestX = x;
}
index++;
}
}
fill(255, 204, 0, 128);
ellipse(brightestX, brightestY, 200, 200);
}
}
Simply declare the variables you want to access outside the scope of the if
condition.
You can reference the same variables later inside the if
condition to update them, then after the if
you can read these updated values.
Of course these values will only be updated while video data is available (otherwise remain as the most recently updated values):
void draw(){
int brightestX = 0;
int brightestY = 0;
if (video.available())
{
video.read();
image(video, 0, 0, width, height);
float brightestValue = 0;
video.loadPixels();
int index = 0;
for ( int y = 0; y < video.height; y++) {
for ( int x = 0; x < video.width; x++) {
int pixelValue = video.pixels[index];
float pixelBrightness = brightness(pixelValue);
if (pixelBrightness > brightestValue) {
brightestValue = pixelBrightness;
brightestY = y;
brightestX = x;
}
index++;
}
}
fill(255, 204, 0, 128);
ellipse(brightestX, brightestY, 200, 200);
}
println(brightestX, brightestY);
}
If it helps, also check out Java Scope.