angle of x and y points

Hey, I am pretty new to this stuff so I have been trying to do a program where the user enters the x and y cords and itll be able to figure out the angle by doing inverse tan (y,x). for some reason when the points lie in quad 2 or 3 it gives me wrong answers but when its 1 or 4 its correct. any idea what I am doing wrong? Thanks in advance!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
using namespace std;

float MyArcTangent(float y, float x);

int main(){

	float xCord, yCord;

	cout << "Enter the x coordinate: ";
	cin >> xCord;
	cout << "Enter the Y coordinate: ";
	cin >> yCord;
	cout << "The angle = "<< MyArcTangent(yCord,xCord);

	int  dumbyload;
	cin >> dumbyload;
}

float MyArcTangent(float y, float x){
	float angle,pi=3.14;
	if (x < 0){
		//Quad 2 or 3
		angle = (atan2(y , x)*180/pi) + 180;
	}
	else if(x > 0) {
		//Quad 1 or 4
		angle = atan2(y , x)*180/pi;
		cout << "1 or 4   ";
	}
	
	return angle;
}
Last edited on
The range of atan2() is [-pi,pi]. You should not be comparing on x. Only if y < 0 should you make an adjustment.

(I presume you are trying to get an angle in 0 to 360 degrees?)

Hope this helps.
Topic archived. No new replies allowed.