Skip to content

Commit

Permalink
Remove some code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
simonpoole committed Nov 4, 2023
1 parent cf966d2 commit a5b040c
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions src/main/java/de/blau/android/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -225,14 +225,7 @@ public static IntCoordinates getCenter(@NonNull final StorageDelegator delegator
*/
@NonNull
public static String getBearingString(@NonNull Context context, double sLon, double sLat, double eLon, double eLat) {
long bearing = GeoMath.bearing(sLon, sLat, eLon, eLat);

int index = (int) (bearing - 22.5);
if (index < 0) {
index += 360;
}
index = index / 45;
return context.getString(bearingStrings[index]);
return context.getString(bearingStrings[getBearingIndex(sLon, sLat, eLon, eLat)]);
}

private static final char[] bearingArrows = { '↗', '→', '↘', '↓', '↙', '←', '↖', '↑' };
Expand All @@ -248,14 +241,27 @@ public static String getBearingString(@NonNull Context context, double sLon, dou
*/
@NonNull
public static char getBearingArrow(double sLon, double sLat, double eLon, double eLat) {
return bearingArrows[getBearingIndex(sLon, sLat, eLon, eLat)];
}

/**
* Calculate the bearing and return an index in to an array holding display values
*
* @param sLon start lon
* @param sLat start lat
* @param eLon end lon
* @param eLat end lat
* @return the index
*/
private static int getBearingIndex(double sLon, double sLat, double eLon, double eLat) {
long bearing = GeoMath.bearing(sLon, sLat, eLon, eLat);

int index = (int) (bearing - 22.5);
if (index < 0) {
index += 360;
}
index = index / 45;
return bearingArrows[index];
return index;
}

/**
Expand Down

0 comments on commit a5b040c

Please sign in to comment.