Skip to content

Commit

Permalink
improve floating point issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Helium314 authored and westnordost committed Jul 19, 2023
1 parent 5646ea1 commit 8c6b138
Showing 1 changed file with 4 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import kotlinx.serialization.Serializable
import kotlin.math.PI
import kotlin.math.asinh
import kotlin.math.atan
import kotlin.math.nextDown
import kotlin.math.nextUp
import kotlin.math.sinh
import kotlin.math.tan

Expand Down Expand Up @@ -114,12 +116,12 @@ private fun tile2lon(x: Int, zoom: Int): Double =
360.0 * x / numTiles(zoom).toDouble() - 180.0

private fun tile2lat(y: Int, zoom: Int): Double =
180.0 * atan(sinh(PI * (1.0 - 2.0 * y / numTiles(zoom)))) / PI
(180.0 / PI * atan(sinh(PI * (1.0 - 2.0 * y / numTiles(zoom))))).nextDown()

private fun lon2tile(lon: Double, zoom: Int): Int =
(numTiles(zoom) * (lon + 180.0) / 360.0).toInt()

private fun lat2tile(lat: Double, zoom: Int): Int =
(numTiles(zoom) * (1.0 - asinh(tan(PI * lat / 180.0)) / PI) / 2.0).toInt()
(numTiles(zoom) * (1.0 - asinh(tan(PI * lat / 180.0)) / PI) / 2.0).nextUp().toInt()

private fun numTiles(zoom: Int): Int = 1 shl zoom

0 comments on commit 8c6b138

Please sign in to comment.