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:

  1. // Java program to find convex hull of a set of points. Refer
  2. // https://www.geeksforgeeks.org/orientation-3-ordered-points/
  3. // for explanation of orientation()
  4. import java.util.*;
  5.  
  6. class Point
  7. {
  8. int x, y;
  9. Point(int x, int y){
  10. this.x=x;
  11. this.y=y;
  12. }
  13. }
  14.  
  15. class GFG {
  16.  
  17. // To find orientation of ordered triplet (p, q, r).
  18. // The function returns following values
  19. // 0 --> p, q and r are colinear
  20. // 1 --> Clockwise
  21. // 2 --> Counterclockwise
  22. public static int orientation(Point p, Point q, Point r)
  23. {
  24. int val = (q.y - p.y) * (r.x - q.x) -
  25. (q.x - p.x) * (r.y - q.y);
  26.  
  27. if (val == 0) return 0; // collinear
  28. return (val > 0)? 1: 2; // clock or counterclock wise
  29. }
  30.  
  31. // Prints convex hull of a set of n points.
  32. public static void convexHull(Point points[], int n)
  33. {
  34. // There must be at least 3 points
  35. if (n < 3) return;
  36.  
  37. // Initialize Result
  38. Vector<Point> hull = new Vector<Point>();
  39.  
  40. // Find the leftmost point
  41. int l = 0;
  42. for (int i = 1; i < n; i++)
  43. if (points[i].x < points[l].x)
  44. l = i;
  45.  
  46. // Start from leftmost point, keep moving
  47. // counterclockwise until reach the start point
  48. // again. This loop runs O(h) times where h is
  49. // number of points in result or output.
  50. int p = l, q;
  51. do
  52. {
  53. // Add current point to result
  54. hull.add(points[p]);
  55.  
  56. // Search for a point 'q' such that
  57. // orientation(p, x, q) is counterclockwise
  58. // for all points 'x'. The idea is to keep
  59. // track of last visited most counterclock-
  60. // wise point in q. If any point 'i' is more
  61. // counterclock-wise than q, then update q.
  62. q = (p + 1) % n;
  63.  
  64. for (int i = 0; i < n; i++)
  65. {
  66. // If i is more counterclockwise than
  67. // current q, then update q
  68. if (orientation(points[p], points[i], points[q])
  69. == 2)
  70. q = i;
  71. }
  72.  
  73. // Now q is the most counterclockwise with
  74. // respect to p. Set p as q for next iteration,
  75. // so that q is added to result 'hull'
  76. p = q;
  77.  
  78. } while (p != l); // While we don't come to first
  79. // point
  80.  
  81. // Print Result
  82. for (Point temp : hull)
  83. System.out.println("(" + temp.x + ", " +
  84. temp.y + ")");
  85. }
  86.  
  87. /* Driver program to test above function */
  88. public static void main(String[] args)
  89. {
  90.  
  91. Point points[] = new Point[7];
  92. points[0]=new Point(0, 3);
  93. points[1]=new Point(2, 3);
  94. points[2]=new Point(1, 1);
  95. points[3]=new Point(2, 1);
  96. points[4]=new Point(3, 0);
  97. points[5]=new Point(0, 0);
  98. points[6]=new Point(3, 3);
  99.  
  100. int n = points.length;
  101. convexHull(points, n);
  102.  
  103. }
  104. }

  

Convex Hull | Set 1的更多相关文章

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

    凸包(Convex Hull) 在图形学中,凸包是一个非常重要的概念.简明的说,在平面中给出N个点,找出一个由其中某些点作为顶点组成的凸多边形,恰好能围住所有的N个点. 这十分像是在一块木板上钉了N个 ...

  2. Convex Hull 实现理论+自制Python代码

    Convex Hull 概述 计算n维欧式空间散点集的凸包,有很多的方法.但是如果要实现快速运算则其难点在于:如何快速判断散点集的成员是否是在凸集的内部.如果可以简化判断的运算过程,则可以极大简化迭代 ...

  3. OpenCV入门之寻找图像的凸包(convex hull)

    介绍   凸包(Convex Hull)是一个计算几何(图形学)中的概念,它的严格的数学定义为:在一个向量空间V中,对于给定集合X,所有包含X的凸集的交集S被称为X的凸包.   在图像处理过程中,我们 ...

  4. 2D Convex Hulls and Extreme Points( Convex Hull Algorithms) CGAL 4.13 -User Manual

    1 Introduction A subset S⊆R2 is convex if for any two points p and q in the set the line segment wit ...

  5. Monotone Chain Convex Hull(单调链凸包)

    Monotone Chain Convex Hull(单调链凸包)算法伪代码: //输入:一个在平面上的点集P //点集 P 按 先x后y 的递增排序 //m 表示共a[i=0...m]个点,ans为 ...

  6. convex hull

    1 什么是convex hull 就是凸包,是计算几何中的一个概念,计算几何是计算机图形学的基础之一. 对于二维平面来说是这样的:对于二维平面上的点集,凸包是位于最外层的点构成的包围其它所有的点的凸多 ...

  7. opencv::凸包-Convex Hull

    概念介绍 什么是凸包(Convex Hull),在一个多变形边缘或者内部任意两个点的连线都包含在多边形边界或者内部. 正式定义:包含点集合S中所有点的最小凸多边形称为凸包 Graham扫描算法 首先选 ...

  8. 2018牛客网暑假ACM多校训练赛(第三场)I Expected Size of Random Convex Hull 计算几何,凸包,其他

    原文链接https://www.cnblogs.com/zhouzhendong/p/NowCoder-2018-Summer-Round3-I.html 题目传送门 - 2018牛客多校赛第三场 I ...

  9. Gym 101986D Making Perimeter of the Convex Hull Shortest(凸包+极角排序)

    首先肯定是构造一个完整的凸包包括所有的点,那么要使得刚好有两个点在外面,满足这个条件的只有三种情况. 1.两个在凸包上但是不连续的两个点. 2.两个在凸包上但是连续的两个点. 3.一个在凸包上,还有一 ...

随机推荐

  1. Spring核心概念和案例

    一.Spring概念 1.Spring框架概述 轻量级的Java EE开源框架,它是由Rod Johnson为了解决企业应用程序开发的复杂性而创建, Spring框架提供了一个开发平台,用于整合其他技 ...

  2. 55、servlet3.0-ServletContext注册三大组件

    55.servlet3.0-ServletContext注册三大组件 使用ServletContext 注册 Servlet.Filter.Listener 使用编码的方式,在项目启动的时候给 Ser ...

  3. Windows 创建Raid

    Windows 常见raid有0.1和5,以下操作在虚拟机下模拟,学会这招在自己电脑做个raid也未尝不可啊~ 一.RAID 0 创建: 添加两块硬盘,联机并初始化(2T以下选MBR,以上选GPT) ...

  4. Spring 对事务的整合

    对事务的复习 什么是事务: 事务(TRANSACTION) 是作为单个逻辑工作单元执行的一系列操作. 多个操作作为一个整体向系统提交,要么都执行,要么都不执行. 事务是一个不可分割的逻辑单元. 事务的 ...

  5. js关闭当前页面不弹出提示

    window.top.opener=null; window.top.open('','_top');//top当前最顶层窗口.self表示当前打开的窗口 window.top.close(); 作用 ...

  6. Spring-RabbitMQ实现商品的同步(后台系统)

    1.配置rabbitMQ 需要把以上配置文件加载到spring容器,在appliacationContext.xml中添加如下内容: 注意:无需配置监听,因为服务器端(生产者只需要将消息发送到交换机即 ...

  7. 在win10环境下配置spark和scala

    在这里配置的是在命令行下运行spark的环境用来学习,最后结果如下,可运行简单的代码. 0.jdk.scala和spark的版本问题 有关版本如官网所示,我想要强调的是spark至今并不支持jdk11 ...

  8. python 字典元素操作

    #字典创建>>> dict2 = { 'abc': 123, 98.6: 37 }>>> dict2[98.6]37>>> dict2[" ...

  9. python pillow 处理图片

    demo1 #打开图片,并随机添加一些椒盐噪声 from PIL import Image import numpy as np import matplotlib.pyplot as plt img ...

  10. エンジニア死滅シタ世界之学べない学校 [MISSION LEVEL: C]-Python3

    答案 # coding: utf-8 # 自分の得意な言語で # Let's チャレンジ!! N=input() w_a=0 w_b=0 gpc_dict={ "gg":0,&qu ...