c++顺时针排序二维点

标签 c++ algorithm sorting trigonometry

我编写了一个程序,从 12 点钟开始按顺时针方向排列图形上的点,这样,包含这些点的 vector 按该顺序排序。我正在使用 atan2 从 12 点开始获取角度,然后根据象限进行调整。我试图找出错误的来源,因为它没有正确排序它们。所以给定 4 个像照片中的随机点,它应该在包含 vector 中排序为 P1、P2、P3、P4 order 这是我的代码:

//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;
}

最佳答案

您不需要调整。 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;
}

关于c++顺时针排序二维点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39187184/

相关文章:

c++ - 为什么boost mpl set允许非唯一类型

c++ - 似乎没有调用重载的复制构造函数

arrays - 以有效的方式排序和合并两个数组?

sorting - ParseQueryAdapter : sort data on client side

Java.util.Arrays.sort - 什么类型?

java - 使用集合的排序方法对 JList 进行排序

c++ - 在并行 omp 循环中同时写入同一内​​存

c++ - bloom_filter_prl.exe : 0xC00000FD: Stack overflow 中 0x013f3277 的未处理异常

algorithm - 加权无向图中的最长路径

java - 对 Java 中 *any* 类的所有实例进行总排序