Guest User

Untitled

a guest
Apr 25th, 2019
76
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class Town {
  2. final int id;
  3. final String name;
  4. final double x,y;
  5. private static final double R= 6378.1370D;
  6. private static final double CONVERT_DEGREE_TO_RADIANS = Math.PI/180D;
  7. public Town(int id, String name, double x, double y) {
  8. this.id = id;
  9. this.name = name;
  10. this.x = x*CONVERT_DEGREE_TO_RADIANS;
  11. this.y = y*CONVERT_DEGREE_TO_RADIANS;
  12. }
  13. //distanza dal monumento al monumento inserito
  14.  
  15. public double distanceTo(Town b){
  16. double deltaLat = (b.x - this.x);
  17. double deltaLon = (b.y-this.y);
  18. double a = Math.sin(deltaLat / 2) * Math.sin(deltaLat / 2)
  19. + Math.cos(this.x) * Math.cos(b.x)
  20. * Math.sin(deltaLon / 2) * Math.sin(deltaLon / 2);
  21. double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
  22.  
  23. double distance = R * c * 1000;
  24.  
  25. return Math.sqrt(distance);
  26.  
  27. }
  28.  
  29.  
  30.  
  31. }
RAW Paste Data