patternjavaMinor
Nearest pair of points
Viewed 0 times
nearestpairpoints
Problem
Given a set of 2 dimensional points, it returns the two nearest points. If more pairs have the same min-distance between them, then an arbitrary choice is made. This program expects the points to be sorted on the x-axis. If not, input is unpredictable.
I'm looking for code review, optimizations and best practices.
```
final class PointPair {
private final Point point1;
private final Point point2;
private final double distance;
PointPair (Point point1, Point point2, double distance) {
this.point1 = point1;
this.point2 = point2;
this.distance = distance;
}
public Point getPoint1() {
return point1;
}
public Point getPoint2() {
return point2;
}
public double getDistance() {
return distance;
}
}
final class Point {
private final int x;
private final int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
public final class ClosestPair {
private static final int BRUTEFORCE_INDEX = 3;
private ClosestPair() {}
/**
* Given a set of 2 dimensional points it returns the the two nearest points.
* If more pairs have the same min-distance between then then arbitrary choice is made.
* This program expects the points to be sorted on x-axis. If not input is unpredictable.
*
*
* @param points the array of points sorted by x-axis
* @return the pair of points which are nearest to each other.
*/
public static PointPair minPointPair (Point[] points) {
return calcPointPair(points, 0, points.length);
}
private static PointPair calcPointPair(Point[] points, int low, int high) {
assert points != null;
if ((high - low) pointList = new ArrayList();
for (int i = 0; i () {
@Override
public int compare(Point point1, Point point2) {
I'm looking for code review, optimizations and best practices.
```
final class PointPair {
private final Point point1;
private final Point point2;
private final double distance;
PointPair (Point point1, Point point2, double distance) {
this.point1 = point1;
this.point2 = point2;
this.distance = distance;
}
public Point getPoint1() {
return point1;
}
public Point getPoint2() {
return point2;
}
public double getDistance() {
return distance;
}
}
final class Point {
private final int x;
private final int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
public final class ClosestPair {
private static final int BRUTEFORCE_INDEX = 3;
private ClosestPair() {}
/**
* Given a set of 2 dimensional points it returns the the two nearest points.
* If more pairs have the same min-distance between then then arbitrary choice is made.
* This program expects the points to be sorted on x-axis. If not input is unpredictable.
*
*
* @param points the array of points sorted by x-axis
* @return the pair of points which are nearest to each other.
*/
public static PointPair minPointPair (Point[] points) {
return calcPointPair(points, 0, points.length);
}
private static PointPair calcPointPair(Point[] points, int low, int high) {
assert points != null;
if ((high - low) pointList = new ArrayList();
for (int i = 0; i () {
@Override
public int compare(Point point1, Point point2) {
Solution
The
main() method could be replaced with automatized JUnit tests to avoid manual result verification. You could also have more test methods which help defect localization.Context
StackExchange Code Review Q#41259, answer score: 2
Revisions (0)
No revisions yet.