From 1ca1b3cd43efea4479adb3577838bd25d7a5e52b Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Wed, 22 Feb 2023 11:48:44 +0300 Subject: [PATCH] Fix line distance --- .../kotlin/center/sciprog/maps/features/CoordinateSpace.kt | 6 +++--- .../kotlin/center/sciprog/maps/features/Feature.kt | 5 ++--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/maps-kt-features/src/commonMain/kotlin/center/sciprog/maps/features/CoordinateSpace.kt b/maps-kt-features/src/commonMain/kotlin/center/sciprog/maps/features/CoordinateSpace.kt index 800b097..809f81a 100644 --- a/maps-kt-features/src/commonMain/kotlin/center/sciprog/maps/features/CoordinateSpace.kt +++ b/maps-kt-features/src/commonMain/kotlin/center/sciprog/maps/features/CoordinateSpace.kt @@ -66,12 +66,12 @@ public interface CoordinateSpace { public fun T.offsetTo(b: T, zoom: Float): DpOffset - public fun T.distanceTo(b: T, zoom: Float = Float.MAX_VALUE): Dp { - val offset = offsetTo(b, zoom) + public fun T.distanceTo(b: T, zoom: Float): Dp { + val offset: DpOffset = offsetTo(b, zoom) return sqrt(offset.x.value * offset.x.value + offset.y.value * offset.y.value).dp } - public fun T.distanceToLine(a: T, b: T, zoom: Float = Float.MAX_VALUE): Dp { + public fun T.distanceToLine(a: T, b: T, zoom: Float): Dp { val d12 = a.offsetTo(b, zoom) val d01 = offsetTo(a, zoom) val distanceVale = abs(d12.x.value * d01.y.value - d12.y.value * d01.x.value) / a.distanceTo(b, zoom).value diff --git a/maps-kt-features/src/commonMain/kotlin/center/sciprog/maps/features/Feature.kt b/maps-kt-features/src/commonMain/kotlin/center/sciprog/maps/features/Feature.kt index 5fc9bb4..9c3b289 100644 --- a/maps-kt-features/src/commonMain/kotlin/center/sciprog/maps/features/Feature.kt +++ b/maps-kt-features/src/commonMain/kotlin/center/sciprog/maps/features/Feature.kt @@ -140,10 +140,9 @@ public data class LineFeature( val center: T by lazy { getBoundingBox().center } - val length: Dp by lazy { with(space) { a.distanceTo(b) } } - override fun contains(viewPoint: ViewPoint): Boolean = with(space) { - viewPoint.focus.distanceTo(center) <= length / 2 && + val length = a.distanceTo(b, viewPoint.zoom) + viewPoint.focus.distanceTo(center, viewPoint.zoom) <= length / 2 && viewPoint.focus.distanceToLine(a, b, viewPoint.zoom).value <= clickRadius }