mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-12-19 07:00:35 +08:00
feat: Add ConvexHull new algorithm with Junit tests (#5789)
This commit is contained in:
40
src/test/java/com/thealgorithms/geometry/ConvexHullTest.java
Normal file
40
src/test/java/com/thealgorithms/geometry/ConvexHullTest.java
Normal file
@@ -0,0 +1,40 @@
|
||||
package com.thealgorithms.geometry;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class ConvexHullTest {
|
||||
|
||||
@Test
|
||||
void testConvexHullBruteForce() {
|
||||
List<Point> points = Arrays.asList(new Point(0, 0), new Point(1, 0), new Point(10, 1));
|
||||
List<Point> expected = Arrays.asList(new Point(0, 0), new Point(1, 0), new Point(10, 1));
|
||||
assertEquals(expected, ConvexHull.convexHullBruteForce(points));
|
||||
|
||||
points = Arrays.asList(new Point(0, 0), new Point(1, 0), new Point(10, 0));
|
||||
expected = Arrays.asList(new Point(0, 0), new Point(10, 0));
|
||||
assertEquals(expected, ConvexHull.convexHullBruteForce(points));
|
||||
|
||||
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));
|
||||
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));
|
||||
assertEquals(expected, ConvexHull.convexHullBruteForce(points));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testConvexHullRecursive() {
|
||||
List<Point> points = Arrays.asList(new Point(0, 0), new Point(1, 0), new Point(10, 1));
|
||||
List<Point> expected = Arrays.asList(new Point(0, 0), new Point(1, 0), new Point(10, 1));
|
||||
assertEquals(expected, ConvexHull.convexHullRecursive(points));
|
||||
|
||||
points = Arrays.asList(new Point(0, 0), new Point(1, 0), new Point(10, 0));
|
||||
expected = Arrays.asList(new Point(0, 0), new Point(10, 0));
|
||||
assertEquals(expected, ConvexHull.convexHullRecursive(points));
|
||||
|
||||
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));
|
||||
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));
|
||||
assertEquals(expected, ConvexHull.convexHullRecursive(points));
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,7 @@ import org.junit.jupiter.api.Test;
|
||||
public class GrahamScanTest {
|
||||
@Test
|
||||
void testGrahamScan() {
|
||||
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)};
|
||||
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)};
|
||||
String expectedResult = "[(0, 0), (3, 1), (4, 4), (0, 3)]";
|
||||
|
||||
GrahamScan graham = new GrahamScan(points);
|
||||
|
||||
Reference in New Issue
Block a user