Javascript 使用 Jarvis 算法或包装的凸包(Convex Hull using Jarvis’ Algorithm or Wrapping)

embedded/2024/10/15 14:49:22/

 给定平面中的一组点,该集合的凸包是包含该集合所有点的最小凸多边形。

我们强烈建议您先阅读以下文章。 
如何检查两个给定的线段是否相交?

c++ https://blog.csdn.net/hefeng_aspnet/article/details/141713655
java https://blog.csdn.net/hefeng_aspnet/article/details/141713762
python https://blog.csdn.net/hefeng_aspnet/article/details/141714389
C# https://blog.csdn.net/hefeng_aspnet/article/details/141714420
javascript https://blog.csdn.net/hefeng_aspnet/article/details/141714442


贾维斯算法的思想很简单,我们从最左边的点(或 x 坐标值最小的点)开始,然后继续沿逆时针方向包裹点。 

最大的问题是,给定一个点 p 作为当前点,如何在输出中找到下一个点? 

这里的想法是使用orientation()。

 //c++ C++ 3 个有序点的方向(Orientation of 3 ordered points)-CSDN博客
//java Java 3 个有序点的方向(Orientation of 3 ordered points)-CSDN博客
//python Python 3 个有序点的方向(Orientation of 3 ordered points)_gh python 判断三点是否顺时针方向 如果是则反序-CSDN博客
//C# C# 3 个有序点的方向(Orientation of 3 ordered points)-CSDN博客
//Javascript javascript 3 个有序点的方向(Orientation of 3 ordered points)_threejs计算三个点的朝向-CSDN博客 

下一个点被选为在逆时针方向上领先于所有其他点的点,即,如果对于任何其他点 r,我们有“orientation(p, q, r) = 逆时针”,则下一个点是 q。 

 算法
步骤 1)将 p 初始化为最左边的点。 
步骤 2)继续进行,直到不再回到第一个(或最左边的)点。2.1 
            )下一个点 q 是这样的点,即对于任何其他点 r,三元组 (p, q, r) 都是逆时针的。 

                    为了找到这一点,我们只需将 q 初始化为下一个点,然后遍历所有点。 

                    对于任意点 i,如果 i 更偏向逆时针,即 orientation(p, i, q) 是逆时针的,则我们将 q 更新为 i。 

                    我们的 q 的最终值将是最逆时针的点。2.2 
           ) next[p] = q(将 q 作为 p 的下一个存储在输出凸包中)。2.3 
           ) p = q(将 p 设置为 q 以进行下一次迭代)。

以下是上述算法的实现: 

// Javascript program to find convex hull of a set of points. Refer 
// https://www.geeksforgeeks.org/orientation-3-ordered-points/
// for explanation of orientation()
 
class Point
{
    constructor(x, y)
    {
        this.x = x;
        this.y = y;
    }
}
 
// To find orientation of ordered triplet (p, q, r).
    // The function returns following values
    // 0 --> p, q and r are collinear
    // 1 --> Clockwise
    // 2 --> Counterclockwise
function orientation(p, q, r)
{
    let val = (q.y - p.y) * (r.x - q.x) -
                  (q.x - p.x) * (r.y - q.y);
        
        if (val == 0) return 0;  // collinear
        return (val > 0)? 1: 2; // clock or counterclock wise
}
 
// Prints convex hull of a set of n points.
function convexHull(points, n)
{
    // There must be at least 3 points
        if (n < 3) return;
        
        // Initialize Result
        let hull = [];
        
        // Find the leftmost point
        let l = 0;
        for (let i = 1; i < n; i++)
            if (points[i].x < points[l].x)
                l = i;
        
        // Start from leftmost point, keep moving 
        // counterclockwise until reach the start point
        // again. This loop runs O(h) times where h is
        // number of points in result or output.
        let p = l, q;
        do
        {
         
            // Add current point to result
            hull.push(points[p]);
        
            // Search for a point 'q' such that 
            // orientation(p, q, x) is counterclockwise 
            // for all points 'x'. The idea is to keep 
            // track of last visited most counterclock-
            // wise point in q. If any point 'i' is more 
            // counterclock-wise than q, then update q.
            q = (p + 1) % n;
               
            for (let i = 0; i < n; i++)
            {
               // If i is more counterclockwise than 
               // current q, then update q
               if (orientation(points[p], points[i], points[q])
                                                   == 2)
                   q = i;
            }
        
            // Now q is the most counterclockwise with
            // respect to p. Set p as q for next iteration, 
            // so that q is added to result 'hull'
            p = q;
        
        } while (p != l);  // While we don't come to first 
                           // point
        
        // Print Result
        for (let temp of hull.values())
            document.write("(" + temp.x + ", " +
                                temp.y + ")<br>");
}
 
