Advertisement
Guest User

Untitled

a guest
Nov 20th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. package pointDistance;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class PointDistance {
  6.  
  7. public static void main(String[] args) {
  8. // TODO Auto-generated method stub
  9. Scanner input = new Scanner(System.in);
  10. double[][] a = new double[][] { { -1, 3 }, { -1, -1 }, { 1, 1 }, { 2, 0.5 }, { 2, -1 }, { 3, 3 }, { 4, 2 },
  11. { 4, -0.5 } };
  12.  
  13. double minDist = 100000;
  14.  
  15. String indexOfMinDist = new String("");
  16.  
  17. int startIndex = 0;
  18.  
  19. for (int i = 0; i < a.length - 1; i++) {
  20. for (int j = startIndex; j < a.length - 1; j++) {
  21.  
  22. double distance = Math.sqrt(Math.pow(a[i][0] - a[j + 1][0], 2) + Math.pow(a[i][1] - a[j + 1][1], 2));
  23.  
  24. if (minDist > distance) {
  25. indexOfMinDist = "The smallest distance between point: " + a[i][0] + ", " + a[i][1] + " and point: "
  26. + a[j + 1][0] + ", " + a[j + 1][1] + " and the distance is " + distance;
  27. }
  28.  
  29. System.out.println("Distance between point: " + a[i][0] + ", " + a[i][1] + " and point: " + a[j + 1][0]
  30. + ", " + a[j + 1][1] + " and the distance is " + distance);
  31.  
  32. }
  33. startIndex++;
  34. }
  35.  
  36. System.out.println(indexOfMinDist);
  37. }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement