Delayed drawing techniques

edited October 2013 in Questions about Code

I'm working on a project that builds a cutsheet for laser cutting objects based on audio analysis. A delay is necessary to allow sampling different parts of the audio.

So far, I've managed to get it working through one line, but I want it to work with any amount of rows/columns

I've posted the stripped down version ofmy code, which works for drawing a simple object at pre-set coordinates along one line. For some reason, it's not working through the y position array, though.

Anyone have an idea that might make this work?

int counter=0;
int grid=10;

int[] x = new int[grid];
int[] y = new int[grid];

int savedTime;

void setup() {

  size(1200, 1200);
  background(0);
  smooth();

  //initialize grid coordinates               
  for (int i=0;i<grid;i++) {
    x[i] = i*100;
    y[i] = i*100;
  }

  savedTime = millis(); //save current time
} 


void draw() {

  println(savedTime); 
  int line=0; //increment to adjust y position

  //iterate through coordinate arrays
  for (int i = 0; i <  counter; i++) {
    ellipse(x[i], y[line], 50, 50);

  } // for

  //draw based on time delay
  if (millis ()- savedTime >= 500) {
    savedTime = millis();
    counter++;

      //increase y values when end of line is reached
      if (counter>=grid){
        counter=0; // edited 
        line++;
      }
    } // if

} // func 

Answers

  • _vk_vk
    edited October 2013

    Hi you are reseting line to 0 every draw() loop in line 28 . Declare line as a global var and delete that line.

    int counter=0;
    int grid=10;
    
    int[] x = new int[grid];
    int[] y = new int[grid];
    
    int savedTime;
    

    int line = 0;//here

    void setup() {
    
      size(1200, 1200);
      background(0);
      smooth();
     //not here
    
    //initialize grid coordinates               
      for (int i=0;i<grid;i++) {
        x[i] = i*100;
        y[i] = i*100;
      }
    
      savedTime = millis(); //save current time
    } 
    
    
    void draw() {
    
      println(savedTime); 
       //increment to adjust y position
    
      //iterate through coordinate arrays
      for (int i = 0; i <  counter; i++) {
        ellipse(x[i], y[line], 50, 50);
    
      } // for
    
      //draw based on time delay
      if (millis ()- savedTime >= 500) {
        savedTime = millis();
        counter++;
    
          //increase y values when end of line is reached
          if (counter>=grid){
            counter=0; // edited 
            line++;
          }
        } // if
    
    } // func 
    
  • Ah I knew it was something simple. Thanks so much!

Sign In or Register to comment.