Mapping PVector heading() to another range

I am creating a PVector using the .fromAngle() method. I use an variable that increments from 0 to TWO_PI. When I access the heading() of the vector, I get a range from 0 → PI → -PI → 0. Is this expected behaviour? I’m trying to map the angle of the vector to another range and am struggling to find a way to do this.
Am I being stupid? Is there another way to access the angle of the vector that isn’t .heading()?


Hello @gomako,

PVector.heading() uses atan2() under the hood:

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()
  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));
  String s = str(int(degrees(h)))+ "°"; // Mofify if you want decimal places
  fill(255, 0, 0);
  text(s, v.x, v.y);

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



Ah thank you so much for the detailed answer, that is just what I needed. I couldn’t quite figure out how to make the -PI to -1 bit into PI+1 to TWO_PI and the following is what did it.

Thanks again for the references too, I appreciate it.