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
Code (Read 436 times)
Code
Nov 23rd, 2008, 10:06pm
 
Hello!
I need some help with some code please!
I would like someone to comment on my code so I may correct it. Comment on anything I should have done differently or organized my code differently or if I have any useless code, anything!
If someone could please help I would apreciate it very much!
I actually need this with a little urgency!

Thank you!
Samantha


//variables//
final int FOLHAS = 10; // Number of leaves
final int COMPINI = 20; //Initial length
final int VARCOMP = 40; //Length variation
final int DIVCOMP = 20; //Length Division
final int LARG = 600; //width of the window
final int ALTU = 600; //hight of the window
//Calculates the center coordenates
final int XC = LARG / 2;
final int YC = ALTU / 2;
int xInicio = XC; //changes the initialx in the animation
int yInicio = YC; //changes the initialy in the animation
int xFim, yFim; //Line extremities coordenates
float comp = COMPINI; //Minimum line length
int varComp = VARCOMP; //Length variation
float angulo = 0; //Current angle
float varang;// = (float) (2 * PI / NLINHAS); //Calculates the angle increment in radians
color [] cores = new int[FOLHAS]; // saves the colours used for each leaf
int posFolha = 0; //indicates the leaf that is being drawn
int varCompInicio = 0; //length used to calculate the intial point of each line in the animation
boolean crescente = true; //indicates when the initial point is growing or not
boolean crescer = false; //indicates when the animation efects are executing or not
boolean rodar = false; //indicates if it is rotating or not
boolean explodir = false; //indicates if its exploding or not
float incAng = 0; //rotate the drawing
int nlinhasActuais = 0; //saves the number of lines that were drawn
int nlinhasJanela = 0; //counts how many vertical lines will be drawn on the screen
int xIniLinha; //saves the x of the initial and final point of the random lines
int yIniLinha; //saves the y of the initial and final point of the random lines
float angLinha; //saves the angle of the random lines
boolean finalFolhasAnimacao = false; //indicates that the leaf animations terminate
boolean finalLinhasAnimacao = false; //indicates that the line animations terminate
boolean ecraLimpoH = false; //terminates de horizontal clear
boolean ecraLimpoV = false; //terminates de vertical clear
boolean finalLinhasHAnimacao = false; //indicates that the horizontal animation of the vertical lines terminates
boolean finalAnimacao = false; //indicates that the animation terminates
boolean pararAnimacao = false; //indicates that the animation stops when clicking the mouse
Re: Code
Reply #1 - Nov 23rd, 2008, 10:06pm
 
continued code...


void setup() {
   background(255);
   size(LARG, ALTU);
   strokeWeight(2);
   for (int i = 0; i < cores.length; i++) { //colour table that randomly aplies colours to the leaves
       cores[i] = color(random(255), random(255), 0);
   }

   smooth();

   frameRate(50); //animation speed
}

void draw() {
   // Depends on the animations that are executed and if the reaches its end
   // enters in a certain animation
   if (!finalFolhasAnimacao) {
       reiniciaVariaveis(true);
       desenhaFolhas();
   } else if (!finalLinhasAnimacao && (nlinhasJanela < nlinhasActuais)) {
       reiniciaVariaveis(false);
       desenhaLinhas();
   } else if(nlinhasJanela == nlinhasActuais){
       // Initializes again some variables so the previous animations wont be executed
       finalLinhasAnimacao = true;
       explodir = false;
       nlinhasJanela = 0;
       nlinhasActuais = 1; //Must have a different value from nlinhasJanela or else it would always execute this condition
   } else if(!ecraLimpoH) {
       limpaEcra(true);
   }/* else if (!finalLinhasHAnimacao){
       desenhaLinhasRectas();
   }*/ else if(!ecraLimpoV) {
       limpaEcra(false);
       //Prepares the variables to be used in the next animation (final leaves)
       strokeWeight(1);
       incAng = 0;
       finalAnimacao = true;
   } else {
       reiniciaVariaveis(true);
       desenhaFolhas();
   }
}

void mouseReleased() {
   //Stops or resumes the leaves animation when the mouse is clicked
   if (pararAnimacao) {
       pararAnimacao = false;
   } else {
       pararAnimacao = true;
   }
}
Re: Code
Reply #2 - Nov 23rd, 2008, 10:07pm
 
continued code...


//restarts the animation variables
void reiniciaVariaveis(boolean limpa) {
   if(posFolha < FOLHAS) { //if not enough leaves were drawn more are created
       nlinhasActuais++;
       varang = (float) (2 * PI / nlinhasActuais); //and execute the angle between lines update
   }
   angulo = 0; //restarts the animation angle
   if (rodar && !finalAnimacao) { //Changes the angle to rotate the leaves. If it is on the last animation the dont rotate.
       angulo += incAng;
       incAng -= radians(1);
   }

   if (abs(incAng) > PI) { //When rotated 180 degrees it explodes
       explodir = true;
   }
   comp = COMPINI;
   varComp = VARCOMP;
   if(limpa && !pararAnimacao) { //Clears the screen and puts posFolha at 0 to reuse the colours from the start
       posFolha = 0;
       background(255);
   }
}

//Drws the various leaves and executes the animation
void desenhaFolhas() {
   stroke(cores[posFolha % FOLHAS]); // garanties that a position will always be read that exists in the colour table
   boolean terminar = true; //indicates when the screen is clear and the fist animation terminates
   float compCrescer; //saves the length used to calculate the initial point of each line in the animation
   //Draws the lines
   for (int linha = 0; linha < nlinhasActuais; linha++) {
       //Calculates the end point of the line (xFim, yFim)
       xFim = XC + (int) (comp * cos(angulo));
       yFim = YC - (int) (comp * sin(angulo));

       //Verifies if (xFim, yFim) are outside of the applet or if the length is negative
       //If it is, reduces the length, goes to
       //descending length and calculates new point
       if (xFim > LARG || yFim > ALTU || xFim < 0 || yFim < 0 || comp < 0) {
           if (comp < 0) { //In this situation because a leaf was drawn
               posFolha++;
               if (posFolha == FOLHAS) { //When all the leaves are drawn, the grow and rotate
                   rodar = true;
                   crescer = true;
                   break;
               }
               stroke(cores[posFolha % FOLHAS]); //Defines de line colour
           }

           varComp *= -1;
           comp += 2 * varComp;
           xFim = XC + (int) (comp * cos(angulo));
           yFim = YC - (int) (comp * sin(angulo));
       }

       compCrescer = (comp / DIVCOMP) * varCompInicio; //Used to calculate the initial point of each line

       if (compCrescer >= comp) { //If compCrescer is equal to comp indicates that the initial point will
           crescente = false;     //match the final one and then it will start decreasing
       } else if (compCrescer <= 0 ) { //If compCrescer is smaller or equal to zero it will start growing
           crescente = true;
       }

       if (crescer && !pararAnimacao) { //Calculates the initial point diferent from the center of the screen to give the effect that the
                                        //lines will be increasing and decreasing
           xInicio = XC + (int) (compCrescer * cos(angulo));
           yInicio = YC - (int) (compCrescer * sin(angulo));
           if (explodir && !finalAnimacao) { // Explodes. Makes the final point be calculated in relation to the initial point
                                             //and not in relation to the center of the screen so the lines can gradually disapear,
                                             //the initial point already automaticly always increases
               xFim = xInicio + (int) (comp * cos(angulo));
               yFim = yInicio - (int) (comp * sin(angulo));
               varCompInicio++; //Makes the explosion faster
           }
       } else { //In case the lines arent going to increase and decrease, the initial point will always be the center of the screen
           xInicio = XC;
           yInicio = YC;
       }

       if(pararAnimacao && finalAnimacao) { //In case the animation is stopped and its already on the last animation
                                            //move the leaves to the location of the mouse
           xInicio = mouseX;
           yInicio = mouseY;
           xFim = xFim - XC + mouseX; //End points of the calculated lines based on the mouse point and not in the center
           yFim = yFim - YC + mouseY;
       }
       //Draws the line
       line(xInicio, yInicio, xFim, yFim);
       //Updates the length
       comp += varComp;
       //Updates the angle
       angulo += varang;

       //Used to know when there arent any lines on the screen
       if(dentroJanela(xInicio, yInicio) || dentroJanela(xFim, yFim)) {
           terminar = false;
       }
   }
   //Makes the initial points grow or decrease (if they are getting close or far away from the final ones)
   if(crescente) {
       varCompInicio++;
   } else {
       varCompInicio--;
   }

   if(terminar) { //If there doesnt exist any line on the screen, terminates the animation
       finalFolhasAnimacao = true;
   }
}

//Used to verify if a determined point is in the boundries of the window or not
boolean dentroJanela(int x, int y) {
   if (x < 0 || y < 0 || x > LARG || y > ALTU) {
       return false;
   }

   return true;
}
Re: Code
Reply #3 - Nov 23rd, 2008, 10:08pm
 
continued code...


//Draws random lines
void desenhaLinhas() {
   stroke(cores[posFolha % FOLHAS]);

   //In case a line has already exceeded the screen boundries a new point must be calculated
   if(xFim < 0 || yFim < 0 || xFim > LARG || yFim > ALTU || posFolha == 0) {
       calculaNovoPonto();
   }

   xFim = xIniLinha + (int) (varCompInicio * cos(angLinha));
   yFim = yIniLinha - (int) (varCompInicio * sin(angLinha));

   line(xIniLinha, yIniLinha, xFim, yFim);
   varCompInicio += 200; //Makes the animation slower or faster depending if the final point
                         //is calculated based on this length
}

//Calculates a new line entry point
void calculaNovoPonto() {
   //Determines the side the line will apear on the screen
   int lado = (int)random(4);
   if (lado == 4) {
       lado = 3;
   }

   //Calculates a point in the screen
   xIniLinha = (int)random(LARG);
   yIniLinha = (int)random(ALTU);
   //Calculates the line angle
   angLinha = random(PI / 2) + (PI/4);
   varCompInicio = 0;

   switch(lado) {
       case 0: //Top of the screen
           yIniLinha = 0; //Takes away the y component from the point
           angLinha += PI; //Calculates the angle based on the side
           break;
       case 1: //Right side of the screen
           xIniLinha = LARG;
           angLinha += (PI/2);
           break;
       case 2: //Bottom of the screen
           yIniLinha = ALTU;
           break;
       case 3: //Left side of the screen
           xIniLinha = 0; //Takes away the x component from the point
           angLinha -= (PI/2);
           break;
   }
   posFolha++; //Used to read a new colour of the table
   nlinhasJanela++; //Counts the number of lines drawn on the screen
   strokeWeight(nlinhasJanela); //Increases the width of the line
}

//Creates vertical or horizontal lines on the screen
void limpaEcra(boolean horizontal){
   stroke(0);
   strokeWeight(5);
   if(horizontal) { //If its horizontal the y component is 0 to ALTU (height)
       line(nlinhasJanela, 0, nlinhasJanela, ALTU);
   } else { //If its horizontal the x component is 0 to LARG (width)
       line(0, nlinhasJanela, LARG, nlinhasJanela);
   }
   nlinhasJanela += 10; //Draws the next line 10 pixels ahead

   if (horizontal && (nlinhasJanela > LARG)) { //If its horizontal and the lines position has already exceeded the width of the screen
                                               //the animation terminates
       ecraLimpoH = true;
       nlinhasJanela = 0;
   } else if (!horizontal && (nlinhasJanela > ALTU)) { //If its vertical and the lines position has already exceeded the height
                                                       //of the screen the animation terminates
       ecraLimpoV = true;
       nlinhasJanela = 0;
   }
}

/*public void desenhaLinhasRectas() {
   for (int i = 0; i < nlinhasJanela; i++) {
       stroke(cores[i % FOLHAS]);
       strokeWeight(nlinhasJanela - i);
       line(calculaSoma(nlinhasJanela - i), 0, calculaSoma(nlinhasJanela - i), ALTU);
   }
   nlinhasJanela += 2;

   if (nlinhasJanela > LARG) {
       finalLinhasHAnimacao = true;
       nlinhasJanela = 0;
   }
}

public int calculaSoma(int n) {
   int soma = 0;
   for (int i = 0; i < n; i++) {
       soma += i;
   }

   return soma;
}*/
Re: Code
Reply #4 - Nov 24th, 2008, 2:25pm
 
I fear I haven't much time right now to analyse this long code, so  I will just give some general remarks, perhaps coming back later.

Note: this forum isn't well suited to show long code, you might take advantage of snippet sites like http://pastebin.com offering syntax highlighting out of the box and easy copy/paste without loss of formatting.
Do you have any issue on this code or is it working as expected and you only need some coding advices?

On the plus side, the use of final constants, looks like well parametrized, with minimal use of magic numbers in code.
It is well commented.
On the minus side, the Portuguese variable and method names is a bit on the way for people not knowing the language, and is a bit inconsistent with the English comments.
Not sure if it helps in any way, but Processing has TWO_PI and HALF_PI constants.

I like to point out that routines like dentroJanela can be simplified as:

boolean dentroJanela(int x, int y) {
   return !(x < 0 || y < 0 || x > LARG || y > ALTU);
}

OR

boolean dentroJanela(int x, int y) {
   return x >= 0 && y >= 0 && x <= LARG && y <= ALTU;
}


At least, unlike some people, you didn't put the return true in a else... Smiley
Similar remark for mouseReleased()

void mouseReleased() {
   //Stops or resumes the leaves animation when the mouse is clicked
   pararAnimacao = !pararAnimacao;
}

is a classic pattern.
That's all I found with a quick scan of the code.
Re: Code
Reply #5 - Nov 24th, 2008, 5:30pm
 
Thank you for your help PhiLho!
Sorry about the portuguese, but I didn't have time to rename all the variables.
Keep well! =)
Page Index Toggle Pages: 1