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

  1. #1
    Registered User
    Join Date
    Jan 2019
    Posts
    3

    Question 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. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    630
    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;
    }
    Last edited by john.c; 01-20-2019 at 06:13 PM.
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    630
    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;
    }
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Plot Simple Points / Graph, X & Y array points
    By Khadafi in forum C++ Programming
    Replies: 9
    Last Post: 11-11-2011, 03:47 AM
  2. Counter-Clockwise/Clockwise Vertices
    By seubjoh in forum C Programming
    Replies: 1
    Last Post: 08-06-2009, 07:24 AM
  3. Orienting points counter clockwise
    By dudeomanodude in forum C++ Programming
    Replies: 2
    Last Post: 02-07-2008, 01:39 PM
  4. Decimal Points and Binary Points
    By Shadow12345 in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 11-07-2002, 01:06 AM

Tags for this Thread