# Get average heading

**URL:** <https://discourse.processing.org/t/get-average-heading/23483>\
**Category:** Coding Questions\
**Created:** [August 25, 2020, 11:14am UTC](https://discourse.processing.org/t/get-average-heading/23483 "2020-08-25T11:14:22Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![TimoL](https://avatars.discourse-cdn.com/v4/letter/t/9fc29f/32.png) [@TimoL](https://discourse.processing.org/u/TimoL)\
**Post date:** [August 25, 2020, 11:14am UTC](https://discourse.processing.org/t/get-average-heading/23483/1 "2020-08-25T11:14:23Z")

</div>

I have another maths/coding question.

Im trying to figure out the average heading of an array of PVectors.  
At first I tried adding up the heading values and dividing that by the count. But there are scenarios where the average is not the desired vector.  
I think this has to do with ‘heading()’ returning a value between -PI & PI.

So there are 2 vectors; A with a heading of 225° & B with a heading of 135°.  
using the above method my average works out to 0°, when the desired result is actually 180°.

 ![Screenshot 2020-08-25 at 22.36.17](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/2/28f22acaed7c92046267487c68970348a2056c40.png)

I’ve tried changing the heading to a value from 0 to TWO\_PI, but the problem still exists.

here is some code to show what I mean.

I hope I’ve explained myself properly.

```auto
PVector A;
PVector B;
PVector C;
float aA = 225; // change this to a number below 180 degrees
float aB = 135;

float angle = 135;
float averageHeading;

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

}

void draw() {
    background(200);
    stroke(0);
    noFill();
    circle(width/2, height/2, width);
      
    pushMatrix();
    translate(width/2, height/2);
    
    A = PVector.fromAngle(radians(aA));
    B = PVector.fromAngle(radians(aB));
    A.setMag(150);
    B.setMag(150);
  
    stroke(255, 0, 0);
    line(0, 0, A.x, A.y);
    line(0, 0, B.x, B.y);
  
    averageHeading = A.heading() + B.heading();
    averageHeading = averageHeading/2;
    
    C = PVector.fromAngle(averageHeading);
    C.setMag(150);
    stroke(255, 0, 255);
    line(0, 0, C.x, C.y);
  popMatrix();

}

```

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [August 25, 2020, 12:24pm UTC](https://discourse.processing.org/t/get-average-heading/23483/2 "2020-08-25T12:24:42Z")

</div>

Hello,

Intuitively, I would get the heading of the sum of the two vectors for this example.

`:)`

---

<div class="post-metadata">

**Author:** ![micycle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micycle/32/201_2.png) [@micycle](https://discourse.processing.org/u/micycle)\
**Post date:** [August 25, 2020, 12:25pm UTC](https://discourse.processing.org/t/get-average-heading/23483/3 "2020-08-25T12:25:08Z")

</div>

If `heading < 0`, then `heading = 2PI+heading` ?

In a one-liner: `heading = (heading+angle)%2PI;`

---

<div class="post-metadata">

**Author:** ![monkstone](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/monkstone/32/64_2.png) [@monkstone](https://discourse.processing.org/u/monkstone)\
**Post date:** [August 25, 2020, 7:17pm UTC](https://discourse.processing.org/t/get-average-heading/23483/4 "2020-08-25T19:17:07Z")

</div>

Just for a laugh I tried this in JRubyArt:-

```ruby
attr_reader :aaa, :abb

def settings
  size(300, 300)
end

def setup
  sketch_title "Average Heading"
  @aaa = 225
  @abb = 135
end

def draw
  background(200)
  stroke(0)
  noFill
  circle(width / 2, height / 2, width)
  push_matrix
  translate(width / 2, height / 2)
  vector_a = Vec2D.from_angle(aaa.radians)
  vector_b = Vec2D.from_angle(abb.radians)
  stroke(255, 0, 0)
  line(0, 0, vector_a.x * 150, vector_a.y * 150)
  line(0, 0, vector_b.x * 150, vector_b.y * 150)
  averageHeading = ((vector_a + vector_b) / 2.0).heading
  vector_c = Vec2D.from_angle(averageHeading)
  vector_c.set_mag(150)
  stroke(255, 0, 255)
  line(0, 0, vector_c.x, vector_c.y)
  pop_matrix
end

```

![heading](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/f/f281898b615d8264da4a09dac0767d38479eef0b.png)  
Works with 45 and -45 degrees gets:-  
 ![reverse](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/5/52e4aa482e1fa88b93ef1b06431d8c225544068a.png)

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [August 25, 2020, 7:41pm UTC](https://discourse.processing.org/t/get-average-heading/23483/5 "2020-08-25T19:41:18Z")

</div>

> [@glv](#):
>
> Intuitively

I took this out for a spin:

```auto
PVector A;
PVector B;
PVector C;
float aA = 225; // change this to a number below 180 degrees
float aB = 135;

float angle = 135;
float averageHeading;
float theta;

void setup () 
  {
  size (300, 300);
  A = new PVector(0, 0);
  B = new PVector(0, 0);
  C = new PVector(0, 0);
  }

void draw() 
  {
  background(200);
  stroke(0);
  noFill();
  circle(width/2, height/2, width);
    
  theta+= TAU/360;
  
  pushMatrix();
  translate(width/2, height/2);
  
  A = PVector.fromAngle(theta);
  B = PVector.fromAngle(radians(aB));
  
  A.setMag(150);
  B.setMag(150);

  stroke(255, 0, 0);
  line(0, 0, A.x, A.y);
  line(0, 0, B.x, B.y);

  //averageHeading = A.heading() + B.heading();
  //averageHeading = averageHeading/2;
  
  //C = PVector.fromAngle(averageHeading);
  //C.setMag(150);
  
  C.set(A.add(B));
  
  stroke(255, 0, 255);
  line(0, 0, C.x, C.y);
  popMatrix();
  }

```

I will leave the rest with you.

![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/b/bedf3038110211b4f93f761645a1dc870d656e4b.png)

`:)`

---

<div class="post-metadata">

**Author:** ![TimoL](https://avatars.discourse-cdn.com/v4/letter/t/9fc29f/32.png) [@TimoL](https://discourse.processing.org/u/TimoL)\
**Post date:** [August 26, 2020, 6:27am UTC](https://discourse.processing.org/t/get-average-heading/23483/6 "2020-08-26T06:27:32Z")

</div>

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.

```auto
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();
}

```

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [August 26, 2020, 2:54pm UTC](https://discourse.processing.org/t/get-average-heading/23483/7 "2020-08-26T14:54:14Z")

</div>

Hello,

@monkstone

I do not use JRubyArt… so correct me if I am wrong on this.

> [@monkstone](#):
>
> `averageHeading = ((vector_a + vector_b) / 2.0).heading`

This will return the heading for the result of vector A + vector B.

The **/2.0** is not necessary and may _[confound](https://www.merriam-webster.com/dictionary/confound)_ some visitors; I did not understand this but I have experience and was able to follow through on my initial intuition on this with a Processing version.

`:)`

---

<div class="post-metadata">

**Author:** ![monkstone](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/monkstone/32/64_2.png) [@monkstone](https://discourse.processing.org/u/monkstone)\
**Post date:** [August 26, 2020, 3:41pm UTC](https://discourse.processing.org/t/get-average-heading/23483/8 "2020-08-26T15:41:09Z")

</div>

You are absolutely correct, the divide by two isn’t needed, I quickly threw together code and was thinking about averaging turning moments. The dried up code:-

```ruby
attr_reader :aaa, :abb, :scale

def settings
  size(300, 300)
end

def setup
  sketch_title 'Average Heading'
  @aaa = 225
  @abb = 135
  @scale = 150
end

def draw_line(vec, scale)
  vec *= scale
  line(0, 0, vec.x, vec.y)
end

def draw
  background(200)
  stroke(0)
  no_fill
  circle(width / 2, height / 2, width)
  push_matrix
  translate(width / 2, height / 2)
  vector_a = Vec2D.from_angle(aaa.radians)
  vector_b = Vec2D.from_angle(abb.radians)
  stroke 255, 0, 0
  draw_line vector_a, scale
  draw_line vector_b, scale
  stroke 255, 0, 255
  draw_line (vector_a + vector_b), scale
  pop_matrix
end

```

No need for `heading` or `set_mag`, I just thought it might be interesting to see the Vec2D operators `+`, `*` instead of `add` and `mult` etc.

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [August 27, 2020, 4:41pm UTC](https://discourse.processing.org/t/get-average-heading/23483/9 "2020-08-27T16:41:04Z")

</div>

A good reference for PVectors:  
[https://natureofcode.com/book/chapter-1-vectors/](https://natureofcode.com/book/chapter-1-vectors/)

In addition to what is here:  
[https://processing.org/](https://processing.org/)

`:)`
