Why does my int give me 3 results?

Hello! I am re-entering the world of processing, and have hit a learning snag. With what I have right now, It is giving me 10, 11, and 12 as an answer. I understand the 11, but not the other 2. Can anyone explain? Thank you in advance!

void setup(){
  size(400,400);
  int x;
}
int x=1;
void draw(){
  x = (x%3) +10;
  println(x);
  rect(mouseX,mouseY,x,x);
}
1 Like

x % 3 is either 0, 1 or 2. Adding 10 is then either 10, 11 or 12. :man_teacher:

2 Likes

Explanation

draw loops endlessly;

  • first frame x is 1, 1 modulo (%) 3 is 1, 1 plus 10 = 11

  • 2nd frame: 11 modulo 3 = 2, 2+10 = 12

  • 3rd: 12 modulo 3: 0, 0 + 10 = 10

  • 4th 10 mod 3: 1 , 1 + 10 = 11

Alternative formula

Or use

x = (x+10) % 3;

Image of Values

test1

Sketch for the Table

here is a sketch to record the values of x, x mod 3 and + 10 in a table:

int x=1;
int i=0; 

// class with tools 
TableTools tools = new TableTools(); 

// the table
Table tValuesFromDraw; 

void setup() {
  size(400, 400);
  background(90); 

  // setting up table (column names / headlines)
  tValuesFromDraw = tools.newTable("Frame", "x", "x mod 3", "+ 10");
}

void draw() {

  background(90); 

  // When i is >= 12 ------------
  if (i>=12) {
    // show table
    tools.showTable(tValuesFromDraw, 
      22, 22);
    return; // leave here and restart draw
  }

  // normal sketch : --------------------
  // adding data (must match headlines)
  tValuesFromDraw = tools.tableAddDataInt ( tValuesFromDraw, frameCount, x, x % 3, (x % 3) + 10 );

  x = (x%3) +10;
  println(x);
  rect(mouseX, mouseY, x, x);

  i++;
}

void keyPressed() {
  if (key==' ')
    save("test1.jpg");
}

//===============================================================================
// Tools collection 

class TableTools {

  // class not like a car class Car for an object but a collection of tools. 

  final color RED   = color(255, 0, 0); 
  final color GREEN = color(0, 255, 0); 
  final color BLUE  = color(0, 0, 255); 

  final color WHITE = color(255); 
  final color BLACK = color(0); 
  final color GRAY  = color(255/2); 

  // ----------------------------------------------------------------

  void showTable(Table tableBtn, 
    int x, int y) { 

    int factorX=78; // column width 

    // rect
    stroke(WHITE);
    noFill();
    rect( x, y, 
      tableBtn.getColumnCount()*78-6, (tableBtn.getRowCount()+1) * 22 + 10 );

    // headline 
    showTableHeadline(tableBtn, x+6, y+19, factorX);

    // horizontal line 
    stroke(WHITE);
    line( x+2, y+5+19, 
      6+x+(tableBtn.getColumnCount())*factorX-13, y+5+19);

    // grid 
    // loop over rows (y)
    for (int i=0; i<tableBtn.getRowCount(); i++) {

      // current data row
      TableRow row = tableBtn.getRow(i);

      // loop over columns in that row (i2 is for x) 
      for (int i2=0; i2<tableBtn.getColumnCount(); i2++) {

        fill(WHITE);
        text(row.getString(i2), 
          i2*factorX+x+6, 25+ i * 22 +y+8, 
          factorX-8, 15);

        if (mouseInside(i2*factorX+x+6, 25+ i * 22 +y+8, 
          factorX-8, 15)) {
          text (row.getString(i2), 
            20, height-22);
        }//if

        // vertical line 
        line( i2*factorX+x, +y, 
          i2*factorX+x, tableBtn.getRowCount() * 22 + y + 31);
      }//for
    }//for
  }// method 

  boolean mouseInside( float x_, float y_, 
    float w_, float h_) {
    return mouseX>x_ &&
      mouseX<x_+w_ &&
      mouseY>y_ &&
      mouseY<y_+h_;
  }

  void showTableHeadline(Table tableBtn, 
    int x, int y, 
    int factorX) { 
    // headline for table 
    TableRow row0 = tableBtn.getRow(0);
    for (int i=0; i<tableBtn.getColumnCount(); i++) {
      // headline 
      fill(GREEN);
      text(row0.getColumnTitle(i), 
        i*factorX+x, y-2);
    }
  }//method 

  // ---
  // make table 

  Table newTable (String... listColumnNames) { 

    Table newT = new Table();

    // make columns
    for (String s1 : listColumnNames) {
      newT.addColumn(s1);
    }

    return newT;
  }//method

  Table tableAddData(  Table table1, String... data1  ) {

    // add rows with data 
    TableRow newRow = table1.addRow();

    // add rows with data 
    int i=0; 
    for (String s1 : data1) {
      newRow.setString(newRow.getColumnTitle(i), s1);
      i++;
    }
    return table1;
  }//method

  Table tableAddDataFloat (  Table table1, float... data1  ) {

    // add rows with data 
    TableRow newRow = table1.addRow();

    // add rows with data 
    int i=0; 
    for (float f1 : data1) {
      newRow.setFloat(newRow.getColumnTitle(i), f1);
      i++;
    }
    return table1;
  }//method

  Table tableAddDataInt(  Table table1, int... data1  ) {

    // add rows with data 
    TableRow newRow = table1.addRow();

    // add rows with data 
    int i=0; 
    for (int f1 : data1) {
      newRow.setInt(newRow.getColumnTitle(i), f1);
      i++;
    }
    return table1;
  }//method


  // --- 

  void printlnTable(Table tableBtn) { 

    // rect
    stroke(WHITE);
    noFill();

    println("------------------------------------");

    // headline 
    printlnTableHeadline(tableBtn);

    // grid 
    // loop over rows (y)
    for (int i=0; i<tableBtn.getRowCount(); i++) {

      // current data row
      TableRow row = tableBtn.getRow(i);

      // loop over columns in that row (i2 is for x)
      String s1="";
      for (int i2=0; i2<tableBtn.getColumnCount(); i2++) {
        s1+="   "+row.getString(i2);
        //
      }//for
      println(s1);
    }//for
    println("===========================================");
    //
  } // method 

  void printlnTableHeadline(Table tableBtn ) { 
    // headline for table 
    TableRow row0 = tableBtn.getRow(0);
    for (int i=0; i<tableBtn.getColumnCount(); i++) {
      // headline 
      print("   "+row0.getColumnTitle(i));
    }
    println("");
  }//method 
  //
}//class
//
2 Likes

I answered during you wrote your post

See above

1 Like

Apologies, I did not see the update before posting. Thank you both!

1 Like