|
2 | 2 |
|
3 | 3 | import java.util.ArrayList; |
4 | 4 | import java.util.Arrays; |
5 | | -import java.util.Comparator; |
6 | 5 | import java.util.Stack; |
7 | 6 |
|
8 | 7 | /** |
@@ -66,93 +65,4 @@ public GrahamScan(Point[] points) { |
66 | 65 | public Iterable<Point> hull() { |
67 | 66 | return new ArrayList<>(hull); |
68 | 67 | } |
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 | | - } |
158 | 68 | } |
0 commit comments