C/C++,图算法——凸包的快速壳(Quick Hull)算法的源代码

1 文本格式

// C++ program to implement Quick Hull algorithm
// to find convex hull.
#include<bits/stdc++.h>
using namespace std;

// iPair is integer pairs
#define iPair pair<int, int>

// Stores the result (points of convex hull)
set<iPair> hull;

// Returns the side of point p with respect to line
// joining points p1 and p2.
int findSide(iPair p1, iPair p2, iPair p)
{
    int val = (p.second - p1.second) * (p2.first - p1.first) -
        (p2.second - p1.second) * (p.first - p1.first);

    if (val > 0)
        return 1;
    if (val < 0)
        return -1;
    return 0;
}

// returns a value proportional to the distance
// between the point p and the line joining the
// points p1 and p2
int lineDist(iPair p1, iPair p2, iPair p)
{
    return abs((p.second - p1.second) * (p2.first - p1.first) -
        (p2.second - p1.second) * (p.first - p1.first));
}

// End points of line L are p1 and p2.  side can have value
// 1 or -1 specifying each of the parts made by the line L
void quickHull(iPair a[], int n, iPair p1, iPair p2, int side)
{
    int ind = -1;
    int max_dist = 0;

    // finding the point with maximum distance
    // from L and also on the specified side of L.
    for (int i = 0; i < n; i++)
    {
        int temp = lineDist(p1, p2, a[i]);
        if (findSide(p1, p2, a[i]) == side && temp > max_dist)
        {
            ind = i;
            max_dist = temp;
        }
    }

    // If no point is found, add the end points
    // of L to the convex hull.
    if (ind == -1)
    {
        hull.insert(p1);
        hull.insert(p2);
        return;
    }

    // Recur for the two parts divided by a[ind]
    quickHull(a, n, a[ind], p1, -findSide(a[ind], p1, p2));
    quickHull(a, n, a[ind], p2, -findSide(a[ind], p2, p1));
}

void printHull(iPair a[], int n)
{
    // a[i].second -> y-coordinate of the ith point
    if (n < 3)
    {
        cout << "Convex hull not possible\n";
        return;
    }

    // Finding the point with minimum and
    // maximum x-coordinate
    int min_x = 0, max_x = 0;
    for (int i = 1; i < n; i++)
    {
        if (a[i].first < a[min_x].first)
            min_x = i;
        if (a[i].first > a[max_x].first)
            max_x = i;
    }

    // Recursively find convex hull points on
    // one side of line joining a[min_x] and
    // a[max_x]
    quickHull(a, n, a[min_x], a[max_x], 1);

    // Recursively find convex hull points on
    // other side of line joining a[min_x] and
    // a[max_x]
    quickHull(a, n, a[min_x], a[max_x], -1);

    cout << "The points in Convex Hull are:\n";
    while (!hull.empty())
    {
        cout << "(" << (*hull.begin()).first << ", "
            << (*hull.begin()).second << ") ";
        hull.erase(hull.begin());
    }
}

// Driver code
int main()
{
    iPair a[] = { {0, 3}, {1, 1}, {2, 2}, {4, 4},
               {0, 0}, {1, 2}, {3, 1}, {3, 3} };
    int n = sizeof(a) / sizeof(a[0]);
    printHull(a, n);
    return 0;
}
 

2 代码格式

