c++ Sorting 2D Points clockwise

前端 未结 1 796
感动是毒
感动是毒 2021-01-21 08:51

i wrote a program to arrange points on a graph in clockwise manner from 12 o\'clock such that, a vector containing these points is sorted in that order. I am using atan2 to ge

1条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-21 09:18

    You don't need the adjustment. atan2 will give you the angle from positive direction of x axis, counterclockwise in range of -PI to PI.

    Firstly, to make the starting point positive direction of y axis, let me give parameter to atan2 as if negative direction of y axis is positive direction of x axis and positive direction of x axis is positive direction is y axis.

    Then, this will make the angle counterclockwise, so negate the angle in order to reverse the order.

    double get_clockwise_angle(const Point& p)
    {   
        double angle = 0.0;
        int quadrant = get_quadrant(p);
    
        /*making sure the quadrants are correct*/
        cout << "Point: " << p << " is on the " << quadrant << " quadrant" << endl;
    
        /*calculate angle and return it*/
        angle = -atan2(p.x,-p.y);
        return angle;
    }
    

    0 讨论(0)
提交回复
热议问题