# 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()?

2 Likes

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);
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

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.

2 Likes