File: Bearing.java

package info (click to toggle)
gpsprune 26.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,824 kB
  • sloc: java: 52,154; sh: 25; makefile: 21; python: 15
file content (39 lines) | stat: -rw-r--r-- 1,578 bytes parent folder | download | duplicates (4)
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
package tim.prune.data;

public abstract class Bearing
{
	/**
	 * Calculate the initial bearing from one point to another
	 * @param inFromPoint starting point
	 * @param inToPoint finishing point
	 * @return initial angle in degrees clockwise from North
	 */
	public static double calculateDegrees(DataPoint inFromPoint, DataPoint inToPoint)
	{
		double deltaLong = Math.toRadians(inToPoint.getLongitude().getDouble()
				- inFromPoint.getLongitude().getDouble());
		double sinDeltaLong = Math.sin(deltaLong);
		double cosDeltaLong = Math.cos(deltaLong);
		double lat1 = Math.toRadians(inFromPoint.getLatitude().getDouble());
		double lat2 = Math.toRadians(inToPoint.getLatitude().getDouble());
		double angleRadians = Math.atan2(sinDeltaLong * Math.cos(lat2),
			Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1) * Math.cos(lat2) * cosDeltaLong);
		return Math.toDegrees(angleRadians);
	}

	public static double calculateDegreeChange(DataPoint inFromPoint, DataPoint inMidPoint, DataPoint inToPoint)
	{
		if (inFromPoint == null || inMidPoint == null || inToPoint == null) {
			return 180.0; // cannot calculate
		}
		final double firstAngle = calculateDegrees(inFromPoint, inMidPoint);
		final double secondAngle = calculateDegrees(inMidPoint, inToPoint);
		return angleDifferenceDegrees(firstAngle, secondAngle);
	}

	public static double angleDifferenceDegrees(double inFirstDeg, double inSecondDeg)
	{
		double angleDiff = Math.abs(inFirstDeg - inSecondDeg) % 360.0;
		return angleDiff > 180.0 ? (360.0 - angleDiff) : angleDiff;
	}
}