Return an array of values from a loop

Hi there!

I just started using processing, and I am very new to coding.

I am struggling with scope or arrays, im not even sure.

I create an array of points, and paint them by using Perlin Noise.
However, I want to use “stroke” outside of the “for loop”.
Can anyone help me?

Including the code:

float inc = 0.1; // SIZE oF NOISE
float scl = 10; // SCALE of GRID
float r;

void setup() {
  size(500,500);
  noSmooth(); // Draws all geometry and fonts with jagged edges
  strokeCap(PROJECT); // appearance of the points: ROUND - circle, PROJECT - square
  strokeWeight(5);  // thicknes of the points
  
}


void draw() {
  float yoff = 0;
  for (float y = 0; y < height; y++) {
    float xoff = 0;
    for (float x = 0; x < width; x++) {
      float r = noise(xoff, yoff) * 255;
      xoff += inc;
      //stroke(r);
      point(x * scl, y * scl);
    }
    yoff += inc;
  }
  stroke(r); // <----------- I need it here for further development
  println(r);
}
1 Like

Hello,

for (float x = 0; x < width; x++) {
      r = noise(xoff, yoff) * 255; // <----- Remove the float
      xoff += inc;
      //stroke(r);
      point(x * scl, y * scl);
    }

Explained here:
https://processing.org/examples/variablescope.html

1 Like

Hi glv, thank you for your response!

When I do like so, it renders the data with only one value :frowning:

Hi,

Go through your code and address all the scope issues; there may be other logical errors as well.
I showed you one obvious example.
There may be others to sort out.

I just took one quick look… I will leave this with you to sort out.

1 Like

You can store ALL colors from r into an array (1D or 2D array, see tutorials: Tutorials / Processing.org).

In my code below the array is listColors2. (listColors is only temporary)

The scope

The scope of r is only INSIDE the for-loop because of this line:

  float r = noise(xoff, yoff) * 255;

you can use r after the for loop but then you would use only the last value of r (not all of them like in an array). Would that be enough for you?

To achieve this say float r; before the for loop (or even before setup()) and say r = noise(xoff, yoff) * 255; within the for-loop (without repeating the word “float”).

  • before the for loop means: scope = function
  • before setup() means: scope = entire sketch
  • inside the for-loop: scope = inside the for-loop

Explained here:

The colors

By the way, data type color is in fact int.

so you can say

int r = 23344;

  • stroke(r); - for point or line
  • fill(r); - for ellipses, rect, cube, sphere (for sphere() together with noStroke();)…

or

  • color(44); // gray or
  • color(255,2,2); // which is red.

Chrisir

Full code with array

float inc = 0.1; // SIZE of NOISE
float scl = 10; // SCALE of GRID
float r;

color[] listColors2; 

void setup() {
  size(500, 500);
  noSmooth(); // Draws all geometry and fonts with jagged edges
  strokeCap(PROJECT); // appearance of the points: ROUND - circle, PROJECT - square
  strokeWeight(5);  // thickness of the points
}//

void draw() {

  IntList listColors = new IntList();  

  float yoff = 0;
  for (float y = 0; y < height; y++) {
    float xoff = 0;
    for (float x = 0; x < width; x++) {
      float r = noise(xoff, yoff) * 255;
      xoff += inc;
      stroke(r);
      listColors.append(int(r)); 
      point(x * scl, y * scl);
    }
    yoff += inc;
  }
  // stroke(r); // <----------- I need it here for further development
  listColors2 = listColors.array();

  for (color c : listColors2) {
    println(c);
  }
  //
}//
//
2 Likes