PVector[] variable doesn't work when moving PVectors inside itself
              in 
             Programming Questions 
              •  
              7 months ago    
            
 
           
             I'm having some trouble with this data representation program I've been trying to do. I used the example Examples->Basics->Input->MouseSignals to make tests:
            
             
            
            
             
            
            
             
            
            
             
              
             
             
              
             
             
                
             
             
              
             
             
              
             
             
                
             
             
              
             
             
                  
             
             
                
             
             
              
             
             
            
             
            
            
             
            
            
             
            
            
             
            
            
             
            
            
             
              
             
             
                  
             
             
            
             
            
            
             
            
            
 
            
           
              /**
             
             
               * Mouse Signals. 
             
             
               * 
             
             
               * Move and click the mouse to generate signals. 
             
             
               * The top row is the signal from "mouseX", 
             
             
               * the middle row is the signal from "mouseY",
             
             
               * and the bottom row is the signal from "mousePressed". 
             
             
               */
             
             
              PVector[] vals;//*******ADDED
             
             
              int[] xvals;
             
             
              int[] yvals;
             
             
              int[] bvals;
             
             
              void setup() 
             
             
              {
             
             
                size(640, 360);
             
             
                noSmooth();
             
             
                xvals = new int[width];
             
             
                yvals = new int[width];
             
             
                bvals = new int[width];
             
             
                vals =new PVector[width];//*******ADDED
             
             
                for (int i=0;i<width;i++){vals[i]=new PVector();}//*******ADDED
             
             
              }
             
             
              int arrayindex = 0;
             
             
              void draw()
             
             
              {
             
             
                background(102);
             
             
                for(int i = 1; i < width; i++) { 
             
             
                  xvals[i-1] = xvals[i]; 
             
             
                  yvals[i-1] = yvals[i];
             
             
                  bvals[i-1] = bvals[i];
             
             
                  vals[i-1]=vals[i];//*******ADDED
             
             
                } 
             
             
                // Add the new values to the end of the array 
             
             
                xvals[width-1] = mouseX; 
             
             
                yvals[width-1] = mouseY;
             
             
                vals[width-1].x=mouseX;//*******ADDED
             
             
                vals[width-1].y=mouseY;//*******ADDED
             
             
                if(mousePressed) {
             
             
                  bvals[width-1] = 0;
             
             
                  println(vals);
             
             
                } else {
             
             
                  bvals[width-1] = 255;
             
             
                }
             
             
                fill(255);
             
             
                noStroke();
             
             
                rect(0, height/3, width, height/3+1);
             
             
                for(int i=1; i<width; i++) {
             
             
                  stroke(255);
             
             
                  point(i, xvals[i]/3);
             
             
                  point(i, vals[i].x/3);//*******ADDED
             
             
                  stroke(0);
             
             
                  point(i, height/3+yvals[i]/3);
             
             
                  point(i, height/3+vals[i].y/3);//*******ADDED
             
             
                  stroke(255);
             
             
                  line(i, 2*height/3+bvals[i]/3, i, (2*height/3+bvals[i-1]/3));
             
             
                }
             
             
              }
             
            
             The points done by the PVector variable are a straight line and them should be the same as the points done by xvals and yvals. My goal is to replace the two variables (xvals and yvals) for only one PVector. In my program I'll be working with a lot of them and I just wanted to reduce the lines. Looks like the problem happens when I try to move the data one position to the left in the variable PVector[] vals.
            
            
                for(int i = 1; i < width; i++) { 
             
             
                  xvals[i-1] = xvals[i]; 
             
             
                  yvals[i-1] = yvals[i];
             
             
                  bvals[i-1] = bvals[i];
             
             
                 
               vals[i-1]=vals[i];
              //*******ADDED
             
             
                } 
             
            
             Do you know why it doesn't work? Any solutions?
            
            
             Thanks!
            
 
            
              
              1  
            
 
            