25 lines
650 B
Kotlin
25 lines
650 B
Kotlin
package org.pupes.mhdrunpathfinder
|
|
|
|
import kotlin.math.PI
|
|
import kotlin.math.ln
|
|
import kotlin.math.tan
|
|
|
|
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))
|
|
} |