Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
indy256 committed Oct 18, 2014
1 parent 46d8296 commit 79b8cec
Showing 1 changed file with 21 additions and 26 deletions.
47 changes: 21 additions & 26 deletions java/src/CircleOperations.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,33 +146,28 @@ static void tangents(Point center2, double r1, double r2, List<Line> lines) {
}

// min enclosing circle in O(n) on average
public static Circle minEnclosingCircle(Point[] pointsArray) {
if (pointsArray.length == 0)
public static Circle minEnclosingCircle(Point[] points) {
if (points.length == 0)
return new Circle(0, 0, 0);
if (pointsArray.length == 1)
return new Circle(pointsArray[0].x, pointsArray[0].y, 0);
List<Point> points = Arrays.asList(pointsArray);
Collections.shuffle(points);
Circle circle = getCircumCircle(points.get(0), points.get(1));
for (int i = 2; i < points.size(); i++)
if (!circle.contains(points.get(i)))
circle = minEnclosingCircleWith1Point(points.subList(0, i), points.get(i));
return circle;
}

static Circle minEnclosingCircleWith1Point(List<Point> points, Point q) {
Circle circle = getCircumCircle(points.get(0), q);
for (int i = 1; i < points.size(); i++)
if (!circle.contains(points.get(i)))
circle = minEnclosingCircleWith2Points(points.subList(0, i), points.get(i), q);
return circle;
}

static Circle minEnclosingCircleWith2Points(List<Point> points, Point q1, Point q2) {
Circle circle = getCircumCircle(q1, q2);
for (Point point : points)
if (!circle.contains(point))
circle = getCircumCircle(q1, q2, point);
if (points.length == 1)
return new Circle(points[0].x, points[0].y, 0);
Collections.shuffle(Arrays.asList(points));
Circle circle = getCircumCircle(points[0], points[1]);
for (int i = 2; i < points.length; i++) {
if (!circle.contains(points[i])) {
circle = getCircumCircle(points[0], points[i]);
for (int j = 1; j < i; j++) {
if (!circle.contains(points[j])) {
circle = getCircumCircle(points[j], points[i]);
for (int k = 0; k < j; k++) {
if (!circle.contains(points[k])) {
circle = getCircumCircle(points[i], points[j], points[k]);
}
}
}
}
}
}
return circle;
}

Expand Down

0 comments on commit 79b8cec

Please sign in to comment.