Fix line distance

This commit is contained in:
Alexander Nozik 2023-02-22 11:48:44 +03:00
parent a89a5cf36a
commit 1ca1b3cd43
2 changed files with 5 additions and 6 deletions

View File

@ -66,12 +66,12 @@ public interface CoordinateSpace<T : Any> {
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

View File

@ -140,10 +140,9 @@ public data class LineFeature<T : Any>(
val center: T by lazy { getBoundingBox().center }
val length: Dp by lazy { with(space) { a.distanceTo(b) } }
override fun contains(viewPoint: ViewPoint<T>): 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
}