concat PVectors array

edited November 2016 in Programming Questions

Having and array of (random) PVectors, I want to concatenate 2 more "PVector" elements with fixed values. The thing is that I can not make it work with "concat" method for resizing the array.. what am I doin´wrong? thx!

class Agent {

PVector a;

Agent(){
a=new PVector(random(width),random(height));
}


}
// blabla

Agent [] a= new Agent(50);
PVector [] total;

PVector fixed1;
PVector fixed2;

//"solution1"
total=a.concat(fixed1,fixed2);

//"solution2"
PVector [] fixedValues ={ fixed1, fixed2}
total=a.concat(fixedValues);
Tagged:

Answers

  • Answer ✓

    concat() demands 2 arrays of the same compatible type:
    https://Processing.org/reference/concat_.html

    You may try out append() instead:
    https://Processing.org/reference/append_.html

    Or even better, go w/ ArrayList:
    https://Processing.org/reference/ArrayList.html

  • edited November 2016

    Tried out with ArrayList() as well. The idea is to have a sketch with a "repelling core" and 2 ArrayLists of PVectors: 1 with random values and another one with fixed values. Got a Null pointer exception in line 114:(??) :-w

    int elements=50; 
    PVector centro;
    ArrayList <Agent> a = new ArrayList <Agent>();
    Repeler rep;
    ArrayList <FixedPoint> fp = new ArrayList <FixedPoint>();
    Agent ag;
    
    void setup() {//______________________________________________________________
      size(1400, 800);
    
      frameRate(30);
      smooth();
    
      centro= new PVector(width/2, height/2);
      rep = new Repeler(centro);
    
      for (int i=0; i<elements; i++) {
        a.add(new Agent());
        a.add(new FixedPoint());
      }
    }
    
    void draw() {//_______________________________________________________________
      background(100);
    
      for (int i=0; i<a.size(); i++) {
        ag= a.get(i); 
    
        PVector repulse = rep.attract(ag);
        ag.update(repulse);
        ag.run();
      }
    
      rep.display();
    }
    
    
    class Agent {//_______________________________________________________________
      PVector loc,vel,acc;
    
      Agent( ) {
    
        vel= new PVector (0,0);
        acc= new PVector (random (-.01, .01), random (-.01, .01));
      }
    
      void run() {
        move();
        display();
        limits();
      }
    
     void update(PVector force){  
       vel.add(force);
      }
    
      void move() {
        vel.add(acc);
        loc.add(vel);
        acc.mult(0);
      }
    
      void limits() {
        if (loc.x > width-100|| loc.x < 100 ) {
          vel.x = -vel.x ;
        } else if (loc.y > height-100 || loc.y < 100) {
          vel.y = -vel.y;
        }
      }
    
       void display() {
        noStroke();
        fill(0, 255, 0);
        ellipse(loc.x, loc.y, 5, 5);
      }
    }
    
    
    class FixedPoint extends Agent{//____________________________________________
    
      PVector loc1, loc2, loc3, loc4;
    
    
      FixedPoint( ) {
    
        loc1= new PVector(100, 100);
        loc2= new PVector(100, 700);
        loc3= new PVector(1300, 100);
        loc4= new PVector(1300, 700);
      }
    
      void run() {
        display();
      }
    
      void display() {
        fill(0, 0, 255);
        ellipse(loc1.x, loc1.y, 5, 5); 
        ellipse(loc2.x, loc2.y, 5, 5); 
        ellipse(loc3.x, loc3.y, 5, 5); 
        ellipse(loc4.x, loc4.y, 5, 5);
      }
    }
    
    
    class Repeler {//_____________________________________________________________
      PVector rep;
    
      Repeler(PVector _rep) {
        rep= _rep;
      }
    
      PVector attract(Agent ag) {
        PVector force= PVector.sub(rep, ag.loc);
        float dist=force.mag();
    
        dist=constrain(dist, 1., 2.);
    
        force.normalize();
        force.mult(-1); 
        return force;
      }
    
    
      void display() {
        noStroke();
        fill(255, 0, 0);
        ellipse(rep.x, rep.y, 50, 50);
      }
    }
    
  • You should have initialized loc on line 42.
    Was it a typing error?

  • thx for ur tip Lord_of_the_galaxy unforgivable mistake from my side :-??

Sign In or Register to comment.