refactor: Use Math radian to degrees conversion

This commit is contained in:
Thomas Schwery 2022-04-03 13:36:09 +02:00
parent 4f7687eaab
commit 578f8bdf6e
3 changed files with 20 additions and 38 deletions

View file

@ -1,7 +1,5 @@
package ch.inf3.horizoncut.cli;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import ch.inf3.horizoncut.data.DisplayCalculator;
import ch.inf3.horizoncut.data.DistanceLayer;
import ch.inf3.horizoncut.data.GeoJsonExport;
@ -10,6 +8,8 @@ import ch.inf3.horizoncut.data.Position;
import ch.inf3.horizoncut.data.Tile;
import ch.inf3.horizoncut.data.TileMap;
import ch.inf3.horizoncut.data.TileReader;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
@ -144,7 +144,7 @@ public class Cli {
Collection<Peak> feature;
int txtPosition;
boolean displayInvisible;
if (i == 0) {
feature = peaks;
txtPosition = 150;
@ -154,9 +154,9 @@ public class Cli {
txtPosition = height - 50;
displayInvisible = true;
}
for (Peak peak : feature) {
double peakBearing = DisplayCalculator.radToDeg(Position.bearing(c, peak.position));
double peakBearing = Math.toDegrees(Position.bearing(c, peak.position));
if (!dc.isVisible(peakBearing)) {
LOG.info("Peak {} is not visible at bearing {}", peak.name, peakBearing);
continue;
@ -176,7 +176,7 @@ public class Cli {
LOG.info("Peak {} is not visible, hidden behind terrain.", peak.name);
continue;
}
if (visibleAngle < minVertAngle) {
LOG.info("Peak {} is not visible, too low.", peak.name);
continue;
@ -238,10 +238,10 @@ public class Cli {
return output;
}
public static Collection<Peak> readFeatures(String fileName) throws IOException {
Collection<Peak> peaks = new ArrayList<>();
try ( FileReader fr = new FileReader(fileName)) {
Gson gson = new GsonBuilder().create();
GeoJsonExport export = gson.fromJson(fr, GeoJsonExport.class);
@ -259,7 +259,7 @@ public class Cli {
peaks.add(peak);
}
}
return peaks;
}

View file

@ -68,18 +68,9 @@ public class DisplayCalculator {
} else if (eyeHeight < targetVisibleHeight) {
double op = targetVisibleHeight - eyeHeight;
double ad = targetDistance;
return radToDeg(Math.atan(op / ad)) + 90;
return Math.toDegrees(Math.atan(op / ad)) + 90;
} else {
return radToDeg(Math.atan(targetDistance / (eyeHeight - targetVisibleHeight)));
return Math.toDegrees(Math.atan(targetDistance / (eyeHeight - targetVisibleHeight)));
}
}
public static double degToRad(double angleDeg) {
return Math.PI * angleDeg / 180.0;
}
public static double radToDeg(double angleDeg) {
return angleDeg * 180.0 / Math.PI;
}
}

View file

@ -38,7 +38,7 @@ public class Position {
double λ2 = λ1 + Math.atan2(Math.sin(angleRad) * Math.sin(rDistance) * Math.cos(φ1),
Math.cos(rDistance) - Math.sin(φ1) * Math.sin(φ2));
return new Position(radToDeg(φ2), radToDeg(λ2));
return new Position(Math.toDegrees(φ2), Math.toDegrees(λ2));
}
public int distanceToUnits(Position other) {
@ -57,8 +57,8 @@ public class Position {
public int distanceToMeters(Position other) {
final double R = 6371 * 1000;
double dLat = degToRad(other.getLatitude() - this.getLatitude());
double dLon = degToRad(other.getLongitude()- this.getLongitude());
double dLat = Math.toRadians(other.getLatitude() - this.getLatitude());
double dLon = Math.toRadians(other.getLongitude()- this.getLongitude());
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
+ Math.cos(this.getLatitude() * (Math.PI / 180.0)) * Math.cos(other.getLatitude() * (Math.PI / 180.0))
@ -70,19 +70,19 @@ public class Position {
}
public static double bearing(Position a, Position b) {
double φ1 = a.getLatitudeRad();
double φ2 = b.getLatitudeRad();
double λ1 = a.getLongitudeRad();
double λ2 = b.getLongitudeRad();
double y = Math.sin(λ2 - λ1) * Math.cos(φ2);
double x = Math.cos(φ1) * Math.sin(φ2)
- Math.sin(φ1) * Math.cos(φ2) * Math.cos(λ2 - λ1);
double θ = Math.atan2(y, x);
double brng = (θ + 2 * Math.PI) % (2 * Math.PI);
return brng;
}
@ -109,11 +109,11 @@ public class Position {
}
public double getLatitudeRad() {
return degToRad(getLatitude());
return Math.toRadians(getLatitude());
}
public double getLongitudeRad() {
return degToRad(getLongitude());
return Math.toRadians(getLongitude());
}
public String getLatLon() {
@ -163,13 +163,4 @@ public class Position {
return "Position[gridLat=" + gridLat + ", gridLon=" + gridLon + ", col=" + col + ", row=" + row + "]";
}
static double degToRad(double angleDeg) {
return Math.PI * angleDeg / 180.0;
}
static double radToDeg(double angleDeg) {
return angleDeg * 180.0 / Math.PI;
}
}