-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2nd.java
100 lines (65 loc) · 2.73 KB
/
2nd.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import java.text.DecimalFormat;
import java.util.Random;
import java.util.Scanner;
class GeoLocation {
private static int numLocation = 0;
Random random = new Random();
private double lat = 0;
private double lon = 0;
GeoLocation(){
lat = random.nextDouble() * 180 - 90;
lat = (double) Math.round(lat * 1000000d) / 1000000d;
lon = random.nextDouble() * 360 - 180;
lon = (double) Math.round(lon * 1000000d) / 1000000d;
numLocation++;
}
GeoLocation(double lat, double lon){
this.lat = lat;
this.lon = lon;
numLocation++;
}
GeoLocation(GeoLocation copy){
this.lat = copy.lat + random.nextDouble() * 0.2 - 0.1;
this.lat = (double) Math.round(lat * 1000000d) / 1000000d;
this.lon = copy.lon + random.nextDouble() * 0.2 - 0.1;
this.lon = (double) Math.round(lon * 1000000d) / 1000000d;
numLocation++;
}
public void print(){
System.out.println("["+lat+" ; "+lon+"]");
}
public static double distance(GeoLocation one, GeoLocation two){
final int R = 6371;
double lat1 = Math.toRadians(one.lat);
double lon1 = Math.toRadians(one.lon);
double lat2 = Math.toRadians(two.lat);
double lon2 = Math.toRadians(two.lon);
double dLat = lat2 - lat1;
double dLon = lon2 - lon1;
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(lat1) * Math.cos(lat2) *
Math.sin(dLon / 2) * Math.sin(dLon / 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
double herro = R * c;
return Math.round(herro * 10.0) / 10.0; ///////////////////////////////@HERE
}
public static int getNumLocations(){
return numLocation;
}
}
public class Main {
public static void main(String[] args) {
GeoLocation someLocation = new GeoLocation();
GeoLocation vilnius = new GeoLocation(54.683333, 25.283333);
GeoLocation kaunas = new GeoLocation(54.897222, 23.886111);
GeoLocation nearVilnius = new GeoLocation(vilnius);
someLocation.print();
vilnius.print();
nearVilnius.print();
System.out.println("Number of locations: " + GeoLocation.getNumLocations());
System.out.println("From Vilnius to Kaunas: " + GeoLocation.distance(vilnius, kaunas));
System.out.println("From Vilnius to random location: " + GeoLocation.distance(vilnius, someLocation));
System.out.println("From Vilnius to random neighbour: " + GeoLocation.distance(vilnius, nearVilnius));
}
}
/// JAIGU PANAUDOČIAU FLOOR TIES TAG @HERE GAUČIAU REIKIAMA 92.6 TARP VILNIAUS IR KAUNO, NORS TEISINGAS ATSAKYMAS YRA 92.7