Mapping PVector heading() to another range

Hello @gomako,

PVector.heading() uses atan2() under the hood:
https://github.com/benfry/processing4/blob/main/core/src/processing/core/PVector.java#L849

atan2() Processing reference:

atan2() Wikipedia reference:

Angles are clockwise in Processing:

A quick demo to map values returned from atan2() to 0 to TAU radians (0 to 360 degrees):

PVector v;

void setup()
  {
  size(500, 500);
  }
  
void draw()
  {
  background(255);
  translate(width/2, height/2);
  v = new PVector(mouseX-width/2, mouseY-height/2);
  line(0, 0, v.x, v.y);
  float h = v.heading();
  println(h, degrees(h));
  if (h<0) h+= TAU;                  // This does the mapping! TAU = TWO_PI
  println(h, degrees(h));
  println();
  String s = str(int(degrees(h)))+ "°"; // Mofify if you want decimal places
  fill(255, 0, 0);
  textSize(24);
  text(s, v.x, v.y);
  }

There is also a map() function if you want to use that:

:)

2 Likes