2026-01-28 15:00:44 +01:00

30 lines
870 B
Kotlin

package org.pupes.mhdrunpathfinder
import kotlinx.serialization.json.Json
import kotlin.math.PI
import kotlin.math.ln
import kotlin.math.tan
val json = Json {
coerceInputValues = true // Coerce nulls to default values if applicable
ignoreUnknownKeys = true // Ignore fields in JSON not present in the data class
}
fun longitudeToXNormalized(longitude: Double): Double {
return (longitude + 180.0) / 360.0
}
fun latitudeToYNormalized(latitude: Double): Double {
// Clamp latitude to Web Mercator bounds
val lat = latitude.coerceIn(-85.05112878, 85.05112878)
// Convert to radians
val latRad = lat * PI / 180.0
// Web Mercator projection formula
val mercatorY = ln(tan(PI / 4.0 + latRad / 2.0))
// Normalize to 0.0 - 1.0 range
// The mercator Y range is approximately -PI to PI
return 0.5 - (mercatorY / (2.0 * PI))
}