Skip to content

Instantly share code, notes, and snippets.

@ramdhanjr11
Last active November 8, 2022 00:34
Show Gist options
  • Select an option

  • Save ramdhanjr11/6101e9c6f7cdf2e9af1f0622fe55ff2e to your computer and use it in GitHub Desktop.

Select an option

Save ramdhanjr11/6101e9c6f7cdf2e9af1f0622fe55ff2e to your computer and use it in GitHub Desktop.
Kode program untuk mencari jarak tempuh (Modul Praktikum GIS)
import kotlin.math.*
// nilai default radius bumi
const val earthRadiusKm: Double = 6372.8
// method untuk melakukan perhitungan dengan metode haversine
fun haversine(latDest: Double, longDest: Double, latCurrent: Double, longCurrent: Double): Double {
val dLat = Math.toRadians(latDest - latCurrent)
val dLon = Math.toRadians(longDest - longCurrent)
val originLat = Math.toRadians(latCurrent)
val destinationLat = Math.toRadians(latDest)
val a = sin(dLat / 2).pow(2.toDouble()) + sin(dLon / 2).pow(2.toDouble()) * cos(originLat) * cos(destinationLat)
val c = 2 * asin(sqrt(a))
return (earthRadiusKm * c) * 1000
}
fun main() {
val vertexWithCoordinates = mapOf(
"V1" to (-6.984021136912581 to 106.54242422961052),
"V2" to (-6.985831502332914 to 106.54349711310161),
"V3" to (-6.985305087423851 to 106.54417958070525),
"V4" to (-6.984990935926289 to 106.5443190555591)
)
printResult(vertexWithCoordinates, "V1", "V2")
}
fun printResult(vertex: Map<String, Pair<Double, Double>>, v1: String, v2: String) {
print("$v1 - $v2 : ")
val result = haversine(
latCurrent = vertex[v1]!!.first,
longCurrent = vertex[v1]!!.second,
latDest = vertex[v2]!!.first,
longDest = vertex[v2]!!.second,
)
print(result)
println()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment