import java.util.ArrayList;
class ClosestPoints
{
{
CoordSystem coords = new CoordSystem();
coords.
addPoint(new Point(3,
4)); coords.
addPoint(new Point(1,
2)); coords.
addPoint(new Point(-3,
0)); coords.
addPoint(new Point(7,
-5)); coords.
addPoint(new Point(1,
0)); coords.
addPoint(new Point(8,
-4));
CoordSystem closestPoints = Distance.minimumDistance(coords);
System.
out.
println("The closest points are:"); Display.displayPoints(closestPoints);
System.
out.
print("The distance between those points is: "); System.
out.
println(Distance.
distance(closestPoints.
getPoint(0), closestPoints.
getPoint(1))); }
}
class Display
{
public static void displayPoints(CoordSystem coords)
{
for (int i = 0; i < coords.numberOfPoints(); i++)
System.
out.
println(coords.
getPoint(i
)); }
}
class Distance
{
public static double distance
(Point pointOne,
Point pointTwo
) {
int xDiff, yDiff;
xDiff = pointOne.getX() - pointTwo.getX();
yDiff = pointOne.getY() - pointTwo.getY();
return Math.
sqrt(xDiff
* xDiff
+ yDiff
* yDiff
); }
public static CoordSystem minimumDistance(CoordSystem points)
{
CoordSystem minDistPoints = new CoordSystem();
double minDist
= Double.
MAX_VALUE;
double dist;
for (int i = 0; i < points.numberOfPoints(); i++)
{
pointOne = points.getPoint(i);
for (int j = 0; j < points.numberOfPoints(); j++)
{
if (i != j)
{
pointTwo = points.getPoint(j);
dist = distance(pointOne, pointTwo);
if (dist < minDist) {
minDistPoints = new CoordSystem();
minDistPoints.addPoint(pointOne);
minDistPoints.addPoint(pointTwo);
minDist = dist;
}
}
}
}
return minDistPoints;
}
}
class CoordSystem
{
private ArrayList<Point> points;
CoordSystem()
{
points = new ArrayList<Point>();
}
public ArrayList<Point> getAllPoints()
{ return points; }
public Point getPoint
(int i
) { return points.get(i); }
public CoordSystem getPointsWithX(int x)
{
CoordSystem matches = new CoordSystem();
for (int i = 0; i < points.size(); i++)
{
point = getPoint(i);
if (point.getX() == x)
matches.addPoint(point);
}
return matches;
}
public CoordSystem getPointsWithY(int y)
{
CoordSystem matches = new CoordSystem();
for (int i = 0; i < points.size(); i++)
{
point = getPoint(i);
if (point.getY() == y)
matches.addPoint(point);
}
return matches;
}
public void addPoint
(Point point
) {
points.add(point);
}
public boolean hasMatchingPoint
(Point other
) {
for (int i = 0; i < points.size(); i++)
{
if (getPoint(i) == other)
return true;
}
return false;
}
public int numberOfPoints()
{ return points.size(); }
}
{
private int x;
private int y;
{
x = 0;
y = 0;
}
{
x = x_;
y = y_;
}
public int getX()
{ return x; }
public int getY()
{ return y; }
public boolean equals
(Object obj
) {
return x == other.x && y == other.y;
}
{ return "(" + x + ", " + y + ")"; }
}