Why does this keep looping?

edited July 2015 in Questions about Code

Beginner here. Not sure whey this keeps looping. Shouldn't it execute the loop and then stop when it reaches the end of the array?

float [] sales;
float [] time;

float [] x;
float [] y;

float maxSales, maxTime;


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


  String [] lines = loadStrings("Point_Class_Data.txt");

  sales = new float[lines.length];
  time = new float[lines.length];

  for (int i = 0; i <lines.length; i ++) {
    String [] pieces = split(lines[i], "\t");

    sales[i] = float(pieces[3]);
    time[i] = float(pieces[0]);
    }


  maxSales = max(sales);
  maxTime = max(time);  

}

void draw() {
  background(255);

  x = new float [sales.length];
  y = new float [sales.length];
    for (int i = 0; i <sales.length; i++) {
    y[i] = map(sales[i], 0, maxSales, height, 0);
    x[i] = map(time[i], 0, maxTime, 0, width);
    noStroke();

    if(time[i] == 125) {
    fill(34,237,45);
    ellipse(x[i], y[i], 25, 25);      

    } else {
    fill(181, 56, 91,50);
    ellipse(x[i], y[i], 25, 25);
    }

    stroke(230,230,230);
    line(width/2, height/2, x[i], y[i]);  
}
}

Answers

  • edited July 2015 Answer ✓

    Are you getting an error message? The for() loops both look reasonable. Are you asking about the fact that draw() keeps looping? That's what it does, 60 times/sec. To prevent that and run draw() just once you can put noLoop() in the setup() function.

  • Yes, I was playing with noLoop(). Why I didn't use is because I want to add additional interactive elements to the sketch.

  • you can restart with loop after using noLoop when something interactive is meant to happen

Sign In or Register to comment.