for loop add atribute,to every 3

edited August 2016 in Programming Questions

is there any way of adding the same atribute to every 3 or whetever number with a for loop?

Tagged:

Answers

  • Check if the remainder of the division of the iterator by 3 is 0:
    https://Processing.org/reference/modulo.html

    for (int i = 0; i < 10; ++i) {
      if (i % 3 == 0) {
      }
    }
    
  • i didnt meant that.i meant,if we have a legth of array that is divides by three,the first three eg[0,1,3] put the same atribute,then[4,5,6],an other atribute and so on...

  • edited August 2016

    Here is an example. Adjust the size and multiple and you will see it will do what you want. The challenge for you is, can you do the same using less lines?

    Kf

    void setup() {
    
      size(400, 400);
      noLoop();
    }
    
    void draw() {
      int multiple=5;
      int myObjSize=23;
      int[] x = new int[myObjSize];
    
      for (int i = 0; i < myObjSize; i=i+multiple) {  //Access each subgroup
        for (int j=0; j<multiple; j++) {              //Access each item per subgroup
          if (i+j < myObjSize) {                      //In case "multiple" is not multiple of myObjSize aka. check for within bounds
            //############################
    
            x[i+j]=i;  //HERE all items in same subgroup will have same value
    
            //############################
          }
        }
      }
    
    
      //Now print result
      for (int i = 0; i < myObjSize; i++)
        println((i)+"\t"+x[i]);
    }
    
  • hey,i dont understand it complete,maybe you can help me I have this code,I take the vertices,in a PVector array.I assume that the PVectors are in such a row that correspond to faces.The faces are triangles,that is 3 Pvectors each face.I want to put the same movement to every 3 Pvectors,so[0,1,2] one movement,[3,4,5] an other movement.

     import peasy.*;
    import saito.objloader.*;
    
    
    //PrintWriter output;
    OBJModel model ;
    OBJModel Smodel ;
    OBJModel tmpmodel ;
    
    PeasyCam cam;
    
    float easing = 0.005;
    float r;
    float k =0.00001;
    int VertCount;
    PVector[] Verts;
    PVector[] locas;
    PVector Mouse;
    
    void setup()
    {
      size(800, 800, P3D);
      frameRate(30);
      noStroke();
    
      model = new OBJModel(this, "Model2.obj", "absolute", TRIANGLES);
      model.enableDebug();
      model.scale(150);
      model.translateToCenter();
    
    
      Smodel = new OBJModel(this, "Model2.obj", "absolute", TRIANGLES);
      Smodel.enableDebug();
      Smodel.scale(150);
      Smodel.translateToCenter();
    
    
      tmpmodel = new OBJModel(this, "Model2.obj", "absolute", TRIANGLES);
      tmpmodel.enableDebug();
      tmpmodel.scale(150);
      tmpmodel.translateToCenter();
    
      //output = createWriter("positions.txt"); 
    
      cam = new PeasyCam(this, width/2, height/2, 0, 994);
    }
    
    
    
    void draw()
    {
      background(129);
      lights();
    
      int VertCount = model.getVertexCount ();
       Verts = new PVector[VertCount];
       locas = new PVector[VertCount];
      float r =500;
      PVector Mouse = new PVector(mouseX-width/2, mouseY-height/2, 0);
    
    
      cam.setMouseControlled(false);
    
    
    
    
    
    
    
    
    
    
      //println(frameCount);
      pushMatrix();
      translate(width/2, height/2, 0);
    
    
    
      for (int i = 0; i < VertCount; i++) {
        //PVector orgv = model.getVertex(i);
    
        locas[i]= model.getVertex(i);
        Verts[i]= Smodel.getVertex(i);
    
    
        //PVector tmpv = new PVector();
    
    
        if (frameCount> 100) {
    
    
    
          float randX = randomGaussian();
          float randY = randomGaussian();
          float randZ = randomGaussian();
    
          PVector Ran = new PVector(randX, randY, randZ);
    
          //float norX = abs(cos(k)) * randX;
          //float norY = abs(cos(k)) * randY;
          //float norZ = abs(cos(k)) * randZ;
    
    
    
    
    
    
    
    
    
          if (Verts[i].y > Mouse.y  - r/2 && Verts[i].y < Mouse.y  + r/2 && Verts[i].x > Mouse.x  - r/2 && Verts[i].x < Mouse.x  + r/2 && Verts[i].z > Mouse.z  - r/2 && Verts[i].z <  Mouse.z  + r/2) {
            tmpmodel.setVertex(i, locas[i].x, locas[i].y, locas[i].z);
          } else {   
    
    
    
            Verts[i].x+=Ran.x;
            Verts[i].y+=Ran.y;
            Verts[i].z+=Ran.z;
    
            if (Verts[i].x > width/2 || Verts[i].x < -width/2) {
              Verts[i].x+=-Ran.x;
            }
            if (Verts[i].y > height/2 || Verts[i].y < -height/2) {
              Verts[i].y+=-Ran.y;
            }
            if (Verts[i].z < -800/2 || Verts[i].z > 800/2) {  
              Verts[i].z+=-Ran.z;
            }
            tmpmodel.setVertex(i, Verts[i].x, Verts[i].y, Verts[i].z);
          }
          k+=0.0001;
        }
        // output.println("Verts " + Verts[i] + " locas " +locas[i]);
      }
    
      pushMatrix();
      translate(Mouse.x, Mouse.y, Mouse.z);
      noFill();
      stroke(255);
      box(r);
      popMatrix();
    
    
      noStroke();
    
      tmpmodel.draw();
    
      popMatrix();
    
    
    
      pushMatrix();
      translate(width/2, height/2, 0);
      noFill();
      stroke(255);
      box(width, height, 600);
      popMatrix();
      //output.flush(); // Writes the remaining data to the file
      //output.close(); // Finishes the file
      //saveFrame(); 
    }
    
    void keyPressed() {
      if (keyCode == UP) {
        setup();
      }
      if (keyCode == LEFT) {
        r+=10;
      }
      if (keyCode == RIGHT) {
        r-=10;
      }
    }
    
  • maybe as i understand it,after putting your code in the lines 117-119 Verts[i].x=Verts[x[i]].x+RanX and so on

Sign In or Register to comment.