angle between two points

hi everyone ,


i'm working on a robotics project, to move the robot from it's current position to target position i need to calculate the angle first before i can move the robot.

this the code I use to calculate the angle:
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
double cal_angle ( int current_x , int current_y , int tar_x , int tar_y )
{
	return atan2(tar_y - current_y, tar_x - current_x);
}

int main ()
{
		double x1 = 2.0;
		double y1 = -1;


		double x2 = 10.0;
		double y2 = -1;

		double x3 = 3.5;
		double y3 = -1;

		double x4 = 2.9;
		double y4 = -1;

		printf("%lf \n", cal_angle(x2 ,y2 , x1 , y1));
		printf("%lf \n", cal_angle(x3 ,y3 , x1 , y1));
		printf("%lf \n", cal_angle(x4 ,y4 , x1 , y1));

}


3.141593 
3.141593 
0.000000 

as u can see the angle between x4,y4 to x1,y1 should be 3.14 (180)
however , the result are correct as long as the distance from the current position to target position > 1 (not sure actually).

any ideas how to fix this thing ?! or why is this happening ,
pls note i'm newbie when it comes to algebra and trigonometry
Just wondering why you have defined your function with int parameters, but send doubles?

2.9 - 2.0 is 0.9 which is zero as an int.
WOW , just WOW

apparently this was the whole problem.
i have been digging into this for almost a week !!!.
i have been blind all this time!

thank u very very much ....
Topic archived. No new replies allowed.