Last active
February 28, 2016 23:21
-
-
Save pzp1997/d52bd235f5378a735fc5 to your computer and use it in GitHub Desktop.
Finds the two closest points from a list of points. — http://ideone.com/NaB5Gb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
class ClosestPoints | |
{ | |
public static void main (String[] args) throws java.lang.Exception | |
{ | |
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; | |
Point pointOne; | |
Point pointTwo; | |
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(); | |
Point point; | |
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(); | |
Point point; | |
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(); } | |
} | |
class Point | |
{ | |
private int x; | |
private int y; | |
Point() | |
{ | |
x = 0; | |
y = 0; | |
} | |
Point(int x_, int y_) | |
{ | |
x = x_; | |
y = y_; | |
} | |
public int getX() | |
{ return x; } | |
public int getY() | |
{ return y; } | |
public boolean equals(Object obj) | |
{ | |
Point other = (Point) obj; | |
return x == other.x && y == other.y; | |
} | |
public String toString() | |
{ return "(" + x + ", " + y + ")"; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment