fork download
  1. import java.util.ArrayList;
  2.  
  3. class ClosestPoints
  4. {
  5. public static void main (String[] args) throws java.lang.Exception
  6. {
  7. CoordSystem coords = new CoordSystem();
  8.  
  9. coords.addPoint(new Point(3, 4));
  10. coords.addPoint(new Point(1, 2));
  11. coords.addPoint(new Point(-3, 0));
  12. coords.addPoint(new Point(7, -5));
  13. coords.addPoint(new Point(1, 0));
  14. coords.addPoint(new Point(8, -4));
  15.  
  16. CoordSystem closestPoints = Distance.minimumDistance(coords);
  17.  
  18. System.out.println("The closest points are:");
  19. Display.displayPoints(closestPoints);
  20. System.out.print("The distance between those points is: ");
  21. System.out.println(Distance.distance(closestPoints.getPoint(0), closestPoints.getPoint(1)));
  22. }
  23. }
  24.  
  25. class Display
  26. {
  27. public static void displayPoints(CoordSystem coords)
  28. {
  29. for (int i = 0; i < coords.numberOfPoints(); i++)
  30. System.out.println(coords.getPoint(i));
  31. }
  32. }
  33.  
  34. class Distance
  35. {
  36. public static double distance(Point pointOne, Point pointTwo)
  37. {
  38. int xDiff, yDiff;
  39.  
  40. xDiff = pointOne.getX() - pointTwo.getX();
  41. yDiff = pointOne.getY() - pointTwo.getY();
  42.  
  43. return Math.sqrt(xDiff * xDiff + yDiff * yDiff);
  44. }
  45.  
  46. public static CoordSystem minimumDistance(CoordSystem points)
  47. {
  48. CoordSystem minDistPoints = new CoordSystem();
  49. double minDist = Double.MAX_VALUE;
  50.  
  51. Point pointOne;
  52. Point pointTwo;
  53. double dist;
  54.  
  55. for (int i = 0; i < points.numberOfPoints(); i++)
  56. {
  57. pointOne = points.getPoint(i);
  58.  
  59. for (int j = 0; j < points.numberOfPoints(); j++)
  60. {
  61. if (i != j)
  62. {
  63. pointTwo = points.getPoint(j);
  64. dist = distance(pointOne, pointTwo);
  65. if (dist < minDist) {
  66. minDistPoints = new CoordSystem();
  67. minDistPoints.addPoint(pointOne);
  68. minDistPoints.addPoint(pointTwo);
  69. minDist = dist;
  70. }
  71. }
  72. }
  73. }
  74. return minDistPoints;
  75. }
  76. }
  77.  
  78. class CoordSystem
  79. {
  80. private ArrayList<Point> points;
  81.  
  82. CoordSystem()
  83. {
  84. points = new ArrayList<Point>();
  85. }
  86.  
  87. public ArrayList<Point> getAllPoints()
  88. { return points; }
  89.  
  90. public Point getPoint(int i)
  91. { return points.get(i); }
  92.  
  93. public CoordSystem getPointsWithX(int x)
  94. {
  95. CoordSystem matches = new CoordSystem();
  96.  
  97. Point point;
  98.  
  99. for (int i = 0; i < points.size(); i++)
  100. {
  101. point = getPoint(i);
  102. if (point.getX() == x)
  103. matches.addPoint(point);
  104. }
  105.  
  106. return matches;
  107. }
  108.  
  109. public CoordSystem getPointsWithY(int y)
  110. {
  111. CoordSystem matches = new CoordSystem();
  112.  
  113. Point point;
  114.  
  115. for (int i = 0; i < points.size(); i++)
  116. {
  117. point = getPoint(i);
  118. if (point.getY() == y)
  119. matches.addPoint(point);
  120. }
  121.  
  122. return matches;
  123. }
  124.  
  125. public void addPoint(Point point)
  126. {
  127. points.add(point);
  128. }
  129.  
  130. public boolean hasMatchingPoint(Point other)
  131. {
  132. for (int i = 0; i < points.size(); i++)
  133. {
  134. if (getPoint(i) == other)
  135. return true;
  136. }
  137. return false;
  138. }
  139.  
  140. public int numberOfPoints()
  141. { return points.size(); }
  142. }
  143.  
  144. class Point
  145. {
  146. private int x;
  147. private int y;
  148.  
  149. {
  150. x = 0;
  151. y = 0;
  152. }
  153.  
  154. Point(int x_, int y_)
  155. {
  156. x = x_;
  157. y = y_;
  158. }
  159.  
  160. public int getX()
  161. { return x; }
  162.  
  163. public int getY()
  164. { return y; }
  165.  
  166. public boolean equals(Object obj)
  167. {
  168. Point other = (Point) obj;
  169. return x == other.x && y == other.y;
  170. }
  171.  
  172. public String toString()
  173. { return "(" + x + ", " + y + ")"; }
  174. }
Success #stdin #stdout 0.1s 320576KB
stdin
Standard input is empty
stdout
The closest points are:
(7, -5)
(8, -4)
The distance between those points is: 1.4142135623730951