-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPoint.java
More file actions
99 lines (79 loc) · 2.68 KB
/
Point.java
File metadata and controls
99 lines (79 loc) · 2.68 KB
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
98
99
import java.time.ZonedDateTime;
import static java.lang.Math.abs;
import static java.lang.Math.atan2;
import static java.lang.Math.cos;
import static java.lang.Math.sin;
import static java.lang.Math.sqrt;
import static java.lang.Math.toRadians;
/**
* Represents a point in space and time, recorded by a GPS sensor.
*
* @author Nick Efford & Eschal Najmi
*/
public class Point {
// Constants useful for bounds checking, etc
private static final double MIN_LONGITUDE = -180.0;
private static final double MAX_LONGITUDE = 180.0;
private static final double MIN_LATITUDE = -90.0;
private static final double MAX_LATITUDE = 90.0;
private static final double MEAN_EARTH_RADIUS = 6.371009e+6;
private double elevation;
private ZonedDateTime time;
private double longitude;
private double latitude;
// TODO: Create a stub for the constructor
public Point(ZonedDateTime t, double lon, double lat, double elev){
time = t;
elevation = elev;
if(lat<MIN_LATITUDE || lat>MAX_LATITUDE){
throw new GPSException("Invalid latitude");
}
if(lon<MIN_LONGITUDE || lon>MAX_LONGITUDE){
throw new GPSException("Invalid longitude");
}
longitude = lon;
latitude = lat;
}
// TODO: Create a stub for getTime()
public ZonedDateTime getTime(){
return time;
}
// TODO: Create a stub for getLatitude()
public double getLatitude(){
return latitude;
}
// TODO: Create a stub for getLongitude()
public double getLongitude(){
return longitude;
}
// TODO: Create a stub for getElevation()
public double getElevation(){
return elevation;
}
// TODO: Create a stub for toString()
public String toString(){
String x = String.format("(%.5f, %.5f), %.1f m", longitude, latitude, elevation);
return x;
}
// IMPORTANT: Do not alter anything beneath this comment!
/**
* Computes the great-circle distance or orthodromic distance between
* two points on a spherical surface, using Vincenty's formula.
*
* @param p First point
* @param q Second point
* @return Distance between the points, in metres
*/
public static double greatCircleDistance(Point p, Point q) {
double phi1 = toRadians(p.getLatitude());
double phi2 = toRadians(q.getLatitude());
double lambda1 = toRadians(p.getLongitude());
double lambda2 = toRadians(q.getLongitude());
double delta = abs(lambda1 - lambda2);
double firstTerm = cos(phi2)*sin(delta);
double secondTerm = cos(phi1)*sin(phi2) - sin(phi1)*cos(phi2)*cos(delta);
double top = sqrt(firstTerm*firstTerm + secondTerm*secondTerm);
double bottom = sin(phi1)*sin(phi2) + cos(phi1)*cos(phi2)*cos(delta);
return MEAN_EARTH_RADIUS * atan2(top, bottom);
}
}