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.
IndexProgramming Questions & HelpPrograms › Help please
Page Index Toggle Pages: 1
Help please (Read 840 times)
Help please
Nov 16th, 2008, 8:36pm
 
Hello!
I am taking my masters couse in multimedia design.
I have a programing subject for the first time and in it we are using Processing.
I have a work to hand in tommorow and i need some help to figure out code for my work.

I have something done already but i need to animate it somehow.
If someone could be so kind as to help me figure out a way to animate it I would be very grateful.

Thank you!
Re: Help please
Reply #1 - Nov 16th, 2008, 9:44pm
 
What are you trying to animate? Post some code, or at least rough ideas, and we can give you some tips.

ie. a ball bouncing around, or wormie thingies swimming around? or even a box changing color?

btw.. the learning-section on this site has a lot of sketches like that

best regards,
seltar
Re: Help please
Reply #2 - Nov 16th, 2008, 10:47pm
 
Hello!
I have this code so far:

final int NLINHAS = 80; //number of lines
final int COMPINI = 10; //initial length
final int VARCOMP = 20; //length variation
final int LARG = 300;;
final int ALTU = 300;
//caculate the center coordenates
final int XC = LARG / 2;
final int YC = ALTU/ 2;
int xFim, yFim; //line extremity coordenates
int comp = COMPINI; //line length
int varComp = VARCOMP; //length variation
float angulo = 0; //current angle
float varang = (float)(2 * PI / NLINHAS); //calculate the angle increment in radians

void setup() {
 background(255);
 size (LARG, ALTU);
 stroke (100);
 strokeWeight (1);
 noLoop();
 frameRate (20);
 }
 
void draw() {
 //draw the lines
 for (int linha=0; linha < NLINHAS; linha++){
   //calculate the end of the line (xFim, yFim)
   xFim = XC + (int)(comp * cos(angulo));
   yFim = YC - (int)(comp * sin(angulo));
   //verify if (xFim, yFim) is outside of the applet or if the length is negative
   //if it is reduce the length, then the
   //length decreses amd calculate the new point
   if (xFim>LARG || yFim>ALTU || xFim<0 || yFim<0 || comp <0) {
varComp *= -1;
comp += 2*varComp;
xFim = XC + (int)(comp * cos(angulo));
yFim = YC - (int)(comp * sin(angulo));
   }
   //draw the line
   line(XC, YC, xFim, yFim);
   //update the length
   comp += varComp;
   //update the angle
   angulo += varang;
   }
}

My problem is that i want my animation to start with just 1 line. I want the number of lines to be added by 1. So first it will show 1 line then 2 then 3 then 4 and so on.
I think you have to use the line number variable (NLINHAS) with "++" but I'm not sure, and I wouldn't know in what cycle to put it in.
I also want the lines to have a random stroke density.



Thank you so much!!!
Re: Help please
Reply #3 - Nov 17th, 2008, 12:04am
 
Something like this
I've marked most of my lines with ***, and comments

Code:

final int NLINHAS = 80; //number of lines
final int COMPINI = 10; //initial length
final int VARCOMP = 20; //length variation
final int LARG = 300;
final int ALTU = 300;
//caculate the center coordenates
final int XC = LARG / 2;
final int YC = ALTU/ 2;


// NEW STUFF - START ***
int cLine = 0; // The current line
int timeBetweenAddedLines = 30; // milliseconds;
int lastLineTime = 0; // time last line was added
float[] strokes; // array of strokeWeights
float minStroke = 1; // minimum strokeweight if randomStrokes = true
float maxStroke = 5; // maxiumu strokeweight if randomStrokes = true
float distDiv = 50; // Distance divider;
boolean randomStrokes = false; // set to true if you want random strokeweights between minStroke and maxStroke. when set to false, it uses length of the line, divided by distDiv
// NEW STUFF - END ***

void setup() {
background(255);
size (LARG, ALTU);
stroke (100);
strokeWeight (1);
smooth(); // to get that nice antialiasing
// noLoop(); *** commented out this line, because it forces the draw to only run once
frameRate (20);
strokes = new float[NLINHAS]; // ***
}

void draw() {
// initialize the variables every frame
int xFim, yFim; //line extremity coordenates
int comp = COMPINI; //line length
int varComp = VARCOMP; //length variation
float angulo = 0; //current angle
float varang = (float)(2 * PI / NLINHAS); //calculate the angle increment in radians

// clear background ***
background(255); // ***

// increment lines ***
if(cLine < NLINHAS && lastLineTime+timeBetweenAddedLines < millis()) // ***
{ // ***
strokes[cLine] = random(minStroke,maxStroke); // ***
cLine++; // Increment one to start with, or else, it won't draw ***
lastLineTime = millis(); // ***
} // ***

//draw the lines
for (int linha=0; linha < cLine; linha++){
//calculate the end of the line (xFim, yFim)
xFim = XC + (int)(comp * cos(angulo));
yFim = YC - (int)(comp * sin(angulo));
//verify if (xFim, yFim) is outside of the applet or if the length is negative
//if it is reduce the length, then the
//length decreses amd calculate the new point
if (xFim>LARG || yFim>ALTU || xFim<0 || yFim<0 || comp <0) {
varComp *= -1;
comp += 2*varComp;
xFim = XC + (int)(comp * cos(angulo));
yFim = YC - (int)(comp * sin(angulo));
}
// set the strokeWeight ***
if(randomStrokes){
strokeWeight(strokes[linha]); // ***
}else{
strokeWeight(dist(XC, YC, xFim, yFim)/distDiv); // ***
}
//draw the line
line(XC, YC, xFim, yFim);
//update the length
comp += varComp;
//update the angle
angulo += varang;
}
}



best regards,

seltar
Re: Help please
Reply #4 - Nov 17th, 2008, 12:18am
 
Wow!
I don't know how to thank you!
You've added some things we still haven't learnt yet!!!
But thank you so much!
The worse part is now I have to present this in class and I need to explain to the teacher what I did!!! hehehe

You're the best!
Keep well!
=)
Re: Help please
Reply #5 - Nov 17th, 2008, 1:45am
 
Hello seltar!
I have some questions on the code you kindly sent me.
If you have time or patience to answer them I would apreciate it. If not that is ok too.
Again thank you for you help.
Sincerely Samantha



How come you moved these variables inside void draw?:
int xFim, yFim;
int comp = COMPINI;
int varComp = VARCOMP;
float varang = (float)(2 * PI / NLINHAS);



Could you explain this code to me please, what purpose does it serve?:
int cLine = 0;
int timeBetweenAddedLines = 30;
int lastLineTime = 0;
float[] strokes;
float minStroke = 1;  
float maxStroke = 5;
float distDiv = 50;  
boolean randomStrokes = false;



Why did you add this in void setup, what is it for?:
strokes = new float[NLINHAS];




Could you explain this code to me please, what purpose does it serve?:
if(cLine < NLINHAS && lastLineTime+timeBetweenAddedLines < millis())
 {
   strokes[cLine] = random(minStroke,maxStroke);
   cLine++;  
   lastLineTime = millis();  
 }



In void draw, how come you changed this:
  for (int linha=0; linha < NLINHAS; linha++){
to this:
  for (int linha=0; linha < cLine; linha++){?



What purpose does this serve?:
if(randomStrokes){
strokeWeight(strokes[linha]);  
   }else{
strokeWeight(dist(XC, YC, xFim, yFim)/distDiv);  
   }

Re: Help please
Reply #6 - Nov 17th, 2008, 1:48am
 
forgot this question...



How come you put a background in void draw if there was already one in void setup?
Re: Help please
Reply #7 - Nov 17th, 2008, 9:09am
 
Hey Smiley

1. I moved the variables inside draw, so that they would have the correct values each time draw is run. (20 times per second)

2. That code just sets up some variables, where you can change the numbers to get different results.
If you read the comments on the end of each line, it should tell you what it does. Try changning timeBetweenAddedLines to 150
And set distDiv to 80. Play with the numbers Smiley

3. strokes = new float[NLINHAS]; this is an initialization of an array. an array is a list of a certain variable type, instead of just one variable.

4. That section checks to see if cLine is less than NLINHAS, and that the timeBetweenAddedLines has passed since the last time the code added a line.
It makes sure the time works in other words..
Also, it sets a random stroke for that current line
then i add 1 to cLine, so that it increments towards NLINHAS
and then I update the time, so I can check when to add the next line.


5. NLINHAS is the number of lines you want. cLine is the current number of lines to be drawn.

6. It only serves the purpose for you to have a choice..
you can keep either one of them
the one inside if(randomStrokes) makes the strokeWeight random for each line.
the other one makes each line use strokeWeight determined by the length of the line


7. And the background can be added wherever you want, but if you add it to draw, it clears the screen every frame. if you add it to setup, it only clears the screen once

hope this answers some of your questions Smiley

best regards,
seltar

Page Index Toggle Pages: 1