C++ c++;顺时针排序二维点

C++ c++;顺时针排序二维点,c++,algorithm,sorting,trigonometry,C++,Algorithm,Sorting,Trigonometry,我写了一个程序,从12点开始顺时针排列图上的点,这样,包含这些点的向量就按这个顺序排序。我使用atan2从12点开始获取角度,然后根据象限进行调整。我正试图找出bug的来源,因为它没有正确地对它们进行排序。给定4个随机点,比如照片中的点,它应该在包含向量中排序为P1,P2,P3,P4 这是我的密码: //sort_points.cpp #include <iostream> #include <math.h> #include <algorithm> #inc

我写了一个程序,从12点开始顺时针排列图上的点,这样,包含这些点的向量就按这个顺序排序。我使用atan2从12点开始获取角度,然后根据象限进行调整。我正试图找出bug的来源,因为它没有正确地对它们进行排序。给定4个随机点,比如照片中的点,它应该在包含向量中排序为P1,P2,P3,P4 这是我的密码:

//sort_points.cpp
#include <iostream>
#include <math.h>
#include <algorithm>
#include <vector>

using namespace std;

class Point
{
    public:
        double x;
        double y;
        Point(double xx, double yy) : x(xx), y(yy) {}
        ~Point();   
        inline friend ostream& operator<<(ostream& output, const Point& point)
        {
            output << "[" << point.x << ", " << point.y <<"]";
            return output;
        }
};


Point::~Point() {;}


/* get quadrant from 12 o'clock*/
int get_quadrant (const Point& p)
{
    int result = 4; //origin

    if (p.x > 0 && p.y > 0)
        return 1;
    else if(p.x < 0 && p.y > 0)
        return 2;
    else if(p.x < 0 && p.y < 0)
        return 3;
    //else 4th quadrant
    return result;
}

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;

    /*add the appropriate pi/2 value based on the quadrant. (one of 0, pi/2, pi, 3pi/2)*/
    switch(quadrant)
    {
        case 1:
            angle = atan2(p.x,p.y) * 180/M_PI;
            break;
        case 2:
            angle = atan2(p.y, p.x)* 180/M_PI;
            angle += M_PI/2;
            break;
        case 3:
            angle = atan2(p.x,p.y)* 180/M_PI;
            angle += M_PI;
            break;
        case 4:
            angle = atan2(p.y, p.x)* 180/M_PI;
            angle += 3*M_PI/2;
            break;
    }
    return angle;
}
bool compare_points(const Point& a, const Point& b)
{
    return (get_clockwise_angle(a) < get_clockwise_angle(b));
}
int main(int argc, char const *argv[])
{
    std::vector <Point> points;
    points.push_back( Point( 1, 3 ) );
    points.push_back( Point( 2, 1 ) );
    points.push_back( Point( -3, 2 ) );
    points.push_back( Point( -1, -1 ) );

    cout << "\nBefore sorting" << endl;
    for (int i = 0; i < points.size(); ++i)
    {
        cout << points.at(i) << endl;
    }

    std::sort(points.begin(), points.end(),compare_points);

    cout << "\nAfter sorting" << endl;
    for (int i = 0; i < points.size(); ++i)
    {
        cout << points.at(i) << endl;
    }
    return 0;
}
//sort\u points.cpp
#包括
#包括
#包括
#包括
使用名称空间std;
类点
{
公众:
双x;
双y;
点(双xx,双yy):x(xx),y(yy){
~Point();

内联friend ostream&operator您不需要调整。
atan2
将为您提供从x轴正方向开始的角度,在-PI到PI范围内逆时针方向

首先,为了使起点为y轴的正方向,让我将参数设置为
atan2
,就好像y轴的负方向为x轴的正方向,x轴的正方向为y轴的正方向一样

然后,这将使角度逆时针旋转,因此将角度取反,以反转顺序

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;
}
double get_顺时针(const Point&p)角度
{   
双角度=0.0;
int象限=get_象限(p);
/*确保象限是正确的*/

看起来你是在把弧度转换成度,然后根据圆周率加上一些数字。调整的目的是什么?好的,
atan2
给出了从3点钟开始的角度-clockwise@MikeCAT这是为了得到12的顺时针角度,因为我想顺时针排列,所以我需要比较t每个点的角度都是从12点钟开始的,从文档中可以看出,atan2给出了角度[-pi/2,pi/2],所以对于第二个象限(atan2的输出是[-pi/2,0]),您需要添加pi,结果是[pi/2 pi],pi表示第三象限,2pi表示第四象限。通过此转换,所有角度都将为正,并逆时针增加order@SomeGuy那么+0,+pi,+pi,+2pi的顺序是什么?