Java Program to Find Convex Hull using Quick Hull Algorithm

This is a Java Program to implement Quick Hull Algorithm to find convex hull. Here we’ll talk about the Quick Hull algorithm, which is one of the easiest to implement and has a reasonable expected running time of O(n log n).

Here is the source code of the Java Program to Implement Quick Hull Algorithm to Find Convex Hull. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. //This is a java program to find a points in convex hull using quick hull method
  2. //source: Alexander Hrishov's website
  3. //URL: http://www.ahristov.com/tutorial/geometry-games/convex-hull.html
  4.  
  5. import java.util.ArrayList;
  6. import java.util.Scanner;
  7.  
  8. public class QuickHull
  9. {
  10.     public ArrayList<Point> quickHull(ArrayList<Point> points)
  11.     {
  12.         ArrayList<Point> convexHull = new ArrayList<Point>();
  13.         if (points.size() < 3)
  14.             return (ArrayList) points.clone();
  15.  
  16.         int minPoint = -1, maxPoint = -1;
  17.         int minX = Integer.MAX_VALUE;
  18.         int maxX = Integer.MIN_VALUE;
  19.         for (int i = 0; i < points.size(); i++)
  20.         {
  21.             if (points.get(i).x < minX)
  22.             {
  23.                 minX = points.get(i).x;
  24.                 minPoint = i;
  25.             }
  26.             if (points.get(i).x > maxX)
  27.             {
  28.                 maxX = points.get(i).x;
  29.                 maxPoint = i;
  30.             }
  31.         }
  32.         Point A = points.get(minPoint);
  33.         Point B = points.get(maxPoint);
  34.         convexHull.add(A);
  35.         convexHull.add(B);
  36.         points.remove(A);
  37.         points.remove(B);
  38.  
  39.         ArrayList<Point> leftSet = new ArrayList<Point>();
  40.         ArrayList<Point> rightSet = new ArrayList<Point>();
  41.  
  42.         for (int i = 0; i < points.size(); i++)
  43.         {
  44.             Point p = points.get(i);
  45.             if (pointLocation(A, B, p) == -1)
  46.                 leftSet.add(p);
  47.             else if (pointLocation(A, B, p) == 1)
  48.                 rightSet.add(p);
  49.         }
  50.         hullSet(A, B, rightSet, convexHull);
  51.         hullSet(B, A, leftSet, convexHull);
  52.  
  53.         return convexHull;
  54.     }
  55.  
  56.     public int distance(Point A, Point B, Point C)
  57.     {
  58.         int ABx = B.x - A.x;
  59.         int ABy = B.y - A.y;
  60.         int num = ABx * (A.y - C.y) - ABy * (A.x - C.x);
  61.         if (num < 0)
  62.             num = -num;
  63.         return num;
  64.     }
  65.  
  66.     public void hullSet(Point A, Point B, ArrayList<Point> set,
  67.             ArrayList<Point> hull)
  68.     {
  69.         int insertPosition = hull.indexOf(B);
  70.         if (set.size() == 0)
  71.             return;
  72.         if (set.size() == 1)
  73.         {
  74.             Point p = set.get(0);
  75.             set.remove(p);
  76.             hull.add(insertPosition, p);
  77.             return;
  78.         }
  79.         int dist = Integer.MIN_VALUE;
  80.         int furthestPoint = -1;
  81.         for (int i = 0; i < set.size(); i++)
  82.         {
  83.             Point p = set.get(i);
  84.             int distance = distance(A, B, p);
  85.             if (distance > dist)
  86.             {
  87.                 dist = distance;
  88.                 furthestPoint = i;
  89.             }
  90.         }
  91.         Point P = set.get(furthestPoint);
  92.         set.remove(furthestPoint);
  93.         hull.add(insertPosition, P);
  94.  
  95.         // Determine who's to the left of AP
  96.         ArrayList<Point> leftSetAP = new ArrayList<Point>();
  97.         for (int i = 0; i < set.size(); i++)
  98.         {
  99.             Point M = set.get(i);
  100.             if (pointLocation(A, P, M) == 1)
  101.             {
  102.                 leftSetAP.add(M);
  103.             }
  104.         }
  105.  
  106.         // Determine who's to the left of PB
  107.         ArrayList<Point> leftSetPB = new ArrayList<Point>();
  108.         for (int i = 0; i < set.size(); i++)
  109.         {
  110.             Point M = set.get(i);
  111.             if (pointLocation(P, B, M) == 1)
  112.             {
  113.                 leftSetPB.add(M);
  114.             }
  115.         }
  116.         hullSet(A, P, leftSetAP, hull);
  117.         hullSet(P, B, leftSetPB, hull);
  118.  
  119.     }
  120.  
  121.     public int pointLocation(Point A, Point B, Point P)
  122.     {
  123.         int cp1 = (B.x - A.x) * (P.y - A.y) - (B.y - A.y) * (P.x - A.x);
  124.         if (cp1 > 0)
  125.             return 1;
  126.         else if (cp1 == 0)
  127.             return 0;
  128.         else
  129.             return -1;
  130.     }
  131.  
  132.     public static void main(String args[])
  133.     {
  134.         System.out.println("Quick Hull Test");
  135.         Scanner sc = new Scanner(System.in);
  136.         System.out.println("Enter the number of points");
  137.         int N = sc.nextInt();
  138.  
  139.         ArrayList<Point> points = new ArrayList<Point>();
  140.         System.out.println("Enter the coordinates of each points: <x> <y>");
  141.         for (int i = 0; i < N; i++)
  142.         {
  143.             int x = sc.nextInt();
  144.             int y = sc.nextInt();
  145.             Point e = new Point(x, y);
  146.             points.add(i, e);
  147.         }
  148.  
  149.         QuickHull qh = new QuickHull();
  150.         ArrayList<Point> p = qh.quickHull(points);
  151.         System.out
  152.                 .println("The points in the Convex hull using Quick Hull are: ");
  153.         for (int i = 0; i < p.size(); i++)
  154.             System.out.println("(" + p.get(i).x + ", " + p.get(i).y + ")");
  155.         sc.close();
  156.     }
  157. }

Output:

$ javac QuickHull.java
$ java QuickHull
 
Quick Hull Test
Enter the number of points
4
Enter the coordinates of each points: <x> <y>
12 32
45 98
65 12
10 30
The points in the Convex hull using Quick Hull are: 
(10, 30)
(45, 98)
(65, 12)

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.