From bd65ff20f651ca84ff93904fa569f195b8e1e23c Mon Sep 17 00:00:00 2001 From: Bayram Turgut <137455737+bayramtturgutt@users.noreply.github.com> Date: Sat, 3 Aug 2024 12:00:54 +0300 Subject: [PATCH 01/13] Update GrahamScan.java improved the Javadoc comments, clarified some methods in the Point class, and corrected some text. --- .../thealgorithms/geometry/GrahamScan.java | 47 +++++++++---------- 1 file changed, 21 insertions(+), 26 deletions(-) diff --git a/src/main/java/com/thealgorithms/geometry/GrahamScan.java b/src/main/java/com/thealgorithms/geometry/GrahamScan.java index 2773d03b4769..49eebc0d32e6 100644 --- a/src/main/java/com/thealgorithms/geometry/GrahamScan.java +++ b/src/main/java/com/thealgorithms/geometry/GrahamScan.java @@ -4,13 +4,12 @@ import java.util.Comparator; import java.util.Stack; -/* - * A Java program that computes the convex hull using the Graham Scan algorithm +/** + * A Java program that computes the convex hull using the Graham Scan algorithm. * In the best case, time complexity is O(n), while in the worst case, it is O(nlog(n)). - * O(n) space complexity - * + * O(n) space complexity. * This algorithm is only applicable to integral coordinates. - * + * * Reference: * https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/geometry/graham_scan_algorithm.cpp * https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/geometry/graham_scan_functions.hpp @@ -20,17 +19,12 @@ public class GrahamScan { private final Stack hull = new Stack<>(); public GrahamScan(Point[] points) { - - /* - * pre-process the points by sorting them with respect to the bottom-most point, then we'll - * push the first point in the array to be our first extreme point. - */ + // Pre-process the points by sorting them with respect to the bottom-most point Arrays.sort(points); Arrays.sort(points, 1, points.length, points[0].polarOrder()); hull.push(points[0]); - // find index of first point not equal to a[0] (indexPoint1) and the first point that's not - // collinear with either (indexPoint2). + // Find the index of the first point not equal to points[0] int indexPoint1; for (indexPoint1 = 1; indexPoint1 < points.length; indexPoint1++) { if (!points[0].equals(points[indexPoint1])) { @@ -41,6 +35,7 @@ public GrahamScan(Point[] points) { return; } + // Find the index of the first point that's not collinear with points[0] and points[indexPoint1] int indexPoint2; for (indexPoint2 = indexPoint1 + 1; indexPoint2 < points.length; indexPoint2++) { if (Point.orientation(points[0], points[indexPoint1], points[indexPoint2]) != 0) { @@ -49,7 +44,7 @@ public GrahamScan(Point[] points) { } hull.push(points[indexPoint2 - 1]); - // Now we simply add the point to the stack based on the orientation. + // Add points to the stack based on orientation for (int i = indexPoint2; i < points.length; i++) { Point top = hull.pop(); while (Point.orientation(hull.peek(), top, points[i]) <= 0) { @@ -98,12 +93,12 @@ public int y() { } /** - * Finds the orientation of ordered triplet. + * Finds the orientation of an ordered triplet. * * @param a Co-ordinates of point a - * @param b Co-ordinates of point a - * @param c Co-ordinates of point a - * @return { -1, 0, +1 } if a -→ b -→ c is a { clockwise, collinear; counterclockwise } + * @param b Co-ordinates of point b + * @param c Co-ordinates of point c + * @return { -1, 0, +1 } if a -> b -> c is a { clockwise, collinear, counterclockwise } * turn. */ public static int orientation(Point a, Point b, Point c) { @@ -115,11 +110,11 @@ public static int orientation(Point a, Point b, Point c) { } /** - * @param p2 Co-ordinate of point to compare to. - * This function will compare the points and will return a positive integer if the - * point is greater than the argument point and a negative integer if the point is - * less than the argument point. + * @param p2 Co-ordinate of the point to compare to. + * @return a positive integer if this point is greater than the argument point, + * a negative integer if this point is less than the argument point. */ + @Override public int compareTo(Point p2) { int res = Integer.compare(this.y, p2.y); if (res == 0) { @@ -129,9 +124,8 @@ public int compareTo(Point p2) { } /** - * A helper function that will let us sort points by their polar order - * This function will compare the angle between 2 polar Co-ordinates - * + * A helper function that lets us sort points by their polar order. + * * @return the comparator */ public Comparator polarOrder() { @@ -139,6 +133,7 @@ public Comparator polarOrder() { } private final class PolarOrder implements Comparator { + @Override public int compare(Point p1, Point p2) { int dx1 = p1.x - x; int dy1 = p1.y - y; @@ -146,9 +141,9 @@ public int compare(Point p1, Point p2) { int dy2 = p2.y - y; if (dy1 >= 0 && dy2 < 0) { - return -1; // q1 above; q2 below + return -1; // p1 above; p2 below } else if (dy2 >= 0 && dy1 < 0) { - return +1; // q1 below; q2 above + return +1; // p1 below; p2 above } else if (dy1 == 0 && dy2 == 0) { // 3-collinear and horizontal if (dx1 >= 0 && dx2 < 0) { return -1; From 6f3893f1ad7639c9ec114f5d9bcf9f19861791f1 Mon Sep 17 00:00:00 2001 From: Bayram Turgut <137455737+bayramtturgutt@users.noreply.github.com> Date: Sat, 3 Aug 2024 12:18:41 +0300 Subject: [PATCH 02/13] Minor adjustment to GrahamScan.java --- .../thealgorithms/geometry/GrahamScan.java | 82 ++++++++----------- 1 file changed, 33 insertions(+), 49 deletions(-) diff --git a/src/main/java/com/thealgorithms/geometry/GrahamScan.java b/src/main/java/com/thealgorithms/geometry/GrahamScan.java index 49eebc0d32e6..1652e26dddcc 100644 --- a/src/main/java/com/thealgorithms/geometry/GrahamScan.java +++ b/src/main/java/com/thealgorithms/geometry/GrahamScan.java @@ -6,11 +6,11 @@ /** * A Java program that computes the convex hull using the Graham Scan algorithm. - * In the best case, time complexity is O(n), while in the worst case, it is O(nlog(n)). - * O(n) space complexity. - * This algorithm is only applicable to integral coordinates. + * The time complexity is O(n) in the best case and O(n log(n)) in the worst case. + * The space complexity is O(n). + * This algorithm is applicable only to integral coordinates. * - * Reference: + * References: * https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/geometry/graham_scan_algorithm.cpp * https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/geometry/graham_scan_functions.hpp * https://algs4.cs.princeton.edu/99hull/GrahamScan.java.html @@ -19,12 +19,12 @@ public class GrahamScan { private final Stack hull = new Stack<>(); public GrahamScan(Point[] points) { - // Pre-process the points by sorting them with respect to the bottom-most point + // Pre-process points: sort by y-coordinate, then by polar order with respect to the first point Arrays.sort(points); Arrays.sort(points, 1, points.length, points[0].polarOrder()); hull.push(points[0]); - // Find the index of the first point not equal to points[0] + // Find the first point not equal to points[0] and the first point not collinear with the previous points int indexPoint1; for (indexPoint1 = 1; indexPoint1 < points.length; indexPoint1++) { if (!points[0].equals(points[indexPoint1])) { @@ -35,7 +35,6 @@ public GrahamScan(Point[] points) { return; } - // Find the index of the first point that's not collinear with points[0] and points[indexPoint1] int indexPoint2; for (indexPoint2 = indexPoint1 + 1; indexPoint2 < points.length; indexPoint2++) { if (Point.orientation(points[0], points[indexPoint1], points[indexPoint2]) != 0) { @@ -44,7 +43,7 @@ public GrahamScan(Point[] points) { } hull.push(points[indexPoint2 - 1]); - // Add points to the stack based on orientation + // Process the remaining points and update the hull for (int i = indexPoint2; i < points.length; i++) { Point top = hull.pop(); while (Point.orientation(hull.peek(), top, points[i]) <= 0) { @@ -56,14 +55,14 @@ public GrahamScan(Point[] points) { } /** - * @return A stack of points representing the convex hull. + * @return An iterable collection of points representing the convex hull. */ public Iterable hull() { - Stack s = new Stack<>(); + Stack result = new Stack<>(); for (Point p : hull) { - s.push(p); + result.push(p); } - return s; + return result; } public record Point(int x, int y) implements Comparable { @@ -93,40 +92,34 @@ public int y() { } /** - * Finds the orientation of an ordered triplet. - * - * @param a Co-ordinates of point a - * @param b Co-ordinates of point b - * @param c Co-ordinates of point c - * @return { -1, 0, +1 } if a -> b -> c is a { clockwise, collinear, counterclockwise } - * turn. + * Determines the orientation of the triplet (a, b, c). + * + * @param a The first point + * @param b The second point + * @param c The third point + * @return -1 if (a, b, c) is clockwise, 0 if collinear, +1 if counterclockwise */ public static int orientation(Point a, Point b, Point c) { int val = (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x); - if (val == 0) { - return 0; - } - return (val > 0) ? +1 : -1; + return Integer.compare(val, 0); } /** - * @param p2 Co-ordinate of the point to compare to. - * @return a positive integer if this point is greater than the argument point, - * a negative integer if this point is less than the argument point. + * Compares this point with another point. + * + * @param p2 The point to compare to + * @return A positive integer if this point is greater, a negative integer if less, or 0 if equal */ @Override public int compareTo(Point p2) { - int res = Integer.compare(this.y, p2.y); - if (res == 0) { - res = Integer.compare(this.x, p2.x); - } - return res; + int cmpY = Integer.compare(this.y, p2.y); + return cmpY != 0 ? cmpY : Integer.compare(this.x, p2.x); } /** - * A helper function that lets us sort points by their polar order. + * Returns a comparator to sort points by their polar order relative to this point. * - * @return the comparator + * @return A polar order comparator */ public Comparator polarOrder() { return new PolarOrder(); @@ -141,32 +134,23 @@ public int compare(Point p1, Point p2) { int dy2 = p2.y - y; if (dy1 >= 0 && dy2 < 0) { - return -1; // p1 above; p2 below + return -1; // p1 above p2 } else if (dy2 >= 0 && dy1 < 0) { - return +1; // p1 below; p2 above - } else if (dy1 == 0 && dy2 == 0) { // 3-collinear and horizontal - if (dx1 >= 0 && dx2 < 0) { - return -1; - } else if (dx2 >= 0 && dx1 < 0) { - return +1; - } else { - return 0; - } + return 1; // p1 below p2 + } else if (dy1 == 0 && dy2 == 0) { // Collinear and horizontal + return Integer.compare(dx2, dx1); } else { - return -orientation(Point.this, p1, p2); // both above or below + return -orientation(Point.this, p1, p2); // Compare orientation } } } /** - * Override of the toString method, necessary to compute the difference - * between the expected result and the derived result - * - * @return a string representation of any given 2D point in the format (x, y) + * @return A string representation of this point in the format (x, y) */ @Override public String toString() { - return "(" + x + ", " + y + ")"; + return String.format("(%d, %d)", x, y); } } } From c9322dd00e5274e49497eb1ee3321eb45687b44a Mon Sep 17 00:00:00 2001 From: Bayram Turgut <137455737+bayramtturgutt@users.noreply.github.com> Date: Sat, 3 Aug 2024 12:20:21 +0300 Subject: [PATCH 03/13] revised GrahamScan.java --- src/main/java/com/thealgorithms/geometry/GrahamScan.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/thealgorithms/geometry/GrahamScan.java b/src/main/java/com/thealgorithms/geometry/GrahamScan.java index 1652e26dddcc..22ba617d052d 100644 --- a/src/main/java/com/thealgorithms/geometry/GrahamScan.java +++ b/src/main/java/com/thealgorithms/geometry/GrahamScan.java @@ -9,7 +9,7 @@ * The time complexity is O(n) in the best case and O(n log(n)) in the worst case. * The space complexity is O(n). * This algorithm is applicable only to integral coordinates. - * + * * References: * https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/geometry/graham_scan_algorithm.cpp * https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/geometry/graham_scan_functions.hpp @@ -93,7 +93,7 @@ public int y() { /** * Determines the orientation of the triplet (a, b, c). - * + * * @param a The first point * @param b The second point * @param c The third point @@ -106,7 +106,7 @@ public static int orientation(Point a, Point b, Point c) { /** * Compares this point with another point. - * + * * @param p2 The point to compare to * @return A positive integer if this point is greater, a negative integer if less, or 0 if equal */ @@ -118,7 +118,7 @@ public int compareTo(Point p2) { /** * Returns a comparator to sort points by their polar order relative to this point. - * + * * @return A polar order comparator */ public Comparator polarOrder() { From 22a8eff559e8e4a262ec04c91548cffd9f5e3495 Mon Sep 17 00:00:00 2001 From: Bayram Turgut <137455737+bayramtturgutt@users.noreply.github.com> Date: Thu, 8 Aug 2024 11:08:33 +0300 Subject: [PATCH 04/13] Update-2 GrahamScan.java --- .../thealgorithms/geometry/GrahamScan.java | 28 ++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/thealgorithms/geometry/GrahamScan.java b/src/main/java/com/thealgorithms/geometry/GrahamScan.java index 22ba617d052d..baa8be9f4326 100644 --- a/src/main/java/com/thealgorithms/geometry/GrahamScan.java +++ b/src/main/java/com/thealgorithms/geometry/GrahamScan.java @@ -19,12 +19,32 @@ public class GrahamScan { private final Stack hull = new Stack<>(); public GrahamScan(Point[] points) { + + // Validate input data + if (points == null || points.length < 3) { + throw new IllegalArgumentException("At least 3 points are required to compute the convex hull"); + } + + // Check if all points are the same + boolean allSame = true; + for (int i = 1; i < points.length; i++) { + if (!points[i].equals(points[0])) { + allSame = false; + break; + } + } + + if (allSame) { + hull.push(points[0]); + return; + } + // Pre-process points: sort by y-coordinate, then by polar order with respect to the first point Arrays.sort(points); Arrays.sort(points, 1, points.length, points[0].polarOrder()); hull.push(points[0]); - // Find the first point not equal to points[0] and the first point not collinear with the previous points + // Find the first point not equal to points[0]/firstNonEqualIndex and the first point not collinear firstNonCollinearIndex with the previous points int indexPoint1; for (indexPoint1 = 1; indexPoint1 < points.length; indexPoint1++) { if (!points[0].equals(points[indexPoint1])) { @@ -58,11 +78,11 @@ public GrahamScan(Point[] points) { * @return An iterable collection of points representing the convex hull. */ public Iterable hull() { - Stack result = new Stack<>(); + Stack s = new Stack<>(); for (Point p : hull) { - result.push(p); + s.push(p); } - return result; + return s; } public record Point(int x, int y) implements Comparable { From 9eb7a95874352b58d4865bf6ffc012164b576329 Mon Sep 17 00:00:00 2001 From: Bayram Turgut <137455737+bayramtturgutt@users.noreply.github.com> Date: Thu, 8 Aug 2024 11:11:28 +0300 Subject: [PATCH 05/13] clang format GrahamScan.java --- src/main/java/com/thealgorithms/geometry/GrahamScan.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/main/java/com/thealgorithms/geometry/GrahamScan.java b/src/main/java/com/thealgorithms/geometry/GrahamScan.java index baa8be9f4326..0f023cb6ac5c 100644 --- a/src/main/java/com/thealgorithms/geometry/GrahamScan.java +++ b/src/main/java/com/thealgorithms/geometry/GrahamScan.java @@ -19,12 +19,10 @@ public class GrahamScan { private final Stack hull = new Stack<>(); public GrahamScan(Point[] points) { - // Validate input data if (points == null || points.length < 3) { throw new IllegalArgumentException("At least 3 points are required to compute the convex hull"); } - // Check if all points are the same boolean allSame = true; for (int i = 1; i < points.length; i++) { @@ -33,12 +31,10 @@ public GrahamScan(Point[] points) { break; } } - if (allSame) { hull.push(points[0]); return; } - // Pre-process points: sort by y-coordinate, then by polar order with respect to the first point Arrays.sort(points); Arrays.sort(points, 1, points.length, points[0].polarOrder()); From 49c0a8361daa3844321a83eab106a53f4da9a565 Mon Sep 17 00:00:00 2001 From: Bayram Turgut <137455737+bayramtturgutt@users.noreply.github.com> Date: Thu, 8 Aug 2024 14:44:53 +0300 Subject: [PATCH 06/13] reverted GrahamScan.java --- .../com/thealgorithms/geometry/GrahamScan.java | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/src/main/java/com/thealgorithms/geometry/GrahamScan.java b/src/main/java/com/thealgorithms/geometry/GrahamScan.java index 0f023cb6ac5c..e6e53f28c592 100644 --- a/src/main/java/com/thealgorithms/geometry/GrahamScan.java +++ b/src/main/java/com/thealgorithms/geometry/GrahamScan.java @@ -19,22 +19,6 @@ public class GrahamScan { private final Stack hull = new Stack<>(); public GrahamScan(Point[] points) { - // Validate input data - if (points == null || points.length < 3) { - throw new IllegalArgumentException("At least 3 points are required to compute the convex hull"); - } - // Check if all points are the same - boolean allSame = true; - for (int i = 1; i < points.length; i++) { - if (!points[i].equals(points[0])) { - allSame = false; - break; - } - } - if (allSame) { - hull.push(points[0]); - return; - } // Pre-process points: sort by y-coordinate, then by polar order with respect to the first point Arrays.sort(points); Arrays.sort(points, 1, points.length, points[0].polarOrder()); From 8249731205275210600daeafa4934a6b739448f5 Mon Sep 17 00:00:00 2001 From: Bayram Turgut <137455737+bayramtturgutt@users.noreply.github.com> Date: Thu, 8 Aug 2024 20:58:29 +0300 Subject: [PATCH 07/13] minor updates.java minor updates --- .../thealgorithms/geometry/GrahamScan.java | 59 ++++++++----------- 1 file changed, 26 insertions(+), 33 deletions(-) diff --git a/src/main/java/com/thealgorithms/geometry/GrahamScan.java b/src/main/java/com/thealgorithms/geometry/GrahamScan.java index e6e53f28c592..374ed4e9a4c5 100644 --- a/src/main/java/com/thealgorithms/geometry/GrahamScan.java +++ b/src/main/java/com/thealgorithms/geometry/GrahamScan.java @@ -17,52 +17,45 @@ */ public class GrahamScan { private final Stack hull = new Stack<>(); - public GrahamScan(Point[] points) { // Pre-process points: sort by y-coordinate, then by polar order with respect to the first point Arrays.sort(points); Arrays.sort(points, 1, points.length, points[0].polarOrder()); hull.push(points[0]); - - // Find the first point not equal to points[0]/firstNonEqualIndex and the first point not collinear firstNonCollinearIndex with the previous points - int indexPoint1; - for (indexPoint1 = 1; indexPoint1 < points.length; indexPoint1++) { - if (!points[0].equals(points[indexPoint1])) { - break; - } - } - if (indexPoint1 == points.length) { - return; + // Find the first point not equal to points[0]/firstNonEqualIndex and the first point not collinear firstNonCollinearIndex with the previous points + int firstNonEqualIndex; + for (firstNonEqualIndex = 1; firstNonEqualIndex < points.length; firstNonEqualIndex++) { + if (!points[0].equals(points[firstNonEqualIndex])) { + break; } - - int indexPoint2; - for (indexPoint2 = indexPoint1 + 1; indexPoint2 < points.length; indexPoint2++) { - if (Point.orientation(points[0], points[indexPoint1], points[indexPoint2]) != 0) { - break; - } + } + if (firstNonEqualIndex == points.length) { + return; + } + + int firstNonCollinearIndex; + for (firstNonCollinearIndex = firstNonEqualIndex + 1; firstNonCollinearIndex < points.length; firstNonCollinearIndex++) { + if (Point.orientation(points[0], points[firstNonEqualIndex], points[firstNonCollinearIndex]) != 0) { + break; } - hull.push(points[indexPoint2 - 1]); - - // Process the remaining points and update the hull - for (int i = indexPoint2; i < points.length; i++) { - Point top = hull.pop(); - while (Point.orientation(hull.peek(), top, points[i]) <= 0) { - top = hull.pop(); - } - hull.push(top); - hull.push(points[i]); + } + hull.push(points[firstNonCollinearIndex - 1]); + + // Process the remaining points and update the hull + for (int i = firstNonCollinearIndex; i < points.length; i++) { + Point top = hull.pop(); + while (Point.orientation(hull.peek(), top, points[i]) <= 0) { + top = hull.pop(); } + hull.push(top); + hull.push(points[i]); } - + /** * @return An iterable collection of points representing the convex hull. */ public Iterable hull() { - Stack s = new Stack<>(); - for (Point p : hull) { - s.push(p); - } - return s; + return new ArrayList<>(hull); } public record Point(int x, int y) implements Comparable { From 42b866e499427956d13751cd3adba1ae69aee690 Mon Sep 17 00:00:00 2001 From: Bayram Turgut <137455737+bayramtturgutt@users.noreply.github.com> Date: Thu, 8 Aug 2024 21:09:04 +0300 Subject: [PATCH 08/13] Spc.java --- .../thealgorithms/geometry/GrahamScan.java | 59 +++++++++++-------- 1 file changed, 34 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/thealgorithms/geometry/GrahamScan.java b/src/main/java/com/thealgorithms/geometry/GrahamScan.java index 374ed4e9a4c5..5b6e1efb6afc 100644 --- a/src/main/java/com/thealgorithms/geometry/GrahamScan.java +++ b/src/main/java/com/thealgorithms/geometry/GrahamScan.java @@ -3,6 +3,7 @@ import java.util.Arrays; import java.util.Comparator; import java.util.Stack; +import java.util.ArrayList; /** * A Java program that computes the convex hull using the Graham Scan algorithm. @@ -16,41 +17,49 @@ * https://algs4.cs.princeton.edu/99hull/GrahamScan.java.html */ public class GrahamScan { + private final Stack hull = new Stack<>(); + public GrahamScan(Point[] points) { // Pre-process points: sort by y-coordinate, then by polar order with respect to the first point Arrays.sort(points); Arrays.sort(points, 1, points.length, points[0].polarOrder()); + hull.push(points[0]); - // Find the first point not equal to points[0]/firstNonEqualIndex and the first point not collinear firstNonCollinearIndex with the previous points - int firstNonEqualIndex; - for (firstNonEqualIndex = 1; firstNonEqualIndex < points.length; firstNonEqualIndex++) { - if (!points[0].equals(points[firstNonEqualIndex])) { - break; + + // Find the first point not equal to points[0]/firstNonEqualIndex + // and the first point not collinear firstNonCollinearIndex with the previous points + int firstNonEqualIndex; + for (firstNonEqualIndex = 1; firstNonEqualIndex < points.length; firstNonEqualIndex++) { + if (!points[0].equals(points[firstNonEqualIndex])) { + break; + } } - } - if (firstNonEqualIndex == points.length) { - return; - } - - int firstNonCollinearIndex; - for (firstNonCollinearIndex = firstNonEqualIndex + 1; firstNonCollinearIndex < points.length; firstNonCollinearIndex++) { - if (Point.orientation(points[0], points[firstNonEqualIndex], points[firstNonCollinearIndex]) != 0) { - break; + + if (firstNonEqualIndex == points.length) { + return; } - } - hull.push(points[firstNonCollinearIndex - 1]); - - // Process the remaining points and update the hull - for (int i = firstNonCollinearIndex; i < points.length; i++) { - Point top = hull.pop(); - while (Point.orientation(hull.peek(), top, points[i]) <= 0) { - top = hull.pop(); + + int firstNonCollinearIndex; + for (firstNonCollinearIndex = firstNonEqualIndex + 1; firstNonCollinearIndex < points.length; firstNonCollinearIndex++) { + if (Point.orientation(points[0], points[firstNonEqualIndex], points[firstNonCollinearIndex]) != 0) { + break; + } + } + + hull.push(points[firstNonCollinearIndex - 1]); + + // Process the remaining points and update the hull + for (int i = firstNonCollinearIndex; i < points.length; i++) { + Point top = hull.pop(); + while (Point.orientation(hull.peek(), top, points[i]) <= 0) { + top = hull.pop(); + } + hull.push(top); + hull.push(points[i]); } - hull.push(top); - hull.push(points[i]); } - + /** * @return An iterable collection of points representing the convex hull. */ From d9341aee75c1008d01990d708b4be3a3e081b408 Mon Sep 17 00:00:00 2001 From: Bayram Turgut <137455737+bayramtturgutt@users.noreply.github.com> Date: Thu, 8 Aug 2024 21:17:35 +0300 Subject: [PATCH 09/13] clang format clang format --- .../com/thealgorithms/geometry/GrahamScan.java | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/thealgorithms/geometry/GrahamScan.java b/src/main/java/com/thealgorithms/geometry/GrahamScan.java index 5b6e1efb6afc..a7b46d9ea879 100644 --- a/src/main/java/com/thealgorithms/geometry/GrahamScan.java +++ b/src/main/java/com/thealgorithms/geometry/GrahamScan.java @@ -21,14 +21,13 @@ public class GrahamScan { private final Stack hull = new Stack<>(); public GrahamScan(Point[] points) { - // Pre-process points: sort by y-coordinate, then by polar order with respect to the first point + //Pre-process points: sort by y-coordinate, then by polar order with respect to the first point Arrays.sort(points); Arrays.sort(points, 1, points.length, points[0].polarOrder()); hull.push(points[0]); - // Find the first point not equal to points[0]/firstNonEqualIndex - // and the first point not collinear firstNonCollinearIndex with the previous points + //Find the first point not equal to points[0]/firstNonEqualIndex and the first point not collinear firstNonCollinearIndex with the previous points int firstNonEqualIndex; for (firstNonEqualIndex = 1; firstNonEqualIndex < points.length; firstNonEqualIndex++) { if (!points[0].equals(points[firstNonEqualIndex])) { @@ -49,7 +48,7 @@ public GrahamScan(Point[] points) { hull.push(points[firstNonCollinearIndex - 1]); - // Process the remaining points and update the hull + //Process the remaining points and update the hull for (int i = firstNonCollinearIndex; i < points.length; i++) { Point top = hull.pop(); while (Point.orientation(hull.peek(), top, points[i]) <= 0) { @@ -136,13 +135,13 @@ public int compare(Point p1, Point p2) { int dy2 = p2.y - y; if (dy1 >= 0 && dy2 < 0) { - return -1; // p1 above p2 + return -1; //p1 above p2 } else if (dy2 >= 0 && dy1 < 0) { - return 1; // p1 below p2 - } else if (dy1 == 0 && dy2 == 0) { // Collinear and horizontal + return 1; //p1 below p2 + } else if (dy1 == 0 && dy2 == 0) { //Collinear and horizontal return Integer.compare(dx2, dx1); } else { - return -orientation(Point.this, p1, p2); // Compare orientation + return -orientation(Point.this, p1, p2); //Compare orientation } } } From d35a6c078d3a3583d1117336fe54145fc092a7c2 Mon Sep 17 00:00:00 2001 From: Bayram Turgut <137455737+bayramtturgutt@users.noreply.github.com> Date: Thu, 8 Aug 2024 21:29:31 +0300 Subject: [PATCH 10/13] r.java --- .../com/thealgorithms/geometry/GrahamScan.java | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/thealgorithms/geometry/GrahamScan.java b/src/main/java/com/thealgorithms/geometry/GrahamScan.java index a7b46d9ea879..0a846913f30a 100644 --- a/src/main/java/com/thealgorithms/geometry/GrahamScan.java +++ b/src/main/java/com/thealgorithms/geometry/GrahamScan.java @@ -1,9 +1,9 @@ package com.thealgorithms.geometry; +import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.Stack; -import java.util.ArrayList; /** * A Java program that computes the convex hull using the Graham Scan algorithm. @@ -21,13 +21,14 @@ public class GrahamScan { private final Stack hull = new Stack<>(); public GrahamScan(Point[] points) { - //Pre-process points: sort by y-coordinate, then by polar order with respect to the first point + // Pre-process points: sort by y-coordinate, then by polar order with respect to the first point Arrays.sort(points); Arrays.sort(points, 1, points.length, points[0].polarOrder()); hull.push(points[0]); - //Find the first point not equal to points[0]/firstNonEqualIndex and the first point not collinear firstNonCollinearIndex with the previous points + // Find the first point not equal to points[0]/firstNonEqualIndex + // and the first point not collinear firstNonCollinearIndex with the previous points int firstNonEqualIndex; for (firstNonEqualIndex = 1; firstNonEqualIndex < points.length; firstNonEqualIndex++) { if (!points[0].equals(points[firstNonEqualIndex])) { @@ -48,7 +49,7 @@ public GrahamScan(Point[] points) { hull.push(points[firstNonCollinearIndex - 1]); - //Process the remaining points and update the hull + // Process the remaining points and update the hull for (int i = firstNonCollinearIndex; i < points.length; i++) { Point top = hull.pop(); while (Point.orientation(hull.peek(), top, points[i]) <= 0) { @@ -135,13 +136,13 @@ public int compare(Point p1, Point p2) { int dy2 = p2.y - y; if (dy1 >= 0 && dy2 < 0) { - return -1; //p1 above p2 + return -1; // p1 above p2 } else if (dy2 >= 0 && dy1 < 0) { - return 1; //p1 below p2 - } else if (dy1 == 0 && dy2 == 0) { //Collinear and horizontal + return 1; // p1 below p2 + } else if (dy1 == 0 && dy2 == 0) { // Collinear and horizontal return Integer.compare(dx2, dx1); } else { - return -orientation(Point.this, p1, p2); //Compare orientation + return -orientation(Point.this, p1, p2); // Compare orientation } } } From 78a7b904035fac4cce7501dbae3ea3122c22f9cc Mon Sep 17 00:00:00 2001 From: Bayram Turgut <137455737+bayramtturgutt@users.noreply.github.com> Date: Thu, 8 Aug 2024 21:30:58 +0300 Subject: [PATCH 11/13] rr.java --- src/main/java/com/thealgorithms/geometry/GrahamScan.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/geometry/GrahamScan.java b/src/main/java/com/thealgorithms/geometry/GrahamScan.java index 0a846913f30a..fd856240e20d 100644 --- a/src/main/java/com/thealgorithms/geometry/GrahamScan.java +++ b/src/main/java/com/thealgorithms/geometry/GrahamScan.java @@ -27,7 +27,7 @@ public GrahamScan(Point[] points) { hull.push(points[0]); - // Find the first point not equal to points[0]/firstNonEqualIndex + // Find the first point not equal to points[0]//firstNonEqualIndex // and the first point not collinear firstNonCollinearIndex with the previous points int firstNonEqualIndex; for (firstNonEqualIndex = 1; firstNonEqualIndex < points.length; firstNonEqualIndex++) { From e3d0f5fa1dd1d45fb53a08d65d2ef4635d6e759b Mon Sep 17 00:00:00 2001 From: Bayram Turgut <137455737+bayramtturgutt@users.noreply.github.com> Date: Thu, 8 Aug 2024 21:32:43 +0300 Subject: [PATCH 12/13] rrr.java --- src/main/java/com/thealgorithms/geometry/GrahamScan.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/geometry/GrahamScan.java b/src/main/java/com/thealgorithms/geometry/GrahamScan.java index fd856240e20d..3bd9a460e082 100644 --- a/src/main/java/com/thealgorithms/geometry/GrahamScan.java +++ b/src/main/java/com/thealgorithms/geometry/GrahamScan.java @@ -27,7 +27,7 @@ public GrahamScan(Point[] points) { hull.push(points[0]); - // Find the first point not equal to points[0]//firstNonEqualIndex + // Find the first point not equal to points[0] firstNonEqualIndex // and the first point not collinear firstNonCollinearIndex with the previous points int firstNonEqualIndex; for (firstNonEqualIndex = 1; firstNonEqualIndex < points.length; firstNonEqualIndex++) { From d3619a4902b23c0924e1e7c4a3b7733900514d39 Mon Sep 17 00:00:00 2001 From: Bayram Turgut <137455737+bayramtturgutt@users.noreply.github.com> Date: Thu, 8 Aug 2024 21:35:59 +0300 Subject: [PATCH 13/13] rrrr.java --- src/main/java/com/thealgorithms/geometry/GrahamScan.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/geometry/GrahamScan.java b/src/main/java/com/thealgorithms/geometry/GrahamScan.java index 3bd9a460e082..1a36137895e0 100644 --- a/src/main/java/com/thealgorithms/geometry/GrahamScan.java +++ b/src/main/java/com/thealgorithms/geometry/GrahamScan.java @@ -27,7 +27,7 @@ public GrahamScan(Point[] points) { hull.push(points[0]); - // Find the first point not equal to points[0] firstNonEqualIndex + // Find the first point not equal to points[0] (firstNonEqualIndex) // and the first point not collinear firstNonCollinearIndex with the previous points int firstNonEqualIndex; for (firstNonEqualIndex = 1; firstNonEqualIndex < points.length; firstNonEqualIndex++) {