77import java .util .Objects ;
88import java .util .Set ;
99
10+ /**
11+ * A class representing points on a 2D plane and providing algorithms to compute
12+ * the convex hull of a set of points using brute-force and recursive methods.
13+ *
14+ * Convex hull: The smallest convex polygon that contains all the given points.
15+ *
16+ * Algorithms provided:
17+ * 1. Brute-Force Method
18+ * 2. Recursive (Divide-and-Conquer) Method
19+ */
1020class Point implements Comparable <Point > {
1121 double x , y ;
1222
23+ /**
24+ * Constructor to initialize a point with x and y coordinates.
25+ *
26+ * @param x The x-coordinate of the point.
27+ * @param y The y-coordinate of the point.
28+ */
1329 public Point (double x , double y ) {
1430 this .x = x ;
1531 this .y = y ;
1632 }
1733
34+ /**
35+ * Compares this point to another point based on x and y coordinates.
36+ *
37+ * @param other The other point to compare with.
38+ * @return A negative integer, zero, or a positive integer as this point is
39+ * less than, equal to, or greater than the specified point.
40+ */
1841 @ Override
1942 public int compareTo (Point other ) {
2043 if (this .x != other .x ) {
@@ -23,29 +46,69 @@ public int compareTo(Point other) {
2346 return Double .compare (this .y , other .y );
2447 }
2548
49+ /**
50+ * Checks if this point is equal to another object based on x and y coordinates.
51+ *
52+ * @param obj The object to compare with.
53+ * @return true if the other object is a Point with the same coordinates;
54+ * false otherwise.
55+ */
2656 @ Override
2757 public boolean equals (Object obj ) {
2858 if (!(obj instanceof Point )) return false ;
2959 Point other = (Point ) obj ;
3060 return this .x == other .x && this .y == other .y ;
3161 }
3262
63+ /**
64+ * Generates the hash code for the point based on its coordinates.
65+ *
66+ * @return The hash code of the point.
67+ */
3368 @ Override
3469 public int hashCode () {
3570 return Objects .hash (x , y );
3671 }
3772
73+ /**
74+ * Returns a string representation of the point in (x, y) format.
75+ *
76+ * @return A string representing the point.
77+ */
3878 @ Override
3979 public String toString () {
4080 return String .format ("(%.1f, %.1f)" , x , y );
4181 }
4282}
4383
84+ /**
85+ * A class that provides two algorithms to find the convex hull of a set of points:
86+ * 1. Brute-force method
87+ * 2. Recursive (divide-and-conquer) method
88+ */
4489public class ConvexHull {
90+
91+ /**
92+ * Computes the determinant of three points to determine their orientation.
93+ *
94+ * @param a The first point.
95+ * @param b The second point.
96+ * @param c The third point.
97+ * @return A positive value if points a, b, c are in counter-clockwise order;
98+ * negative if in clockwise order; and 0 if they are collinear.
99+ */
45100 private static double det (Point a , Point b , Point c ) {
46101 return (a .x * b .y + b .x * c .y + c .x * a .y ) - (a .y * b .x + b .y * c .x + c .y * a .x );
47102 }
48103
104+ /**
105+ * Brute-force algorithm to find the convex hull of a set of points.
106+ * This algorithm checks every pair of points and determines if all other
107+ * points lie to one side of the line formed by these two points.
108+ *
109+ * @param points A list of points for which to compute the convex hull.
110+ * @return A sorted list of points forming the convex hull.
111+ */
49112 public static List <Point > convexHullBruteForce (List <Point > points ) {
50113 Collections .sort (points );
51114 Set <Point > convexSet = new HashSet <>();
@@ -88,6 +151,12 @@ public static List<Point> convexHullBruteForce(List<Point> points) {
88151 return result ;
89152 }
90153
154+ /**
155+ * Recursive (divide-and-conquer) algorithm to find the convex hull of a set of points.
156+ *
157+ * @param points A list of points for which to compute the convex hull.
158+ * @return A sorted list of points forming the convex hull.
159+ */
91160 public static List <Point > convexHullRecursive (List <Point > points ) {
92161 Collections .sort (points );
93162 Set <Point > convexSet = new HashSet <>();
@@ -117,6 +186,14 @@ public static List<Point> convexHullRecursive(List<Point> points) {
117186 return result ;
118187 }
119188
189+ /**
190+ * Helper function to construct the convex hull recursively.
191+ *
192+ * @param points The list of candidate points for the hull.
193+ * @param left The left boundary point.
194+ * @param right The right boundary point.
195+ * @param convexSet The set to store points forming the convex hull.
196+ */
120197 private static void constructHull (List <Point > points , Point left , Point right , Set <Point > convexSet ) {
121198 if (!points .isEmpty ()) {
122199 Point extremePoint = null ;
0 commit comments