class Arrays: Why did I have to take away the argument of the class constructor and substitute PVect

edited November 2013 in Questions about Code

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

//Sorry for reposting it, but I hit the answered button when it wasn't, to thanks 2 forum members that explained me I had to format the code. Here is the issue: 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(); 
    //as you will see the function drawlines2 where I even wrote line by line to test it
    // doesn't draw anything even when in the draw() why?
          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

  • Answer ✓

    In the code below I have got rid of the x and y class attributes and use the PVector pos

    Scaling is always performed about the origin (x=0, y=0) which is the top left corner.

    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 you want to scale the image do it here
      scale(0.6);
    
      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].pos.x, d[P].pos.y, d[L].pos.x, d[L].pos.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].pos.x, d[P].pos.y, d[L].pos.x, d[L].pos.y);
        P++;
        L++;
        if (P>=numeroOggetti-1) {
          P=0;
        }
        if (L>=numeroOggetti-1) {
          L=0;
        }
      }
    }
    
    class Disegno {
      PVector pos;
    
      Disegno() {
        pos = new PVector(random(0, width), random(0, height));
      }
    
      void display() {
        pushMatrix();
    
        stroke(0);
    
        //why do the circles move in the corner of the screen when I usescale even if
        // why if I did put popMatrix and pushMatrix()?
        //scale(0.6);
    
        fill(colore);
        stroke(255);
        ellipse(pos.x, pos.y, 50, 50);
        popMatrix();
      }
    }  
    
  • edited November 2013

    Hi Quark thanks!!! so the error was in line 58 where I had put: line(d[P].x, d[P].y, d[L].x, d[L].y); while the correct one is: line(d[P].pos.x, d[P].pos.y, d[L].pos.x, d[L].pos.y); ... I see, so it worked when I took away the argument in the constructor but merely cause then I was using x and y. Great! Hey can I bother you with another probelm. Question 1: right now I want the ellipses to appear one by one, it's doing it but it's looping through, what I want is that once it appears the last one of the array to stop looping through. It's indeed a cool effect I could use later on, but right now it just consumes memory and as my drawings use beziercurves they already consume tons of memory and slow down the program enough as it is :-) Thanks!!!!!

  • edited November 2013

    Is it possible to put the lines traiectory directly into the class?

Sign In or Register to comment.