problem with atan in c++

Iam writing a program for my senior project its a program to input the current coordinates and the destination coordinates and output the compass angle that the user should take, put the program is giving me this errors:

1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\math.h(545): could be 'long double atan(long double)'
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\math.h(497): or 'float atan(float)'
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\math.h(109): or 'double atan(double)'
1> while trying to match the argument list '(int)'
1>c:\users\mohd\documents\visual studio 2008\projects\cordinates angle\cordinates angle\cordinates.cpp(19) : error C2668: 'atan' : ambiguous call to overloaded function

1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\math.h(545): could be 'long double atan(long double)'
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\math.h(497): or 'float atan(float)'
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\math.h(109): or 'double atan(double)'

this is my codes
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
  #include<iostream>
#include<string>
#include <stdio.h>
#include <math.h>
using namespace std;
float getAngle(int x1,int y1,int x2,int y2)
{
	float angle;
	if(x2>x1)
	{
		if(y2>y1)
			angle=atan((x2-x1)/(y2-y1));
		else if(y1>y2)
			angle=(atan((y1-y2)/(x2-x1)))+90;
	}
	else if(x1>x2)
	{
		if(y2>y1)
			angle=(atan((x1-x2)/(y1-y2)))+180;
		else if(y1>y2)
			angle=(atan((x1-x2)/(y2-y1)))+270;
	}
	else if(x1=x2)
	{
		if(y2>y1)
			angle=0.0;
		else if(y1>y2)
			angle=180.0;
	}
	else if(y1=y2)
	{
		if(x2>x1)
			angle=90.0;
		if(x1>x2)
			angle=270.0;
	}
	return angle;
}
int main()
{
	int x1, x2, y1, y2;
	float angle;
	cout<<"Enter the cordinates of your point: ";
	cin>>x1>>y1;
	cout<<"Enter the cordinates of the distination point: ";
	cin>>x2>>y2;
	angle=getAngle(x1,y1,x2,y2);
	cout<<"The angle is "<<angle<<endl;
	return 0;
}


thank you
The compiler doesn't know which of the overloads of atan() to convert to as int is equally convertible to all the given types. Try casting one of the parameter to one of the given types.
As well as casting the parameters to type float or double (or simply making the parameters of type double in the first place), there's another point.

Since you are working with the ratio y/x, it would be simpler to use the function atan2() which is designed for just this purpose. It directly returns the result in the range -pi to +pi radians (equivalent to -180 to +180 degrees).
http://www.cplusplus.com/reference/cmath/atan2/

Note that whichever version you use, the result will be in radians. The current code which adds 90 or 270 degrees etc won't work unless the resulting angle is first converted to degrees (but atan2 simplifies this task).
my program worked after i changed the variables to float but its not outputting the angle do i have to multiply the output by some number?
and if i used atan2() how to use it to get the angle between the two coordinates , i mean what do i have to change?
There are a few issues with your original code.
if(x1=x2)

if(y1=y2)
here the operator = should be ==

You have forgotten to convert from radians to degrees.
1
2
const double PI =  3.1415926535897932385;
angle = atan(n) * 180.0 / PI;

In addition to which, I'm not sure that you are correctly calculating the angle.


If you want to use atan2 instead, it takes two parameters, y and x. Simply call it as atan2(y2-y1, x2-x1).

The exception being the case when both x1 == x2 and y1 == y2, in that case the answer is unknown, but a default of zero may be adequate.

1
2
3
4
5
6
7
double mygetAngle(double x1, double y1, double x2, double y2)
{
    if ((x1 == x2) && (y1 == y2))
        return 0;

    return 180.0 / PI * atan2(y2-y1, x2-x1);
}


Last edited on
Topic archived. No new replies allowed.