// C++ program to implement Quick Hull algorithm
// to find convex hull.
#include<bits/stdc++.h>
using namespace std;// iPair is integer pairs
#define iPair pair<int, int>// Stores the result (points of convex hull)
set<iPair> hull;// Returns the side of point p with respect to line
// joining points p1 and p2.
int findSide(iPair p1, iPair p2, iPair p)
{int val = (p.second - p1.second) * (p2.first - p1.first) -(p2.second - p1.second) * (p.first - p1.first);if (val > 0)return 1;if (val < 0)return -1;return 0;
}// returns a value proportional to the distance
// between the point p and the line joining the
// points p1 and p2
int lineDist(iPair p1, iPair p2, iPair p)
{return abs((p.second - p1.second) * (p2.first - p1.first) -(p2.second - p1.second) * (p.first - p1.first));
}// End points of line L are p1 and p2.  side can have value
// 1 or -1 specifying each of the parts made by the line L
void quickHull(iPair a[], int n, iPair p1, iPair p2, int side)
{int ind = -1;int max_dist = 0;// finding the point with maximum distance// from L and also on the specified side of L.for (int i = 0; i < n; i++){int temp = lineDist(p1, p2, a[i]);if (findSide(p1, p2, a[i]) == side && temp > max_dist){ind = i;max_dist = temp;}}// If no point is found, add the end points// of L to the convex hull.if (ind == -1){hull.insert(p1);hull.insert(p2);return;}// Recur for the two parts divided by a[ind]quickHull(a, n, a[ind], p1, -findSide(a[ind], p1, p2));quickHull(a, n, a[ind], p2, -findSide(a[ind], p2, p1));
}void printHull(iPair a[], int n)
{// a[i].second -> y-coordinate of the ith pointif (n < 3){cout << "Convex hull not possible\n";return;}// Finding the point with minimum and// maximum x-coordinateint min_x = 0, max_x = 0;for (int i = 1; i < n; i++){if (a[i].first < a[min_x].first)min_x = i;if (a[i].first > a[max_x].first)max_x = i;}// Recursively find convex hull points on// one side of line joining a[min_x] and// a[max_x]quickHull(a, n, a[min_x], a[max_x], 1);// Recursively find convex hull points on// other side of line joining a[min_x] and// a[max_x]quickHull(a, n, a[min_x], a[max_x], -1);cout << "The points in Convex Hull are:\n";while (!hull.empty()){cout << "(" << (*hull.begin()).first << ", "<< (*hull.begin()).second << ") ";hull.erase(hull.begin());}
}// Driver code
int main()
{iPair a[] = { {0, 3}, {1, 1}, {2, 2}, {4, 4},{0, 0}, {1, 2}, {3, 1}, {3, 3} };int n = sizeof(a) / sizeof(a[0]);printHull(a, n);return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/245136.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

数据库之 redis

前言&#xff1a; 就学习爬虫而言&#xff0c;对于三种常见的数据库做个基本了解足以&#xff0c;所以笔记都是浅尝辄止&#xff0c;不会涉及太深入的东西。 redis简介 Redis&#xff08;Remote Dictionary Server &#xff0c;远程字典服务&#xff09; 是一个使用ANSI C编写…

他山之石,可以攻玉|银行业数据中心数字化转型之模型篇 04(完结)

导语&#xff1a;他山之石&#xff0c;可以攻玉&#xff5c;银行业数据中心数字化转型之模型篇 04&#xff08;完结&#xff09; 银行业数据中心数字化转型是一项系统性工程既涉及管理层面转型——包括数字化转型战略、基础架构和技术架构转型、技术创新和知识体系转型&#xf…

uniapp自定义进度条组件

目标效果 原型设计为这样的样式&#xff0c;但是现有的进度条都无法满足需求&#xff0c;于是编写组件实现。 设计引用格式为 <zLineProgress :total"15" :val"7" title"你好吗" />定义组件 <template><view style"hei…

SpringBoot的配置加载优先级

目录 一、背景分析 二、学习资源 三、具体使用 四、一些小技巧 方式一 方式二 一、背景分析 SpringBoot项目在打包之后&#xff0c;其配置文件就在jar包内&#xff0c;如果没有<配置文件优先级>这个机制&#xff0c;那么项目打成jar包之后&#xff0c;如果启动项目…

Python的模块与库,及if __name__ == ‘__main__语句【侯小啾python领航班系列(二十四)】

Python的模块与库,及if name == __main__语句【侯小啾python领航班系列(二十四)】 大家好,我是博主侯小啾, 🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ…

分享76个节日PPT,总有一款适合您

分享76个节日PPT&#xff0c;总有一款适合您 76个节日PPT下载链接&#xff1a;https://pan.baidu.com/s/1pUnIoIUhzyXAB_5LnKGnNg?pwd6666 提取码&#xff1a;6666 Python采集代码下载链接&#xff1a;采集代码.zip - 蓝奏云 学习知识费力气&#xff0c;收集整理更不易…

关于svn如何上传一个完整的项目

注意&#xff1a;请一定要按照该步骤进行操作&#xff0c;请上传新项目时将项目名称进行规范命名 例如原始文件是arrange_v2 将此项目需要注入新的医院 则命名为 arrange_某医院名称_门诊或者医技或者药房_v2 重新命名文件夹名称快捷键 &#xff08;F12&#xff09; 一 &…

计算机网络扫盲(4)——时延

一、概述 在这里&#xff0c;我们考虑分组交换网的情况&#xff0c;因特网可以被看成是一种基础设施&#xff0c;该基础设施为运行在端系统上的分布式应用提供服务。在理想情况下&#xff0c;我们希望因特网服务能够在任意两个端系统之间随心所欲地移动数据而没有任何数据地丢失…

Python标准库:copy模块【侯小啾python领航班系列(十五)】

Python标准库:copy模块【侯小啾python领航班系列(十五)】 大家好,我是博主侯小啾, 🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔…

风控交易系统跟单系统资管软件都有哪些功能特点?

资管分仓软件的主要功能就是母账户可以添加子账号&#xff0c;并且设置出入金&#xff0c;手续费、保证金、风控等功能&#xff0c;同时监控端更可以直观的看子账户的交易情况直接折线图展示更加直观&#xff0c;在监控端的最高权限可以直接一键平仓子账户&#xff08;如果子账…

二蛋赠书十期:《剪映短视频剪辑从入门到精通》

前言 大家好&#xff01;我是二蛋&#xff0c;一个热爱技术、乐于分享的工程师。在过去的几年里&#xff0c;我一直通过各种渠道与大家分享技术知识和经验。我深知&#xff0c;每一位技术人员都对自己的技能提升和职业发展有着热切的期待。因此&#xff0c;我非常感激大家一直…

大数据|计算机毕业设计——基于Django协同过滤算法的房源可视化分析推荐系统的设计与实现

大数据|计算机毕业设计——基于Django协同过滤算法的房源可视化分析推荐系统的设计与实现 技术栈&#xff1a;大数据爬虫/机器学习学习算法/数据分析与挖掘/大数据可视化/Django框架/Mysql数据库 本项目基于 Django框架开发的房屋可视化分析推荐系统。这个系统结合了大数据爬…