# Acos() not giving angles between PI/2-3PI/2

**URL:** <https://discourse.processing.org/t/acos-not-giving-angles-between-pi-2-3pi-2/15513>\
**Category:** Coding Questions\
**Created:** [November 15, 2019, 4:49pm UTC](https://discourse.processing.org/t/acos-not-giving-angles-between-pi-2-3pi-2/15513 "2019-11-15T16:49:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![F53](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/f53/32/7118_2.png) [@F53](https://discourse.processing.org/u/F53)\
**Post date:** [November 15, 2019, 4:49pm UTC](https://discourse.processing.org/t/acos-not-giving-angles-between-pi-2-3pi-2/15513/1 "2019-11-15T16:49:42Z")

</div>

I am making a tank game, using the following to determine the angle from the tank to the cursor  
`rotate(atan(((mouseY-height/2)*scalar)/((mouseX-width/2)*scalar)));`  
then drawing my cannon within it (for shortening purpouses its just a rect)  
`rect(0,-5,30,10);`  
Then undoing the rotation with the negative of the previously mentioned rotate

Problem is, whenever the angle is from pi/2 to 3pi/2 (quadrants 2 and 3), it gives me the inverse.  
To better show this I have made a pointer from where the tank is, to where the cursor is with a line:  
`ellipse((mouseX-width/2)*(scalar),(mouseY-height/2)*(scalar), 5,5);`  
`line(0,0,(mouseX-width/2)*(scalar),(mouseY-height/2)*(scalar));`  
When the line is intersecting the rect, it is working correctly

here is a working copypasta that shows my problem

```auto
void setup(){
  fullScreen();
}

void draw(){
  background(255);
  translate(width/2, height/2);
  scale(height/540.0);
  
  ellipse(0,0,20,20);
  rotate(atan(((mouseY-height/2)*(540.0/height))/((mouseX-width/2)*(540.0/height))));
  rect(0,-5,30,10);
  rotate(-atan(((mouseY-height/2)*(540.0/height))/((mouseX-width/2)*(540.0/height))));
  ellipse((mouseX-width/2)*(540.0/height),(mouseY-height/2)*(540.0/height), 5,5);
  line(0,0,(mouseX-width/2)*(540.0/height),(mouseY-height/2)*(540.0/height));
}

```

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [November 15, 2019, 5:42pm UTC](https://discourse.processing.org/t/acos-not-giving-angles-between-pi-2-3pi-2/15513/2 "2019-11-15T17:42:11Z")

</div>

I suggest you try using `atan2` rather than `atan` to get the angle for rotation. The code below seems to work

```auto
void setup(){
  fullScreen();
}

void draw(){
  background(255);
  translate(width/2, height/2);
  scale(height/540.0);
  
  ellipse(0,0,20,20);
  rotate(atan2(((mouseY-height/2)*(540.0/height)), ((mouseX-width/2)*(540.0/height))));
  rect(0,-5,30,10);
  rotate(-atan2(((mouseY-height/2)*(540.0/height)), ((mouseX-width/2)*(540.0/height))));
  ellipse((mouseX-width/2)*(540.0/height),(mouseY-height/2)*(540.0/height), 5,5);
  line(0,0,(mouseX-width/2)*(540.0/height),(mouseY-height/2)*(540.0/height));
}

```

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [November 15, 2019, 5:51pm UTC](https://discourse.processing.org/t/acos-not-giving-angles-between-pi-2-3pi-2/15513/3 "2019-11-15T17:51:17Z")

</div>

In computing we don’t use tan to calculate the angle because it can produce illegal results… Consider your line  
`rotate(atan(((mouseY-height/2)*(540.0/height))/((mouseX-width/2)*(540.0/height))));`

If mouseX == width/2 then we have  
`rotate(atan(((mouseY-height/2)*(540.0/height))/ 0 ));`

i.e. division by zero.

this is not a problem with `atan2` because we are not doing the division, we have  
`rotate(atan2(((mouseY-height/2)*(540.0/height)) , 0 ));`
