|
| 1 | +package com.thealgorithms.geometry; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | +import java.util.Objects; |
| 6 | + |
| 7 | +/** |
| 8 | + * This class implements the Jarvis March algorithm (also known as the Gift Wrapping algorithm) |
| 9 | + * for computing the convex hull of a set of points in a 2D plane. |
| 10 | + * The convex hull is the smallest convex polygon that can enclose all given points. |
| 11 | + */ |
| 12 | +public final class JarvisMarch { |
| 13 | + |
| 14 | + private JarvisMarch() { |
| 15 | + // Private constructor to prevent instantiation |
| 16 | + } |
| 17 | + |
| 18 | + /** |
| 19 | + * Represents a point in 2D space with x and y coordinates. |
| 20 | + */ |
| 21 | + static class Point { |
| 22 | + private double x; |
| 23 | + private double y; |
| 24 | + |
| 25 | + /** |
| 26 | + * Constructs a Point with specified x and y coordinates. |
| 27 | + * |
| 28 | + * @param x the x-coordinate of the point |
| 29 | + * @param y the y-coordinate of the point |
| 30 | + */ |
| 31 | + public Point(double x, double y) { |
| 32 | + this.x = x; |
| 33 | + this.y = y; |
| 34 | + } |
| 35 | + |
| 36 | + public double getX() { |
| 37 | + return x; |
| 38 | + } |
| 39 | + |
| 40 | + public double getY() { |
| 41 | + return y; |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public boolean equals(Object obj) { |
| 46 | + if (this == obj) return true; // Check if both references point to the same object |
| 47 | + if (!(obj instanceof Point)) return false; // Check if obj is an instance of Point |
| 48 | + Point other = (Point) obj; |
| 49 | + // Compare x and y coordinates for equality |
| 50 | + return Double.compare(x, other.x) == 0 && Double.compare(y, other.y) == 0; |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public int hashCode() { |
| 55 | + return Objects.hash(x, y); // Generate hash code based on x and y coordinates |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Computes the convex hull of a given list of points using the Jarvis March algorithm. |
| 61 | + * |
| 62 | + * @param points a list of Points for which to compute the convex hull |
| 63 | + * @return a list of Points representing the vertices of the convex hull in counter-clockwise order |
| 64 | + */ |
| 65 | + public static List<Point> jarvisMarch(List<Point> points) { |
| 66 | + List<Point> hull = new ArrayList<>(); |
| 67 | + |
| 68 | + // If there are less than 3 points, a convex hull cannot be formed |
| 69 | + if (points.size() < 3) return hull; |
| 70 | + |
| 71 | + // Find the leftmost point (with the smallest x-coordinate) |
| 72 | + Point leftmost = points.get(0); |
| 73 | + for (Point p : points) { |
| 74 | + if (p.getX() < leftmost.getX()) { |
| 75 | + leftmost = p; // Update leftmost point if a new leftmost point is found |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + Point current = leftmost; // Start from the leftmost point |
| 80 | + |
| 81 | + do { |
| 82 | + hull.add(current); // Add current point to the hull |
| 83 | + |
| 84 | + Point nextTarget = points.get(0); // Initialize next target as first point in list |
| 85 | + |
| 86 | + for (Point candidate : points) { |
| 87 | + if (candidate.equals(current)) continue; // Skip current point |
| 88 | + |
| 89 | + // Check if candidate makes a left turn or is collinear and farther than nextTarget |
| 90 | + if ((nextTarget.equals(current) || isLeftTurn(current, nextTarget, candidate)) || (isCollinear(current, nextTarget, candidate) && distance(current, candidate) > distance(current, nextTarget))) { |
| 91 | + nextTarget = candidate; // Update next target if conditions are met |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + current = nextTarget; // Move to the next target point |
| 96 | + |
| 97 | + } while (!current.equals(leftmost)); // Continue until we loop back to the starting point |
| 98 | + |
| 99 | + return hull; // Return the computed convex hull |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Determines whether moving from point A to point B to point C makes a left turn. |
| 104 | + * |
| 105 | + * @param a the starting point |
| 106 | + * @param b the second point |
| 107 | + * @param c the third point |
| 108 | + * @return true if it makes a left turn, false otherwise |
| 109 | + */ |
| 110 | + private static boolean isLeftTurn(Point a, Point b, Point c) { |
| 111 | + return (b.getX() - a.getX()) * (c.getY() - a.getY()) - (b.getY() - a.getY()) * (c.getX() - a.getX()) > 0; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Checks whether three points A, B, and C are collinear. |
| 116 | + * |
| 117 | + * @param a the first point |
| 118 | + * @param b the second point |
| 119 | + * @param c the third point |
| 120 | + * @return true if points are collinear, false otherwise |
| 121 | + */ |
| 122 | + private static boolean isCollinear(Point a, Point b, Point c) { |
| 123 | + return (b.getX() - a.getX()) * (c.getY() - a.getY()) == (b.getY() - a.getY()) * (c.getX() - a.getX()); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Calculates the Euclidean distance between two points A and B. |
| 128 | + * |
| 129 | + * @param a the first point |
| 130 | + * @param b the second point |
| 131 | + * @return the distance between points A and B |
| 132 | + */ |
| 133 | + private static double distance(Point a, Point b) { |
| 134 | + return Math.sqrt(Math.pow(b.getX() - a.getX(), 2) + Math.pow(b.getY() - a.getY(), 2)); |
| 135 | + } |
| 136 | +} |
0 commit comments