/* Driver program to test above function */
let points = new Array(7);
points[0] = new Point(0, 3);
points[1] = new Point(2, 3);
points[2] = new Point(1, 1);
points[3] = new Point(2, 1);
points[4] = new Point(3, 0);
points[5] = new Point(0, 0);
points[6] = new Point(3, 3);
 
let n = points.length;
convexHull(points, n);
 
// This code is contributed by avanitrachhadiya2155 

输出:

(0,3)
(0,0)
(3,0)
(3,3)

时间复杂度:O(m * n),其中 n 是输入点数,m 是输出点数或船体点数 (m <= n)。对于船体上的每个点,我们都会检查所有其他点以确定下一个点。  

最坏情况,时间复杂度:O(n 2 )。最坏情况发生在所有点都在船体上(m = n)。

辅助空间:O(n),因为已占用 n 个额外空间

集合 2-凸包(Graham 扫描) 

注意:当凸包中存在共线点时,上述代码可能会对不同顺序的输入产生不同的结果。例如,当输入 (0, 3), (0, 0), (0, 1), (3, 0), (3, 3) 时,它产生 (0, 3) (0, 0) (3, 0) (3, 3) 的输出;当输入 (0, 3), (0, 1), (0, 0), (3, 0), (3, 3) 时,输出为 (0, 3) (0, 1) (0, 0) (3, 0) (3, 3)。如果是共线,我们通常需要最远的下一个点,如果是共线点,我们可以通过添加一个 if 条件来获得所需的结果。请参考此修改后的代码。

资料来源:
http://www.dcs.gla.ac.uk/~pat/52233/slides/Hull1x1.pdf


http://www.ppmy.cn/embedded/127914.html

相关文章

Loss:CornerNet: Detecting Objects as Paired Keypoints

目录 3 CornerNet(角点网络)3.1 概述3.2 检测角点3.2.1 检测角点概述3.2.2 训练中的惩罚调整3.2.3 焦点损失变体计算3.2.4 下采样与偏移量预测3.3 角点分组3.3.1 角点分组的需求与启发3.3.2 关联嵌入在角点分组中的应用3.3.3 “拉近”损失和“推开”损失计算3.4 角点池化3.4.…

Golang | Leetcode Golang题解之第474题一和零

题目&#xff1a; 题解&#xff1a; func findMaxForm(strs []string, m, n int) int {dp : make([][]int, m1)for i : range dp {dp[i] make([]int, n1)}for _, s : range strs {zeros : strings.Count(s, "0")ones : len(s) - zerosfor j : m; j > zeros; j--…

静态变量、变量作用域、命名空间

静态变量 静态变量一般位于程序全局data区&#xff0c;只是编程语言根据它所在的scope做语言级别访问限制。 静态变量和全局变量 可以在C语言一个函数中定义static变量&#xff0c;并比较和全局变量的地址差异。 C系语言使用static关键字标示静态变量。 PHP使用大写的STATIC关键…

(计算机毕设)基于Vue和Spring Boot的宠物救助网站设计与实现

博主可接毕设&#xff01;&#xff01;&#xff01; 毕业设计&#xff08;论文&#xff09; 基于Vue和Spring Boot的宠物救助网站设计与实现 摘 要 随着中国互联网的迅猛发展&#xff0c;传统宠物救助领域面临着信息管理繁琐、辐射范围有限、信息传播受限、丢失宠物找回几率较…

WPF样式详解:行内样式、模板样式和页面样式的全方位分析

Windows Presentation Foundation (WPF) 是微软推出的一种用于构建桌面应用程序的UI框架。WPF 提供了强大的样式和模板机制&#xff0c;允许开发人员以声明的方式定义和复用UI元素的视觉外观。本文将深入探讨WPF的行内样式、模板样式和页面样式&#xff0c;帮助您在实际开发中更…

C++:内存泄漏问题

内存泄漏是指程序在动态分配内存后&#xff0c;未能正确释放已分配的内存&#xff0c;导致这部分内存无法被再次使用的现象。它会导致程序的内存占用逐渐增大&#xff0c;可能最终导致系统内存不足&#xff0c;甚至程序崩溃。内存泄漏常见于使用手动内存管理的语言&#xff0c;…

openpdf

1、简介 2、示例 2.1 引入依赖 <dependency><groupId>com.github.librepdf</groupId><artifactId>openpdf</artifactId><version>1.3.34</version></dependency><dependency><groupId>com.github.librepdf</…

前端 | Uncaught (in promise) undefined

前端 | Uncaught (in promise) undefined 最近开发运行前端项目时&#xff0c;经常预计控制台报错 &#xff0c;如下图&#xff1a; 这里我总结下&#xff0c;这种报错的场景和原因&#xff0c;并通过实际代码案例帮助小伙伴更好理解下 。 文章目录 前端 | Uncaught (in promi…