Problem making a wrap around loop with modulo

I’m trying to make a wrap around loop with modulus. But it’s not working. Keep getting error “expecting {, found [”.
Showing function for loop appears in.
If anyone can help, many thanks!

color[]getThreeColorLerp() { 

  color start = color (245, 17, 48);
  color midpoint = color (250, 200, 63);
  color end = color(12, 187, 229);

  int arraySize = 20;
  float step = 1.0f / arraySize;

  IntList colorGroup_1 = new IntList ();

  // wrap around loop with modulo not working
  for (int k = 0; k < arraySize*5; k++) {

    [k] = k % arraySize;
  }

  println(k);

  float m = map (step, 0.0, 1.0, 0.0, 3.3);

  if (k * m <= 1.0) {

    colorGroup_1.set (k, lerpColor(start, midpoint, k*m));
    println(k*m);
  } else if (k*m > 1.0 && k*m <= 2.0) {

    colorGroup_1.set (k, lerpColor(midpoint, end, (k*m)-1.1));
    println(k*m);
  } else {

    colorGroup_1.set (k, lerpColor(end, start, (k*m)-2.2));
    println(k*m);
  }
  return colorGroup_1.array();
}
1 Like

That won’t work

You need

  • an array to use [k]: list[k]

  • or you say k = …

  • or using Intlist you want something like colorGroup1.get( k % arraySize ) ;

See reference

Chrisir

1 Like

I’m realizing I’m in over my head with the wrap around loop. So am setting this project aside for now… I’ll come back to it when I have a broader foundation.
But I do appreciate your suggestions above.

1 Like

Hello,

Consider:

  • Writing small and simple functional blocks first to understand every element of code.
  • Using console prints, writing text to canvas, coloring, animating, etc. to visualize and understand code.

I saw “wrap around loop using modulo” and wrote this simple example:

// Simple Wrap Around Loop Using Modulo
// v1.0.0
// GLV 2012-02-16

int [] data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; //16 elements 
int offset;

void setup() 
	{
  offset = 5;
  for (int i = 0; i< data.length; i++)
    { 
    int w = (i+offset)%data.length;
    println(i, data[i] , data[w]);
    }
  }

I then modified (write text to canvas, added color, animated, etc. ):

Wrap around loop (enhanced)
// Simple Wrap Around Loop Using Modulo
// v2.0.0
// GLV 2012-02-16

int [] data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; //16 elements 
int offset;

void setup() 
	{
  size(700, 100);
  textSize(24);
  }

void draw() 
	{
  background(0);
  
  if (frameCount%30 == 0) offset++;       // increments offset every 30 frames
  if (offset > data.length) offset = 0;
  
  for (int i = 0; i< data.length; i++)
    { 
    int w = (i+offset)%data.length;
    
    // no wrapping
    if(i >= offset) fill(255, 0, 0); else fill(0, 255, 0);    
    text(data[i], i*40 + 40, height/2);    
    
    // wrapping
    if(w >= offset) fill(255, 0, 0); else fill(0, 255, 0);
    text(data[w], i*40 + 40, height/2+30); // wrapping
    
    println(i, data[i] , data[w]);
    }
  }

:slight_smile:

1 Like

modulo is simple, it takes the remainder of a division

for (int i=0; i < 16; i++) 
  println (i+" -> " + i%5); //or k=i%5;

gives you


0 -> 0
1 -> 1
2 -> 2
3 -> 3
4 -> 4
5 -> 0
6 -> 1
7 -> 2
8 -> 3
9 -> 4
10 -> 0
11 -> 1
12 -> 2
13 -> 3
14 -> 4
15 -> 0

So you can use it when your array has only 5 elements (index 2nd column), but your for loop is longer than 5 (first column above).

You can also write

k++;
if(k>4) i=0;

which I personally find much better to read

Chrisir

1 Like

Understood.

Ah ha! This way looks more straightforward. I will try this!
Thank you!!

Thank you, that’s good advice. I will start doing this more going forward!!

You also had tried to change the variable of your for loop

This is not recommended and you should know what you are doing if you do it

Instead take a new variable (k2 or so) like in my example above

1 Like

here is an example using modulo

please note that modulo is not used here to make an array but to read the content of an array (use it)

more precise, we have more slots on the screen to fill with a color than the color array has elements.

To avoid that we read over the end of the color array, we use % modulo, so that after the end of the array we restart reading the color array from the beginning (in setup() it’s this line: shuffledInventory[ i % shuffledInventory.length ] ).

Alternative to modulo would be (as I said) ... shuffledInventory[ k ] ... and then

k++; 
if(k>shuffledInventory.length) 
    k=0;

Chrisir


// global array 
Cell [] cells = new Cell [39];

void setup() {
  size (900, 400);

  // NOT shuffled at the moment 
  color[] shuffledInventory = getColorListShuffled(); 
  println(shuffledInventory.length);

  int x = 20, y = x;
  int w = (width-40)/cells.length, h = 360;

  for (int i = 0; i < cells.length; i++) {
    cells[i] = new Cell(x+i*w, y, 
      w, h, 
      shuffledInventory[ i % shuffledInventory.length ] );
  }//for
}//func

void draw() {
  background (51);

  for (int i = 0; i < cells.length; i++) {
    cells[i].display();
  }//for
}//func 

//-----------------------------------------------------------------------------------------
// Tools 

color[] getColorListShuffled() {

  // for the command lerpColor: 
  color from = color(random(255), random(255), random(255)); 
  color to = color(random(255), random(255), random(255));
  float step = 1.0f / (10-1); // **must use a float number to return a float number**

  IntList inventory = new IntList(); // stores list of colors

  // for loop to generate an IntList of random colors
  for (int k = 0; k < 10; k++) {
    inventory.set(k, lerpColor (from, to, k*step));
  }
  // inventory.shuffle(); // NOT randomly shuffles the list inventory

  // directs the inventory 
  // as an array to 
  return inventory.array();
}

///////////////////////////////////////////////////////////////////////////////////////////

class Cell {
  float x, y, w, h;
  color clr;

  Cell ( 
    float tempX, float tempY, 
    float tempW, float tempH, color tempClr) {

    x = tempX;
    y = tempY;
    w = tempW;
    h = tempH;
    clr = tempClr;
  }
  void display() {

    stroke(255); 
    fill(clr);
    rect(x, y, w, h);
  }
  //
}//class
//
2 Likes

Ah! That makes sense! I had overthought it and was way off track…
Thank you!I
:slightly_smiling_face:

1 Like