Skip to content

Instantly share code, notes, and snippets.

@wojciech-zurek
Created September 30, 2019 11:53
Show Gist options
  • Save wojciech-zurek/9f791ba02b34769e1c539faca0dcdd90 to your computer and use it in GitHub Desktop.
Save wojciech-zurek/9f791ba02b34769e1c539faca0dcdd90 to your computer and use it in GitHub Desktop.
Simple conversion between meters and nautical miles and vice versa.
import kotlin.math.roundToInt
import kotlin.test.assertEquals
const val PI = 3.14
const val NAUTICAL_MILE_IN_METERS = 1852
const val MEAN_EARTH_RADIUS = 6371100
fun main(args: Array<String>): Unit {
assertEquals(NAUTICAL_MILE_IN_METERS, toMeters(1))
assertEquals(10 * NAUTICAL_MILE_IN_METERS, toMeters(10))
assertEquals(0.0005399568034557236, toNauticalMiles(1))
assertEquals(0.5399568034557235, toNauticalMiles(1000))
assertEquals(1.0, toNauticalMiles(NAUTICAL_MILE_IN_METERS))
assertEquals(1.079913606911447, toNauticalMiles(2000))
assertEquals(5.399568034557236, toNauticalMiles(10000))
}
fun toMeters(nauticalMiles: Int): Int = nauticalMiles * ((2 * PI * MEAN_EARTH_RADIUS) / (360 * 60)).roundToInt()
fun toNauticalMiles(meters: Int): Double = meters.toDouble() / ((2 * PI * MEAN_EARTH_RADIUS) / (360 * 60)).roundToInt()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment