feat(dtos): Add util function for PathFinder

This commit is contained in:
js0ny 2025-11-27 11:59:25 +00:00
parent 6795612079
commit 221707afb0
3 changed files with 50 additions and 13 deletions

View file

@ -12,35 +12,51 @@ public record Angle(double degrees) {
public Angle { public Angle {
if (degrees < 0 || degrees >= 360) { if (degrees < 0 || degrees >= 360) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Angle must be in range [0, 360). Got: " + degrees "Angle must be in range [0, 360). Got: " + degrees);
);
} }
// Should be a multiple of 22.5 (one of the 16 major directions) // Should be a multiple of 22.5 (one of the 16 major directions)
double remainder = degrees % STEP; double remainder = degrees % STEP;
// Floating point modulo may have tiny errors, e.g. 45.0 % 22.5 could be 0.0 or 1.0e-15 // Floating point modulo may have tiny errors, e.g. 45.0 % 22.5 could be 0.0 or
// So we need to check if the remainder is small enough, or close enough to STEP (handling negative errors) // 1.0e-15
if ( // So we need to check if the remainder is small enough, or close enough to STEP
Math.abs(remainder) > EPSILON && // (handling negative errors)
Math.abs(remainder - STEP) > EPSILON if (Math.abs(remainder) > EPSILON &&
) { Math.abs(remainder - STEP) > EPSILON) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Angle must be a multiple of 22.5 (one of the 16 major directions). Got: " + "Angle must be a multiple of 22.5 (one of the 16 major directions). Got: " +
degrees degrees);
);
} }
} }
public static Angle fromIndex(int index) { public static Angle fromIndex(int index) {
if (index < 0 || index > 15) { if (index < 0 || index > 15) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Direction index must be between 0 and 15" "Direction index must be between 0 and 15");
);
} }
return new Angle(index * STEP); return new Angle(index * STEP);
} }
public static Angle snap(double rawAngle) {
double normalized = normalize(rawAngle);
double snapped = Math.round(normalized / STEP) * STEP;
return new Angle(normalize(snapped));
}
public Angle offset(int increments) {
double rotated = degrees + increments * STEP;
return new Angle(normalize(rotated));
}
private static double normalize(double angle) {
double normalized = angle % 360;
if (normalized < 0) {
normalized += 360;
}
return normalized;
}
public static double toRadians(double degrees) { public static double toRadians(double degrees) {
return Math.toRadians(degrees); return Math.toRadians(degrees);
} }
@ -48,4 +64,5 @@ public record Angle(double degrees) {
public double toRadians() { public double toRadians() {
return Math.toRadians(degrees); return Math.toRadians(degrees);
} }
} }

View file

@ -8,6 +8,8 @@ package io.github.js0ny.ilp_coursework.data.common;
* @param lat latitude of the coordinate/point * @param lat latitude of the coordinate/point
*/ */
public record LngLat(double lng, double lat) { public record LngLat(double lng, double lat) {
private static final double EPSILON = 1e-9;
public LngLat { public LngLat {
if (lat < -90 || lat > 90) { if (lat < -90 || lat > 90) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
@ -25,4 +27,14 @@ public record LngLat(double lng, double lat) {
public LngLat(LngLatAlt coord) { public LngLat(LngLatAlt coord) {
this(coord.lng(), coord.lat()); this(coord.lng(), coord.lat());
} }
public boolean isSamePoint(LngLat other) {
if (other == null) {
return false;
}
return (
Math.abs(lng - other.lng()) < EPSILON &&
Math.abs(lat - other.lat()) < EPSILON
);
}
} }

View file

@ -9,4 +9,12 @@ public record Drone(
String name, String name,
String id, String id,
DroneCapability capability) { DroneCapability capability) {
public int parseId() {
try {
return Integer.parseInt(id);
} catch (NumberFormatException e) {
return id.hashCode();
}
}
} }