C++ 顺时针排序 2D 点

2023-12-11

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 get the angle from 12 o'clock and then making adjustments based on the quadrant. i am trying to figure out where the bug is coming from as it is not ordering them correctly. So given 4 random points like those in the photo, it should order then in the containing vector as P1,P2,P3,P4order Here is my code:

//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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C++ 顺时针排序 2D 点 的相关文章

  • OpenCv读/写视频色差

    我试图简单地使用 openCV 打开视频 处理帧并将处理后的帧写入新的视频文件 我的问题是 即使我根本不处理帧 只是打开视频 使用 VideoCapture 读取帧并使用 VideoWriter 将它们写入新文件 输出文件看起来比输入更 绿
  • 32 位应用程序的特征最大矩阵大小

    所以 我正在寻找Eigen http eigen tuxfamily org index php title Main Page当我尝试声明大于 10000x10000 的矩阵时 包崩溃 我需要声明一个像这样的矩阵 可靠地大约有 13000
  • 为什么要序列化对象需要 Serialized 属性

    根据我的理解 SerializedAttribute 不提供编译时检查 因为它都是在运行时完成的 如果是这样 那么为什么需要将类标记为可序列化呢 难道序列化器不能尝试序列化一个对象然后失败吗 这不就是它现在所做的吗 当某些东西被标记时 它会
  • C++:重写已弃用的虚拟方法时出现弃用警告

    我有一个纯虚拟类 它有一个纯虚拟方法 应该是const 但不幸的是不是 该接口位于库中 并且该类由单独项目中的其他几个类继承 我正在尝试使用这个方法const不会破坏兼容性 至少在一段时间内 但我找不到在非常量方法重载时产生警告的方法 以下
  • 对齐 GridView 中的行值

    我需要在 asp net 3 5 中右对齐 gridview 列中的值 我怎样才能做到这一点
  • 如何使用recv()检测客户端是否仍然连接(并且没有挂起)?

    我写了一个多客户端服务器程序C on SuSE Linux 企业服务器 12 3 x86 64 我为每个客户端使用一个线程来接收数据 我的问题是 我使用一个终端来运行服务器 并使用其他几个终端来运行服务器telnet到我的服务器 作为客户端
  • POCO HTTPSClientSession 发送请求时遇到问题 - 证书验证失败

    我正在尝试使用 POCO 库编写一个向服务器发出 HTTPS 请求的程序 出于测试目的 我正在连接到具有自签名证书的服务器 并且我希望允许客户端进行连接 为了允许这种情况发生 我尝试安装InvalidCertificateHandler这是
  • 如何配置 WebService 返回 ArrayList 而不是 Array?

    我有一个在 jax ws 上实现的 java Web 服务 此 Web 服务返回用户的通用列表 它运行得很好 Stateless name AdminToolSessionEJB RemoteBinding jndiBinding Admi
  • 暂停下载线程

    我正在用 C 编写一个非常简单的批量下载程序 该程序读取要下载的 URL 的 txt 文件 我已经设置了一个全局线程和委托来更新 GUI 按下 开始 按钮即可创建并启动该线程 我想要做的是有一个 暂停 按钮 使我能够暂停下载 直到点击 恢复
  • 如何从网站下载 .EXE 文件?

    我正在编写一个应用程序 需要从网站下载 exe 文件 我正在使用 Visual Studio Express 2008 我正在使用以下代码 private void button1 Click object sender EventArgs
  • 将数组排序为第一个最小值、第一个最大值、第二个最小值、第二个最大值等

    编写一个JS程序 返回一个数组 其中第一个元素是第一个最小值 第二个元素是第一个最大值 依此类推 该程序包含一个函数 该函数接受一个参数 一个数组 该函数根据要求返回数组 输入示例 array 2 4 7 1 3 8 9 预期输出 1 9
  • Azure 事件中心 - 按顺序接收事件

    我使用下面的代码从 Azure Event Hub 接收事件 https learn microsoft com en us azure event hubs event hubs dotnet framework getstarted s
  • 如何重置捕获像素的值

    我正在尝试创建一个 C 函数 该函数返回屏幕截图位图中每四个像素的 R G 和 B 值 这是我的代码的一部分 for int ix 4 ix lt 1366 ix ix 4 x x 4 for int iy 3 iy lt 768 iy i
  • 当“int”处于最大值并使用 postfix ++ 进行测试时,代码定义良好吗?

    示例 未定义行为的一个示例是整数溢出的行为 C11dr 3 4 3 3 int溢出是未定义的行为 但这是否适用于存在循环的以下内容 并且不使用现在超出范围的副作用i 特别是 这是否后缀增量规格帮助 结果的值计算在副作用之前排序 更新操作数的
  • 在类的所有方法之前运行一个方法

    在 C 3 或 4 中可以做到这一点吗 也许有一些反思 class Magic RunBeforeAll public void BaseMethod runs BaseMethod before being executed public
  • 结构体指针的动态数组

    我必须使用以下代码块来完成学校作业 严格不进行任何修改 typedef struct char firstName char lastName int id float mark pStudentRecord pStudentRecord
  • strcmp 给出分段错误[重复]

    这个问题在这里已经有答案了 这是我的代码给出分段错误 include
  • 我在在线程序挑战编译器中遇到演示错误

    include
  • 双精度类型二维多维数组的 pinvoke 编组作为 c# 和 c++ 之间的输入和输出

    我有以下我正在尝试解决的双物质类型的 2d 多维数组的 c 和 c pinvoke 编组 我已经查看了以下热门内容以获得我目前拥有的内容使用双精度数组进行 P Invoke 在 C 和 C 之间编组数据 https stackoverflo
  • Googletest:如何异步运行测试?

    考虑到一个包含数千个测试的大型项目 其中一些测试需要几分钟才能完成 如果按顺序执行 整套测试需要一个多小时才能完成 通过并行执行测试可以减少测试时间 据我所知 没有办法直接从 googletest mock 做到这一点 就像 async选项

随机推荐