# Mapping PVector heading() to another range

**URL:** https://discourse.processing.org/t/mapping-pvector-heading-to-another-range/42999
**Category:** Coding Questions
**Created:** [October 17, 2023, 9:42pm UTC](https://discourse.processing.org/t/mapping-pvector-heading-to-another-range/42999 "2023-10-17T21:42:49Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![gomako](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gomako/32/18918_2.png) [@gomako](https://discourse.processing.org/u/gomako)
#### Post date: [October 17, 2023, 9:42pm UTC](https://discourse.processing.org/t/mapping-pvector-heading-to-another-range/42999/1 "2023-10-17T21:42:49Z")

</div>

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

---

<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: [October 18, 2023, 10:02am UTC](https://discourse.processing.org/t/mapping-pvector-heading-to-another-range/42999/2 "2023-10-18T10:02:33Z")

</div>

Hello @gomako,

> [@gomako](#):
>
> Is this expected behaviour?

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

_atan2()_ Processing reference:

> **[atan2() / Reference](https://processing.org/reference/atan2_.html)**
>
> Calculates the angle (in radians) from a specified point to the coordinate origin as measured from the positive x-axis. Values are returned as a float in the range from PI to -PI.…

_atan2()_ Wikipedia reference:

> **[atan2](https://en.wikipedia.org/wiki/Atan2)**
>
> In computing and mathematics, the function atan2 is the 2-argument arctangent. By definition, 
>   
>     
>       
> θ
> =
> atan2
> ⁡
> (
> y
> ,
> x
> )
>       
>     
> {\\displaystyle \\theta =\\operatorname {atan2} (y,x)}
>   
> is the angle measure (in radians, with 
>   
>     
>       
> −
> π
> \<
> θ
> ≤
> π
>       
>     
> {\\displaystyle -\\pi \<\\theta \\leq \\pi }
>   
> ) between the positive 
>   
>     
>       
> x
> ...

Angles are clockwise in Processing:

> **[2D Transformations](https://processing.org/tutorials/transform2d/)**
>
> Learn how to translate, rotate, and scale shapes using 2D transformations.

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

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

> **[map() / Reference](https://processing.org/reference/map_.html)**
>
> Re-maps a number from one range to another. In the first example above, the number 25 is converted from a value in the range of 0 to 100 into a value that ranges from the left edge of the w…

`:)`

---

<div class="post-metadata">

### Author: ![gomako](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gomako/32/18918_2.png) [@gomako](https://discourse.processing.org/u/gomako)
#### Post date: [October 18, 2023, 10:19am UTC](https://discourse.processing.org/t/mapping-pvector-heading-to-another-range/42999/3 "2023-10-18T10:19:22Z")

</div>

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.

> [@glv](#):
>
> `if (h<0) h+= TAU; // This does the mapping! TAU = TWO_PI`

Thanks again for the references too, I appreciate it.
