We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
for() loop? (Read 1379 times)
for() loop?
Oct 15th, 2009, 11:34am
 
I'm using the following code to create a band of lines from a set of numbers, but currently all lines show up at once, how can I make the function create each line in turn (or just more slowly) so I can watch it happening? Thanks!


for(int row = 1; row < rowCount;) {
 String timedata = dataTable.getString(row, 0); // column 1.
 
 String fulltime = (timedata.substring(1,5));
 
 String daytime = dataTable.getString(row, 1); // column 2.
 
 String kind = dataTable.getString(row, 2);//column3.
 
 String hourx = (timedata.substring(1,3));//from character 2-3.
 
 String minx = (timedata.substring(3,5));//characters 4-5.
 
 
 int n = int(hourx);
 float m = map(n, 0, 24, 10, width-10);
 stroke(100,0,0);
 line(m, (((height/3)*2)+30), m, (((height/3)*2)+20));

 int nn = int(minx);
 float mm = map(nn, 0, 59, 10, width-10);
 stroke(0,100,0);
 line( mm, (((height/3)*2)+20), mm, (((height/3)*2)+10));
 
 stroke(0,100,0);
 noFill();
 ellipse(width/2, (height/2)-10, m/2, m/2);
Re: for() loop?
Reply #1 - Oct 15th, 2009, 12:09pm
 
Make 'row' a global variable, increment it at end of draw().
When it reaches some milestones (the moments you want to go to next step), draw the next line.
Re: for() loop?
Reply #2 - Oct 16th, 2009, 6:03am
 
I've tried moving the row variable to the setup() section, which seems to work, at least I end up with the same image if I add row = row++ at the end. but I think I am doing somethign wrong in trying to add a mouse event to control 'row'.

When I use the mousepressed function (highlighted) I only see the first 'frame', and cant make it add  1 to 'row' on click...? Make any sense!?



PImage blueImage;
Table dataTable;
int rowCount;
PFont textfont;
int row;

                   

void setup(){
 size (400, 200);
 
 textfont = loadFont("Serif-48.vlw");
 textFont(textfont, 32);
 // blueImage = loadImage("blueImage.png");
 dataTable = new Table("testnums2.txt");
 rowCount = dataTable.getRowCount();
 //frameRate(1);
}
 
 
 
 
void draw() {
 background(0);
// image(blueImage, 0, 0);
 smooth();
 fill(192,0,0);
 noStroke();


fill(0, 100, 0);
 text("Mins", 1, (height/2)-5);  //text lables.
fill(100, 0, 0);
 text("Hours", 1, (height/2)+45);


for(int row = 1; row < rowCount;) {
 String timedata = dataTable.getString(row, 0); // column 1.
 
 String fulltime = (timedata.substring(1,5));
 
 String daytime = dataTable.getString(row, 1); // column 2.
 
 String kind = dataTable.getString(row, 2);//column3.
 
 String hourx = (timedata.substring(1,3));//from character 2-3.
 
 String minx = (timedata.substring(3,5));//characters 4-5.
 
 
 int n = int(hourx);
 float m = map(n, 0, 24, 10, width-10);
 stroke(100,0,0);
 line(m, (height/2)+50, m, (height/2)+60); // HOUR LINES ARE RED.

 int nn = int(minx);
 float mm = map(nn, 0, 59, 10, width-10);// min lines are green.
 stroke(0,100,0);
 line( mm, (height/2), mm, (height/2)+10);
 
 stroke(0,0,100);
 noFill();
 ellipse(width/2, (height/5), m/5, m/5); //   ellipse.
 

 int h = hour();
 float hourmark = map(h, 0, 24, 10, width-10);
noStroke();
 fill(255, 215, 0, 50);
 smooth();
 ellipse(hourmark, (height/2)+55, 10, 10); //hour counter.

 int mint = minute();
 float minmark = map(mint, 0, 60, 10, width-10);
 noStroke();
 fill(255, 215, 0, 50);
 smooth();
 ellipse(minmark, ((height/2)+5), 5, 5); // min counter.
 
  int sec = second();
 float secmark = map(sec, 0, 60, 10, width-10);
 noStroke();
 fill(255, 215, 0, 50);
 smooth();
 ellipse(secmark, (height-10), 5, 5); // second counter.


 void mousePressed() {
      row++;
}



 println(row);
 println(timedata);

 
}



}
Re: for() loop?
Reply #3 - Oct 16th, 2009, 6:23am
 
You cannot put a function inside another function.
And the idea was to increment row on external events (I was thinking of time reaching a value, a mouse click or key press is fine too), so removing the for loop.
And perhaps do something like:
if (row > step1)
 DrawLine1();
if (row > step2)
 DrawLine2();

making the lines to appear progressively.
Well, if I understood correctly the initial problem.
Re: for() loop?
Reply #4 - Oct 17th, 2009, 1:52pm
 
Im not sure I understand whether that ould work or not, maybe if I explain more thoroughly: I'm using the getString function to read lines from a text file, and then the value from the first column of each row is converted into a point between '10, width-10)'. So to do it with the mouse do i need to make 'row' increase whenever mouse is clicked?

If you could post an example (however simple) that I could try out i would appreciate it!
Re: for() loop?
Reply #5 - Oct 18th, 2009, 2:32am
 
OK, I understand better.
Make a global variable named, for example, currentRowCount, initialized to 0.
Change the loop: for(int row = 1; row < rowCount;) to: for (int row = 1; row < currentRowCount; row++) and increment currentRowCount on mouse click.
It should work...
Re: for() loop?
Reply #6 - Oct 18th, 2009, 3:46am
 
Wicked I'll try to get that working today!
Re: for() loop?
Reply #7 - Oct 18th, 2009, 9:03am
 
Perfect thanks!
Re: for() loop?
Reply #8 - Oct 20th, 2009, 3:00am
 
Is there a way of making a variable that remembers the 'previous' value of the string ? eg
x=1

x=2
previous=1

x=3
previous = 2 etc..
Re: for() loop?
Reply #9 - Oct 20th, 2009, 3:28am
 
Simple enough: Define a variable before starting the loop and assign it the current value before closing each iteration:

Code:
String lastImage = "";

for (int i=0; i<someLength; i++) {
 println(lastImage);
 String filename = "image" + i + ".jpg";
 loadImage(filename);
 
 // store the last value:
 lastImage = filename;
}
Page Index Toggle Pages: 1