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:
@@ -2,7 +2,6 @@ package com.thealgorithms.geometry;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.Stack;
|
||||
|
||||
/**
|
||||
@@ -66,93 +65,4 @@ public class GrahamScan {
|
||||
public Iterable<Point> hull() {
|
||||
return new ArrayList<>(hull);
|
||||
}
|
||||
|
||||
public record Point(int x, int y) implements Comparable<Point> {
|
||||
|
||||
/**
|
||||
* Default constructor
|
||||
* @param x x-coordinate
|
||||
* @param y y-coordinate
|
||||
*/
|
||||
public Point {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the x-coordinate
|
||||
*/
|
||||
@Override
|
||||
public int x() {
|
||||
return x;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the y-coordinate
|
||||
*/
|
||||
@Override
|
||||
public int y() {
|
||||
return 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
|
||||
* @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);
|
||||
return Integer.compare(val, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 cmpY = Integer.compare(this.y, p2.y);
|
||||
return cmpY != 0 ? cmpY : Integer.compare(this.x, p2.x);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a comparator to sort points by their polar order relative to this point.
|
||||
*
|
||||
* @return A polar order comparator
|
||||
*/
|
||||
public Comparator<Point> polarOrder() {
|
||||
return new PolarOrder();
|
||||
}
|
||||
|
||||
private final class PolarOrder implements Comparator<Point> {
|
||||
@Override
|
||||
public int compare(Point p1, Point p2) {
|
||||
int dx1 = p1.x - x;
|
||||
int dy1 = p1.y - y;
|
||||
int dx2 = p2.x - x;
|
||||
int dy2 = p2.y - y;
|
||||
|
||||
if (dy1 >= 0 && dy2 < 0) {
|
||||
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 Integer.compare(dx2, dx1);
|
||||
} else {
|
||||
return -orientation(Point.this, p1, p2); // Compare orientation
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return A string representation of this point in the format (x, y)
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("(%d, %d)", x, y);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user