Convex Hull | Set 1

Given a set of points in the plane. the convex hull of the set is the smallest convex polygon that contains all the points of it.

https://www.geeksforgeeks.org/convex-hull-set-1-jarviss-algorithm-or-wrapping/

Lin家

Java:

// Java program to find convex hull of a set of points. Refer
// https://www.geeksforgeeks.org/orientation-3-ordered-points/
// for explanation of orientation()
import java.util.*; 

class Point
{
    int x, y;
    Point(int x, int y){
        this.x=x;
        this.y=y;
    }
} 

class GFG { 

    // To find orientation of ordered triplet (p, q, r).
    // The function returns following values
    // 0 --> p, q and r are colinear
    // 1 --> Clockwise
    // 2 --> Counterclockwise
    public static int orientation(Point p, Point q, Point r)
    {
        int 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.
    public static void convexHull(Point points[], int n)
    {
        // There must be at least 3 points
        if (n < 3) return; 

        // Initialize Result
        Vector<Point> hull = new Vector<Point>(); 

        // Find the leftmost point
        int l = 0;
        for (int 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.
        int p = l, q;
        do
        {
            // Add current point to result
            hull.add(points[p]); 

            // Search for a point ‘q‘ such that
            // orientation(p, x, q) 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 (int 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 (Point temp : hull)
            System.out.println("(" + temp.x + ", " +
                                temp.y + ")");
    } 

    /* Driver program to test above function */
    public static void main(String[] args)
    { 

        Point points[] = new Point[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); 

        int n = points.length;
        convexHull(points, n); 

    }
}

  

原文地址:https://www.cnblogs.com/lightwindy/p/9744073.html

时间: 2024-11-06 09:44:58

Convex Hull | Set 1的相关文章

凸包(Convex Hull)构造算法——Graham扫描法

凸包(Convex Hull) 在图形学中,凸包是一个非常重要的概念.简明的说,在平面中给出N个点,找出一个由其中某些点作为顶点组成的凸多边形,恰好能围住所有的N个点. 这十分像是在一块木板上钉了N个钉子,然后用一根绷紧的橡皮筋它们都圈起来,这根橡皮筋的形状就是所谓的凸包. 计算凸包的一个著名算法是Graham Scan法,它的时间复杂度与所采用的排序算法时间复杂度相同,通常采用线性对数算法,因此为\( O\left(N\mathrm{log}\left(N\right)\right) \).

OpenCV Tutorials &mdash;&mdash; Convex Hull

凸包 找到物体的轮廓之后,再找其凸包   void convexHull(InputArray points, OutputArray hull, bool clockwise=false, bool returnPoints=true ) Parameters: points – Input 2D point set, stored in std::vector or Mat. hull – Output convex hull. It is either an integer vector

zoj 3871 Convex Hull(凸包)

题目链接:zoj 3871 Convex Hull 枚举每条边,计算出有多少情况下为凸包的边界,即有多少点在该边的左边. #include <cstdio> #include <cstring> #include <cmath> #include <vector> #include <complex> #include <algorithm> using namespace std; typedef pair<int,int&g

opencv笔记(二十四)——得到轮廓之后找到凸包convex hull

当我们得到一张轮廓之后,我们可以对其运用convexHull方法,寻找该轮廓的凸包. 一个轮廓可以有无数个包围它的外壳,而其中表面积最小的一个外壳,就是凸包. void convexHull(InputArray points, OutputArray hull, bool clockwise=false, bool returnPoints=true ) points是一个contour. vector<Point>类型或者Mat类型 hull是输出,也是一个点集vector<Poin

Computational Geometry PA1 Convex Hull (凸包)

题目链接:http://dsa.cs.tsinghua.edu.cn/oj/problem.shtml?id=710 CG2015 PA1-1 Convex Hull (凸包) Description (描述) After learning Chapter 1, you must have mastered the convex hull very well. Yes, convex hull is at the kernel of computational geometry and serv

Monotone Chain Convex Hull(单调链凸包)

1 Monotone Chain Convex Hull(单调链凸包)算法伪代码: 2 //输入:一个在平面上的点集P 3 //点集 P 按 先x后y 的递增排序 4 //m 表示共a[i=0...m]个点,ans为要求的点; 5 struct P 6 { 7 int x,y; 8 friend int operator < (P a, P b) 9 { 10 if((a.x<b.x) || (a.x==b.x && a.y<b.y)) 11 return 1; 12 r

convex hull - Graham&#39;s scam Algorithm version-0.1

input:a finite set of two dimentional points P (the number of points n is more than 2) output: the convex hull of P Pesudo: 1, sort P in Lexgrahical order 2, construct the upper hull UHLL: 1' add two points on UHLL 2' for i=3 to  n add P[i] on UHLL W

凸包问题Finding the convex hull

问题描述:找到包含点集Q的最小凸多边形.使得点集内的点均在凸多边形的边上或内部. 即集合内任意两点的连线均在凸多边形内部. 输入:平面上的n个点的集合Q 输出: CH(Q):Q的convexhull (一)蛮力法思路: 找到点集内的内部点去掉,剩余未边界点. 内部点的判断:只要其中三点A,B,C构成的三角形包含的点P则P为内部点. 三角形内部点具体判断方法: 如果P在△ABC内部,则A,P两个点在BC同侧:P,B两个点在AC同侧:C,P两个点在AB同侧.转换为点与线关系的判断. 判断点P与直线A

[算法课][分治]寻找凸包 (Convex Hull)

凸包问题是算法中经典的题目了,最近算法课讲分治问题时提到了Convex Hull,算法导论的书上也花了篇幅讨论了Convex Hull的求解,主要是Graham方法. 为了能更好地理解分治和Graham这两种解法,我决定自己动手把代码写一遍. 然而,在写之前,我发现我大一学的用行列式求解由三个点围城的三角形面积已经忘得差不多了,现在补充一下: 利用这个计算结果来判断点p3在p1p2直线的左侧还是右侧 下面是分治算法求解: #include <iostream> #include <alg