Skip to content

Instantly share code, notes, and snippets.

@ragavendra
Created September 26, 2025 20:30
Show Gist options
  • Select an option

  • Save ragavendra/4e249014a25ae8be5de7a9a7f067d04b to your computer and use it in GitHub Desktop.

Select an option

Save ragavendra/4e249014a25ae8be5de7a9a7f067d04b to your computer and use it in GitHub Desktop.
Degrees to seconds converter, add and subtract.
import java.util.Arrays;
public class DegMinSec {
/*
// calc deg-min-sec to deg
dd = Math.signum(d) * (Math.abs(d) + (m / 60.0) + (s / 3600.0));
// res is in "ddd.mmss" format
jshell> decimal res = Math.signum(6) * (Math.abs(6) + (18 / 60.0) + (34 / 3600.0));
$48 ==> 6.309444444444444
// the res can be added to some other no, then do below to get d-m-s
d = (int)dd; // Truncate the decimals
t1 = (dd - d) * 60;
m = (int)t1;
s = (t1 - m) * 60;
*/
// res is in "ddd.mmss" format
// get in degrees
public static double degrees(int degree, int minutes, int seconds){
return Math.signum(degree) * (Math.abs(degree) + (minutes / 60.0) + (seconds / 3600.0));
}
public static double degrees(int arr[]){
return degrees(arr[0], arr[1], arr[2]);
}
// get in degrees, minutes and seconds, can be used as ar[0]-ar[1]-ar[2]
public static int[] getGeoCoords(int degree, int minutes, int seconds) {
double resDegrees = degrees(degree, minutes, seconds);
return getGeoCoordsFromDegree(resDegrees);
}
public static int[] getGeoCoords(int arr[]) {
return getGeoCoords(arr[0], arr[1], arr[2]);
}
public static int[] getGeoCoordsFromDegree(double resDegrees) {
int d = (int)resDegrees; // Truncate the decimals
double t1 = (resDegrees - d) * 60;
int m = (int)t1;
double s = (t1 - m) * 60.0;
int ar[] = { d, m, (int) s};
return ar;
}
public static int[] add(int dms1[], int dms2[]) {
double d1 = degrees(dms1);
double d2 = degrees(dms2);
double res = d1 + d2;
// System.out.println("Add is " + res);
// if more than 360 subtract it from 360
if(res > 360)
return minus(getGeoCoordsFromDegree(res), getGeoCoordsFromDegree(degrees(360, 0, 0)));
return getGeoCoordsFromDegree(res);
}
public static int[] minus(int dms1[], int dms2[]) {
double d1 = degrees(dms1);
double d2 = degrees(dms2);
// swap
if(d1 > d2){
double d = d2;
d2 = d1;
d1 = d;
}
double res = d2 - d1;
// System.out.println("Sub is " + res);
return getGeoCoordsFromDegree(res);
}
public static void main(String ags[]) throws Exception {
if (ags.length != 3) {
System.err.println("Usage: java DegMinSec deg min sec");
System.exit(1);
}
int ar1[] = { Integer.parseInt(ags[0]), Integer.parseInt(ags[1]), Integer.parseInt(ags[2])};
System.out.printf("Deg is %s\n", DegMinSec.degrees(ar1));
System.out.printf("Get Coords as arr is %s\n", Arrays.toString(DegMinSec.getGeoCoords(ar1)));
int ar2[] = { 284, 40, 20 };
System.out.printf("Add arr is %s\n", Arrays.toString(DegMinSec.add(ar1, ar2)));
System.out.printf("Minus arr is %s\n", Arrays.toString(DegMinSec.minus(ar1, ar2)));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment