Get average heading

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:-

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.

2 Likes