Add extension builders for distance

This commit is contained in:
Alexander Nozik 2022-09-10 18:47:03 +03:00
parent 25e9eed88d
commit 8429577fd8
No known key found for this signature in database
GPG Key ID: F7FCF2DD25C71357
3 changed files with 6 additions and 3 deletions

View File

@ -1,4 +1,3 @@
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
import androidx.compose.desktop.ui.tooling.preview.Preview
import androidx.compose.material.MaterialTheme
import androidx.compose.material.icons.Icons
@ -23,6 +22,7 @@ import kotlinx.coroutines.launch
import java.nio.file.Path
import kotlin.math.PI
import kotlin.random.Random
import center.sciprog.maps.coordinates.kilometers
private fun GeodeticMapCoordinates.toShortString(): String =
"${(latitude.degrees.value).toString().take(6)}:${(longitude.degrees.value).toString().take(6)}"
@ -91,7 +91,7 @@ fun App() {
drawLine(start = Offset(-10f, 10f), end = Offset(10f, -10f), color = Color.Red)
}
arc(pointOne, Distance(10.0), 0f, PI)
arc(pointOne, 10.0.kilometers, 0f, PI)
line(pointOne, pointTwo, id = "line")
text(pointOne, "Home", font = { size = 32f })

View File

@ -3,10 +3,13 @@ package center.sciprog.maps.coordinates
import kotlin.jvm.JvmInline
@JvmInline
public value class Distance(public val kilometers: Double) : Comparable<Distance> {
public value class Distance internal constructor(public val kilometers: Double) : Comparable<Distance> {
override fun compareTo(other: Distance): Int = this.kilometers.compareTo(other.kilometers)
}
public val Number.kilometers: Distance get() = Distance(toDouble())
public val Number.meters: Distance get() = Distance(toDouble() / 1000)
public operator fun Distance.div(other: Distance): Double = kilometers / other.kilometers
public operator fun Distance.plus(other: Distance): Distance = Distance(kilometers + other.kilometers)