Skip to content

Commit 1dfa2e5

Browse files
authored
feat: Add ConvexHull new algorithm with Junit tests (TheAlgorithms#5789)
1 parent bcf4034 commit 1dfa2e5

File tree

6 files changed

+205
-91
lines changed

6 files changed

+205
-91
lines changed

DIRECTORY.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,9 @@
304304
* [WildcardMatching](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/WildcardMatching.java)
305305
* [WineProblem](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/WineProblem.java)
306306
* geometry
307+
* [ConvexHull](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/geometry/ConvexHull.java)
307308
* [GrahamScan](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/geometry/GrahamScan.java)
309+
* [Point](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/geometry/Point.java)
308310
* greedyalgorithms
309311
* [ActivitySelection](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/ActivitySelection.java)
310312
* [BinaryAddition](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/BinaryAddition.java)
@@ -896,6 +898,7 @@
896898
* [WildcardMatchingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/WildcardMatchingTest.java)
897899
* [WineProblemTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/WineProblemTest.java)
898900
* geometry
901+
* [ConvexHullTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/geometry/ConvexHullTest.java)
899902
* [GrahamScanTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/geometry/GrahamScanTest.java)
900903
* greedyalgorithms
901904
* [ActivitySelectionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/ActivitySelectionTest.java)
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package com.thealgorithms.geometry;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.Comparator;
6+
import java.util.HashSet;
7+
import java.util.List;
8+
import java.util.Set;
9+
import java.util.TreeSet;
10+
11+
/**
12+
* A class providing algorithms to compute the convex hull of a set of points
13+
* using brute-force and recursive methods.
14+
*
15+
* Convex hull: The smallest convex polygon that contains all the given points.
16+
*
17+
* Algorithms provided:
18+
* 1. Brute-Force Method
19+
* 2. Recursive (Divide-and-Conquer) Method
20+
*
21+
* @author Hardvan
22+
*/
23+
public final class ConvexHull {
24+
private ConvexHull() {
25+
}
26+
27+
private static boolean checkPointOrientation(Point i, Point j, Point k) {
28+
int detK = Point.orientation(i, j, k);
29+
if (detK > 0) {
30+
return true; // pointsLeftOfIJ
31+
} else if (detK < 0) {
32+
return false; // pointsRightOfIJ
33+
} else {
34+
return k.compareTo(i) >= 0 && k.compareTo(j) <= 0;
35+
}
36+
}
37+
38+
public static List<Point> convexHullBruteForce(List<Point> points) {
39+
Set<Point> convexSet = new TreeSet<>(Comparator.naturalOrder());
40+
41+
for (int i = 0; i < points.size() - 1; i++) {
42+
for (int j = i + 1; j < points.size(); j++) {
43+
boolean allPointsOnOneSide = true;
44+
boolean leftSide = checkPointOrientation(points.get(i), points.get(j), points.get((i + 1) % points.size()));
45+
46+
for (int k = 0; k < points.size(); k++) {
47+
if (k != i && k != j && checkPointOrientation(points.get(i), points.get(j), points.get(k)) != leftSide) {
48+
allPointsOnOneSide = false;
49+
break;
50+
}
51+
}
52+
53+
if (allPointsOnOneSide) {
54+
convexSet.add(points.get(i));
55+
convexSet.add(points.get(j));
56+
}
57+
}
58+
}
59+
60+
return new ArrayList<>(convexSet);
61+
}
62+
63+
public static List<Point> convexHullRecursive(List<Point> points) {
64+
Collections.sort(points);
65+
Set<Point> convexSet = new HashSet<>();
66+
Point leftMostPoint = points.get(0);
67+
Point rightMostPoint = points.get(points.size() - 1);
68+
69+
convexSet.add(leftMostPoint);
70+
convexSet.add(rightMostPoint);
71+
72+
List<Point> upperHull = new ArrayList<>();
73+
List<Point> lowerHull = new ArrayList<>();
74+
75+
for (int i = 1; i < points.size() - 1; i++) {
76+
int det = Point.orientation(leftMostPoint, rightMostPoint, points.get(i));
77+
if (det > 0) {
78+
upperHull.add(points.get(i));
79+
} else if (det < 0) {
80+
lowerHull.add(points.get(i));
81+
}
82+
}
83+
84+
constructHull(upperHull, leftMostPoint, rightMostPoint, convexSet);
85+
constructHull(lowerHull, rightMostPoint, leftMostPoint, convexSet);
86+
87+
List<Point> result = new ArrayList<>(convexSet);
88+
Collections.sort(result);
89+
return result;
90+
}
91+
92+
private static void constructHull(List<Point> points, Point left, Point right, Set<Point> convexSet) {
93+
if (!points.isEmpty()) {
94+
Point extremePoint = null;
95+
int extremePointDistance = Integer.MIN_VALUE;
96+
List<Point> candidatePoints = new ArrayList<>();
97+
98+
for (Point p : points) {
99+
int det = Point.orientation(left, right, p);
100+
if (det > 0) {
101+
candidatePoints.add(p);
102+
if (det > extremePointDistance) {
103+
extremePointDistance = det;
104+
extremePoint = p;
105+
}
106+
}
107+
}
108+
109+
if (extremePoint != null) {
110+
constructHull(candidatePoints, left, extremePoint, convexSet);
111+
convexSet.add(extremePoint);
112+
constructHull(candidatePoints, extremePoint, right, convexSet);
113+
}
114+
}
115+
}
116+
}

src/main/java/com/thealgorithms/geometry/GrahamScan.java

Lines changed: 0 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.util.ArrayList;
44
import java.util.Arrays;
5-
import java.util.Comparator;
65
import java.util.Stack;
76

87
/**
@@ -66,93 +65,4 @@ public GrahamScan(Point[] points) {
6665
public Iterable<Point> hull() {
6766
return new ArrayList<>(hull);
6867
}
69-
70-
public record Point(int x, int y) implements Comparable<Point> {
71-
72-
/**
73-
* Default constructor
74-
* @param x x-coordinate
75-
* @param y y-coordinate
76-
*/
77-
public Point {
78-
}
79-
80-
/**
81-
* @return the x-coordinate
82-
*/
83-
@Override
84-
public int x() {
85-
return x;
86-
}
87-
88-
/**
89-
* @return the y-coordinate
90-
*/
91-
@Override
92-
public int y() {
93-
return y;
94-
}
95-
96-
/**
97-
* Determines the orientation of the triplet (a, b, c).
98-
*
99-
* @param a The first point
100-
* @param b The second point
101-
* @param c The third point
102-
* @return -1 if (a, b, c) is clockwise, 0 if collinear, +1 if counterclockwise
103-
*/
104-
public static int orientation(Point a, Point b, Point c) {
105-
int val = (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
106-
return Integer.compare(val, 0);
107-
}
108-
109-
/**
110-
* Compares this point with another point.
111-
*
112-
* @param p2 The point to compare to
113-
* @return A positive integer if this point is greater, a negative integer if less, or 0 if equal
114-
*/
115-
@Override
116-
public int compareTo(Point p2) {
117-
int cmpY = Integer.compare(this.y, p2.y);
118-
return cmpY != 0 ? cmpY : Integer.compare(this.x, p2.x);
119-
}
120-
121-
/**
122-
* Returns a comparator to sort points by their polar order relative to this point.
123-
*
124-
* @return A polar order comparator
125-
*/
126-
public Comparator<Point> polarOrder() {
127-
return new PolarOrder();
128-
}
129-
130-
private final class PolarOrder implements Comparator<Point> {
131-
@Override
132-
public int compare(Point p1, Point p2) {
133-
int dx1 = p1.x - x;
134-
int dy1 = p1.y - y;
135-
int dx2 = p2.x - x;
136-
int dy2 = p2.y - y;
137-
138-
if (dy1 >= 0 && dy2 < 0) {
139-
return -1; // p1 above p2
140-
} else if (dy2 >= 0 && dy1 < 0) {
141-
return 1; // p1 below p2
142-
} else if (dy1 == 0 && dy2 == 0) { // Collinear and horizontal
143-
return Integer.compare(dx2, dx1);
144-
} else {
145-
return -orientation(Point.this, p1, p2); // Compare orientation
146-
}
147-
}
148-
}
149-
150-
/**
151-
* @return A string representation of this point in the format (x, y)
152-
*/
153-
@Override
154-
public String toString() {
155-
return String.format("(%d, %d)", x, y);
156-
}
157-
}
15868
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.thealgorithms.geometry;
2+
3+
import java.util.Comparator;
4+
5+
public record Point(int x, int y) implements Comparable<Point> {
6+
7+
@Override
8+
public int compareTo(Point other) {
9+
int cmpY = Integer.compare(this.y, other.y);
10+
return cmpY != 0 ? cmpY : Integer.compare(this.x, other.x);
11+
}
12+
13+
@Override
14+
public String toString() {
15+
return String.format("(%d, %d)", x, y);
16+
}
17+
18+
public Comparator<Point> polarOrder() {
19+
return new PolarOrder();
20+
}
21+
22+
public static int orientation(Point a, Point b, Point c) {
23+
return Integer.compare((b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x), 0);
24+
}
25+
26+
private final class PolarOrder implements Comparator<Point> {
27+
@Override
28+
public int compare(Point p1, Point p2) {
29+
int dx1 = p1.x - x;
30+
int dy1 = p1.y - y;
31+
int dx2 = p2.x - x;
32+
int dy2 = p2.y - y;
33+
34+
if (dy1 >= 0 && dy2 < 0) {
35+
return -1; // p1 above p2
36+
} else if (dy2 >= 0 && dy1 < 0) {
37+
return 1; // p1 below p2
38+
} else if (dy1 == 0 && dy2 == 0) { // Collinear and horizontal
39+
return Integer.compare(dx2, dx1);
40+
} else {
41+
return -orientation(Point.this, p1, p2); // Compare orientation
42+
}
43+
}
44+
}
45+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.thealgorithms.geometry;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.Arrays;
6+
import java.util.List;
7+
import org.junit.jupiter.api.Test;
8+
9+
public class ConvexHullTest {
10+
11+
@Test
12+
void testConvexHullBruteForce() {
13+
List<Point> points = Arrays.asList(new Point(0, 0), new Point(1, 0), new Point(10, 1));
14+
List<Point> expected = Arrays.asList(new Point(0, 0), new Point(1, 0), new Point(10, 1));
15+
assertEquals(expected, ConvexHull.convexHullBruteForce(points));
16+
17+
points = Arrays.asList(new Point(0, 0), new Point(1, 0), new Point(10, 0));
18+
expected = Arrays.asList(new Point(0, 0), new Point(10, 0));
19+
assertEquals(expected, ConvexHull.convexHullBruteForce(points));
20+
21+
points = Arrays.asList(new Point(0, 3), new Point(2, 2), new Point(1, 1), new Point(2, 1), new Point(3, 0), new Point(0, 0), new Point(3, 3), new Point(2, -1), new Point(2, -4), new Point(1, -3));
22+
expected = Arrays.asList(new Point(2, -4), new Point(1, -3), new Point(0, 0), new Point(3, 0), new Point(0, 3), new Point(3, 3));
23+
assertEquals(expected, ConvexHull.convexHullBruteForce(points));
24+
}
25+
26+
@Test
27+
void testConvexHullRecursive() {
28+
List<Point> points = Arrays.asList(new Point(0, 0), new Point(1, 0), new Point(10, 1));
29+
List<Point> expected = Arrays.asList(new Point(0, 0), new Point(1, 0), new Point(10, 1));
30+
assertEquals(expected, ConvexHull.convexHullRecursive(points));
31+
32+
points = Arrays.asList(new Point(0, 0), new Point(1, 0), new Point(10, 0));
33+
expected = Arrays.asList(new Point(0, 0), new Point(10, 0));
34+
assertEquals(expected, ConvexHull.convexHullRecursive(points));
35+
36+
points = Arrays.asList(new Point(0, 3), new Point(2, 2), new Point(1, 1), new Point(2, 1), new Point(3, 0), new Point(0, 0), new Point(3, 3), new Point(2, -1), new Point(2, -4), new Point(1, -3));
37+
expected = Arrays.asList(new Point(2, -4), new Point(1, -3), new Point(0, 0), new Point(3, 0), new Point(0, 3), new Point(3, 3));
38+
assertEquals(expected, ConvexHull.convexHullRecursive(points));
39+
}
40+
}

src/test/java/com/thealgorithms/geometry/GrahamScanTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
public class GrahamScanTest {
88
@Test
99
void testGrahamScan() {
10-
GrahamScan.Point[] points = {new GrahamScan.Point(0, 3), new GrahamScan.Point(1, 1), new GrahamScan.Point(2, 2), new GrahamScan.Point(4, 4), new GrahamScan.Point(0, 0), new GrahamScan.Point(1, 2), new GrahamScan.Point(3, 1), new GrahamScan.Point(3, 3)};
10+
Point[] points = {new Point(0, 3), new Point(1, 1), new Point(2, 2), new Point(4, 4), new Point(0, 0), new Point(1, 2), new Point(3, 1), new Point(3, 3)};
1111
String expectedResult = "[(0, 0), (3, 1), (4, 4), (0, 3)]";
1212

1313
GrahamScan graham = new GrahamScan(points);

0 commit comments

Comments
 (0)