Get average heading

thanks guys. I guess I went wrong by trying to divide the sum of the headings.

Here’s the code that now gets the average of an array of vectors.

PVector [] vectors = new PVector [3];
PVector C = new PVector(0, 0);

void setup () {
  size (300, 300);

  for (int i = 0; i < vectors.length; i ++) {
    vectors[i] = PVector.random2D();
  }

  C = new PVector(0, 0);
}

void draw() {
  background(200);
  stroke(0);
  noFill();
  circle(width/2, height/2, width);


  pushMatrix();
  translate(width/2, height/2);


  for (int i = 0; i < vectors.length; i ++) {
    C.add(vectors[i]);
    vectors[i].setMag(150);
    line(0, 0, vectors[i].x, vectors[i].y);
  }
  stroke(255, 0, 0);
  C.setMag(150);
  stroke(255, 0, 255);
  line(0, 0, C.x, C.y);
  popMatrix();
}
2 Likes