From f089879d1b7d0448657789072d1bd969106caac6 Mon Sep 17 00:00:00 2001 From: 2ndbeam <2ndbeam@disroot.org> Date: Tue, 2 Sep 2025 15:39:25 +0300 Subject: [PATCH] MERCATE LOOOOOOOOOOOOOOOOOOOOOON --- .../com/mirenkov/ktheightmap/MainActivity.kt | 8 ++--- .../com/mirenkov/ktheightmap/MapCanvas.kt | 29 ++++++++++++------- .../mirenkov/ktheightmap/SphereMercator.kt | 2 ++ .../com/mirenkov/ktheightmap/TileViewModel.kt | 3 ++ 4 files changed, 27 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/com/mirenkov/ktheightmap/MainActivity.kt b/app/src/main/java/com/mirenkov/ktheightmap/MainActivity.kt index 2d9e05e..4deea14 100644 --- a/app/src/main/java/com/mirenkov/ktheightmap/MainActivity.kt +++ b/app/src/main/java/com/mirenkov/ktheightmap/MainActivity.kt @@ -156,7 +156,6 @@ fun ToolButton(viewModel: TileViewModel) { DropdownMenuItem( text = { Text("Set location") }, onClick = { - expanded = false dialogShown.value = true } ) @@ -205,9 +204,10 @@ fun SetLocationDialog(vm: TileViewModel, dialogShown: MutableState) { } }, confirmButton = { TextButton(onClick = { - Log.i(TAG, "Lat: %.6f, Lon: %.6f".format( - latitudeText.text.toDoubleOrNull() ?: 0.0, longitudeText.text.toDoubleOrNull() ?: 0.0 - )) + val latitude = latitudeText.text.toDoubleOrNull() ?: 0.0 + val longitude = longitudeText.text.toDoubleOrNull() ?: 0.0 + Log.i(TAG, "Lat: %.6f, Lon: %.6f".format(latitude, longitude)) + Log.i(TAG, "X = %.6f".format(SphereMercator.mercateLon(longitude, vm.scale.floatValue.toInt(), -TILE_SIZE.toDouble()))) }) { Text("Confirm".uppercase()) } }, diff --git a/app/src/main/java/com/mirenkov/ktheightmap/MapCanvas.kt b/app/src/main/java/com/mirenkov/ktheightmap/MapCanvas.kt index 09684bc..8aa8619 100644 --- a/app/src/main/java/com/mirenkov/ktheightmap/MapCanvas.kt +++ b/app/src/main/java/com/mirenkov/ktheightmap/MapCanvas.kt @@ -51,6 +51,13 @@ fun MapCanvas( } } ) { + if (viewModel.halvedOffsetX == null) { + viewModel.halvedOffsetX = size.width / 2 + viewModel.halvedOffsetY = size.height / 2 + } + val halvedX = viewModel.halvedOffsetX!! + val halvedY = viewModel.halvedOffsetY!! + drawRect( color = backColor, size = size @@ -67,8 +74,8 @@ fun MapCanvas( if (levelDiff < 0) { repeat (levelDiff.absoluteValue) { - offsetX -= size.width / 2F + TILE_SIZE - offsetY -= size.height / 2F + TILE_SIZE + offsetX -= halvedX + TILE_SIZE + offsetY -= halvedY + TILE_SIZE offsetX /= 2F offsetY /= 2F } @@ -76,15 +83,15 @@ fun MapCanvas( repeat (levelDiff) { offsetX *= 2F offsetY *= 2F - offsetX += size.width / 2F + TILE_SIZE - offsetY += size.height / 2F + TILE_SIZE + offsetX += halvedX + TILE_SIZE + offsetY += halvedY + TILE_SIZE } } val tileOffsetX = (offsetX / TILE_SIZE).toInt() val tileOffsetY = (offsetY / TILE_SIZE).toInt() - val centerTileX = (1 + (offsetX + size.width / 2) / TILE_SIZE).toDouble() - val centerTileY = (1 + (offsetY + size.height / 2) / TILE_SIZE).toDouble() + val centerTileX = (1 + (offsetX + halvedX) / TILE_SIZE).toDouble() + val centerTileY = (1 + (offsetY + halvedY) / TILE_SIZE).toDouble() val strippedOffsetX = offsetX % TILE_SIZE val strippedOffsetY = offsetY % TILE_SIZE @@ -101,7 +108,7 @@ fun MapCanvas( val crossRadius = 24F val additionalSize = if (debug) 96F else 0F - val latLonSize = Size(196F, 96F + additionalSize) + val latLonSize = Size(216F, 96F + additionalSize) val latLonOffset = Offset(16F, 16F) for (cellX in 0 .. gridWidth + 2) { @@ -148,10 +155,10 @@ fun MapCanvas( } val path = Path() - path.moveTo(size.width / 2 - crossRadius, size.height / 2) - path.lineTo(size.width / 2 + crossRadius, size.height / 2) - path.moveTo(size.width / 2, size.height / 2 - crossRadius) - path.lineTo(size.width / 2, size.height / 2 + crossRadius) + path.moveTo(halvedX - crossRadius, halvedY) + path.lineTo(halvedX + crossRadius, halvedY) + path.moveTo(halvedX, halvedY - crossRadius) + path.lineTo(halvedX, halvedY + crossRadius) path.close() drawPath( diff --git a/app/src/main/java/com/mirenkov/ktheightmap/SphereMercator.kt b/app/src/main/java/com/mirenkov/ktheightmap/SphereMercator.kt index 051b375..4d44954 100644 --- a/app/src/main/java/com/mirenkov/ktheightmap/SphereMercator.kt +++ b/app/src/main/java/com/mirenkov/ktheightmap/SphereMercator.kt @@ -52,5 +52,7 @@ class SphereMercator { fun mercateX(x: Double, level: Int): Double = sx2lon((x * TILE_SIZE), level) - 180.0 fun mercateY(y: Double, level: Int): Double = -sy2lat((y - ((1 shl (level - 1)).toDouble() / 2.0)) * TILE_SIZE, level - 1) + + fun mercateLon(longitude: Double, level: Int, offset: Double): Double = lon2sx(longitude + 180.0, level) + offset } } \ No newline at end of file diff --git a/app/src/main/java/com/mirenkov/ktheightmap/TileViewModel.kt b/app/src/main/java/com/mirenkov/ktheightmap/TileViewModel.kt index 4032537..06e829d 100644 --- a/app/src/main/java/com/mirenkov/ktheightmap/TileViewModel.kt +++ b/app/src/main/java/com/mirenkov/ktheightmap/TileViewModel.kt @@ -19,6 +19,9 @@ class TileViewModel(application: Application): ViewModel() { val mapOffsetY = mutableFloatStateOf(-1157.2814F) val scale = mutableFloatStateOf(1F) + var halvedOffsetX: Float? = null + var halvedOffsetY: Float? = null + init { val tileDb = TileDB.getInstance(application) val tileDao = tileDb.tileDao()