Array: Why did I have to take away the argument of the class constructor and substitute PVector ...?

edited November 2013 in Questions about Code

// Dear Forum users, I have a few questions for which I would appreciate a lot your help // Originally I made a class with drawings which would appear one after the other on a number of different locations //of the canvas. To simplify the code to focus on the problems, I left just an ellipse. // From there I wanted to draw the lines uniting the positions where the ellipse are, to draw the trayectory, and only one //line for position. //At the bottom I've put sketch 2 the original, here on top sketch 1 that sort of works but after a series of changes //that i didn't want to apply cause I do need the class to have an argument and use PVectors.... //Could you explain why those changes were needed, and above all how to achieve the line trayectory keeping the class //as it is in sketch 2? Thanks!

//Sketch 1 where the code works after I made changes in the class //the thing is I don't understand why I had to change it and why it didn't work with the sketch as it was in sketch 2

PVector currentPosition;
float scala, rotazione;
color colore;
int numeroOggetti=15;
Disegno[] d =new Disegno[numeroOggetti];


float lastMillis;
int indice=0;
int P,L;

void setup() {

  size(700, 500);
  background(255);
  smooth();

  lastMillis = millis();

  iniziaDisegno();

   P=0;
   L=1;
}


void draw() {
  //background(255);

  if (key=='a') {
    colore= #D8C388;
  }
  if (key=='q') {
    colore= #AF0949;
  }
  fill(colore);

  if (millis() - lastMillis > 100) {

    indice++;


    if (indice>numeroOggetti-1) {
      indice=0;
    }
    lastMillis = millis();


  }

  d[indice].display();
    P++;
              L++;
              if(P>=numeroOggetti-1){P=0;}

              if(L>=numeroOggetti-1){L=0;}
  stroke(255,0,0);
 line(d[P].x, d[P].y, d[L].x, d[L].y);



}



void iniziaDisegno() {

  for (int index = 0; index < d.length; index++) {

    d[index]= new Disegno();
  }
}


void keyPressed() {
  if (key=='z') {
    background(255);

    iniziaDisegno();
  }
}



void drawLines2(){

  for(int i = 0; i < numeroOggetti; i ++) {

           stroke(0);

             line(d[P].x, d[P].y, d[L].x, d[L].y);
              P++;
              L++;
              if(P>=numeroOggetti-1){P=0;}

              if(L>=numeroOggetti-1){L=0;}



  }

}

class Disegno{
  float A,B,a,b;

  PVector pos;
  float x;
  float y;

  //As you'll see I had to change the constructor take away the argument
  //and substitute PVector x with an int x in order for the lines to draw..WHY??????
  //I don't understand it?
  Disegno(){

  x= random(0,width); 
  y= random(0,height); 



  }

  void display(){
    pushMatrix();

      stroke(0);

     // scale(0.6);
      //why do the circles move in the corner of the screen when I usescale even if 
// why if I did put popMatrix and pushMatrix()?

      fill(colore);

      stroke(255);
      ellipse(x, y, 50, 50);
popMatrix();


  }



}

//+++++++++++++++++++HERE Sketch 2 aka the ORIGINAL one+++++++++++++++++ //Notice how I had to change the class, the thing is for my plan I need the class to be like in this one //and I do wanna use PVectors...

PVector currentPosition;
float scala, rotazione;
color colore;
int numeroOggetti=15;
Disegno[] d =new Disegno[numeroOggetti];

int numb=0;//per println
float lastMillis;
int indice=0;
int P,L;

void setup() {

  size(700, 500);
  background(255);
  smooth();

  lastMillis = millis();

  iniziaDisegno();
   drawLines2();
      P=0;
   L=1;
}


void draw() {
  //background(255);

  if (key=='a') {
    colore= #D8C388;
  }
  if (key=='q') {
    colore= #AF0949;
  }
  fill(colore);

  if (millis() - lastMillis > 100) {

    indice++;

    if (indice>numeroOggetti-1) {
      indice=0;
    }
    lastMillis = millis();


  }

  d[indice].display();

  // d[indice].update();

              P++;
              L++;
              if(P>=numeroOggetti-1){P=0;}

              if(L>=numeroOggetti-1){L=0;}
             stroke(255,0,0);
            line(d[P].x, d[P].y, d[L].x, d[L].y);

}



void iniziaDisegno() {

  for (int index = 0; index < d.length; index++) {

    currentPosition = new PVector(random(width), random(height));

    d[index]= new Disegno(currentPosition, random(0.4, 1), radians(random(0, 360)));
  }
}


void keyPressed() {
  if (key=='z') {
    background(255);
    numb+=1;
    println("setup"+" "+ numb);
    iniziaDisegno();
  }
}



void drawLines2(){
  for(int i = 0; i < numeroOggetti; i ++) {

           stroke(0);
           // stroke(col);


           line(d[0].x, d[0].y, d[1].x,d[1].y);
          /*  line(circle[1].x, circle[1].y, circle[2].x, circle[2].y);
             line(circle[2].x, circle[2].y, circle[3].x, circle[3].y);
                line(circle[3].x, circle[3].y, circle[4].x, circle[4].y);
                   line(circle[4].x, circle[4].y, circle[5].x, circle[5].y);
                    line(circle[5].x, circle[5].y, circle[6].x, circle[6].y);
                     line(circle[6].x, circle[6].y, circle[7].x, circle[7].y);
                      line(circle[7].x, circle[7].y, circle[8].x, circle[8].y);
                       line(circle[8].x, circle[8].y, circle[9].x, circle[9].y);
                        line(circle[9].x, circle[9].y, circle[0].x, circle[0].y);
                     */

              /*  
             line(d[P].x, d[P].y, d[L].x, d[L].y);
              P++;
              L++;
              if(P>=numeroOggetti-1){P=0;}

              if(L>=numeroOggetti-1){L=0;}
              */


  }

}


 class Disegno{
  float A,B,scala,rotazione,x,y,a,b;

  PVector pos;

  Disegno(PVector position,float scala_, float rotazione_){

  pos= position; 
  scala=scala_;
  rotazione=rotazione_;


  }

  void display(){
      stroke(0);
       PVector vc=PVector.add(pos,currentPosition);

      line(pos.x,pos.y,currentPosition.x,currentPosition.y); 

      // with the above line of code it indeed draw the lines but all
      //originating from one point, while I wanted the line to go from 
      //one circle to the next defining the trayectory
      //I tried using the PVector addition unsuccessfully
      //what's the correct way to do it from within the class itself, using PVectors?

/*If you comment the line above, you'll see that no lines are drawn even if in 
the main sketch there is the same code as in sketch 1, that should make the lines appear. Why
it doesn't happen?*/

    pushMatrix();
   translate(pos.x, pos.y);

    rotate(rotazione);
     scale(scala);

      fill(colore);

      stroke(255);
      ellipse(x, y, 100, 100);

   popMatrix();

  }

  void update(){
   rotazione+=random(0.003,0.006); 
  }

}

Answers

Sign In or Register to comment.