# Thread: What is the best way of getting clockwise angle of 2 points?

1. ## What is the best way of getting clockwise angle of 2 points?

So if I have two points p1 and p2 and I want to get their clockwise angle.

So for example, if I had two points one at 10,10 and the other at 15, 15 I needed to get the angle that is 45.

If I have 10,10 and 10, 15. I would need 0. etc.

So the first point is my center and I need to get the angle of the second point compared to a vector that goes straight north.

I have already tried:

Code:
```double getAngleBetween(point p1, point p2){

double angle = atan2(p1.y, p1.x) - atan2(p2.y, p2.x);
angle = angle*360/PI;
if(angle<0){
angle+=360;
}
return angle;
}```
NOTE: point is a struct I defined, it just contains x and y values

But this does not give me the results that I need.

Thanks!

2. This seems to work.
Code:
```#include <stdio.h>
#include <math.h>

#define PI 3.14159265359

int main()
{
int x, y;
// x = p2.x - p1.x;
// y = p2.y - p2.y;
while (1)
{
scanf("%d%d", &x, &y);
double angle = asin(y / sqrt(x * x + y * y)) * (180 / PI);
if      (x < 0) angle = 180 - angle;
angle = 90 - angle;
if (angle < 0) angle += 360;
printf("%.3f\n", angle);
}
return 0;
}```

3. Updated code.
Code:
```#include <stdio.h>
#include <math.h>

#ifndef PI
#define PI 3.14159265359
#endif

typedef struct point { double x, y; } point;

double getAngleBetween(point p1, point p2)
{
double x = p2.x - p1.x, y = p2.y - p1.y;
double angle = asin(y / sqrt(x * x + y * y)) * (180 / PI);
angle = x < 0 ? angle - 90 : 90 - angle;
return angle < 0 ? angle + 360 : angle;
}

int main()
{
while (1)  // ctrl-c (or eof) to quit
{
point p1, p2;
printf("Enter two points: ");
if (scanf("%lf%lf%lf%lf", &p1.x, &p1.y, &p2.x, &p2.y) != 4)
break;
printf("%.3f\n", getAngleBetween(p1, p2));
}
return 0;
}```

Popular pages Recent additions