diff --git a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/Circle2D.kt b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/Circle2D.kt index a26a592da..df25dcbea 100644 --- a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/Circle2D.kt +++ b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/Circle2D.kt @@ -18,7 +18,11 @@ public data class Circle2D( public val radius: Double ) -public fun Circle2D.tangentsToCircle(other: Circle2D): kotlin.collections.MutableMap> { +public enum class DubinsRoutes { + RSR, RSL, LSR, LSL +} + +public fun Circle2D.tangentsToCircle(other: Circle2D): Map> { val R1 = this.radius val R2 = other.radius val line = LineSegment(this.center, other.center) @@ -26,11 +30,12 @@ public fun Circle2D.tangentsToCircle(other: Circle2D): kotlin.collections.Mutabl val angle1 = atan2(other.center.x - this.center.x, other.center.y - this.center.y) var r: Double var angle2: Double - val routes = mapOf("RSR" to Pair(R1, R2), - "RSL" to Pair(R1, -R2), - "LSR" to Pair(-R1, R2), - "LSL" to Pair(-R1, -R2)) - val segments = mutableMapOf>() + val routes = mapOf( + DubinsRoutes.RSR to Pair(R1, R2), + DubinsRoutes.RSL to Pair(R1, -R2), + DubinsRoutes.LSR to Pair(-R1, R2), + DubinsRoutes.LSL to Pair(-R1, -R2)) + val segments = mutableMapOf>() for ((route, r1r2) in routes) { val r1 = r1r2.first val r2 = r1r2.second diff --git a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/Euclidean2DSpace.kt b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/Euclidean2DSpace.kt index 3df8dba7b..dc05f6648 100644 --- a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/Euclidean2DSpace.kt +++ b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/Euclidean2DSpace.kt @@ -85,6 +85,13 @@ public object Euclidean2DSpace : GeometrySpace, override fun scale(a: DoubleVector2D, value: Double): DoubleVector2D = vector(a.x * value, a.y * value) override fun DoubleVector2D.dot(other: DoubleVector2D): Double = x * other.x + y * other.y + public fun equalLineSegments(line1: LineSegment, line2: LineSegment): Boolean { + val maxFloatDelta = 0.000001 + return line1.begin.x.equalFloat(line2.begin.x) && line1.begin.y.equalFloat(line2.begin.y) && + line1.end.x.equalFloat(line2.end.x) && line1.end.y.equalFloat(line2.end.y) + } + + public val xAxis: DoubleVector2D = vector(1.0, 0.0) public val yAxis: DoubleVector2D = vector(0.0, 1.0) } diff --git a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/Line.kt b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/Line.kt index bb03b5a93..6413edce9 100644 --- a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/Line.kt +++ b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/Line.kt @@ -27,12 +27,6 @@ public fun LineSegment.line(algebra: GeometrySpace): Line Line(begin, end - begin) } -public fun equalLineSegments(line1: LineSegment, line2: LineSegment): Boolean { - val maxFloatDelta = 0.000001 - return line1.begin.x.equalFloat(line2.begin.x) && line1.begin.y.equalFloat(line2.begin.y) && - line1.end.x.equalFloat(line2.end.x) && line1.end.y.equalFloat(line2.end.y) -// return line1.begin == line2.begin && line1.end == line2.end -} public fun Double.equalFloat(other: Double, maxFloatDelta: Double = 0.000001): Boolean = kotlin.math.abs(this - other) < maxFloatDelta diff --git a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/Tangent.kt b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/Tangent.kt deleted file mode 100644 index cfcb78115..000000000 --- a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/Tangent.kt +++ /dev/null @@ -1,75 +0,0 @@ -/* - * Copyright 2018-2023 KMath contributors. - * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. - */ - -package space.kscience.kmath.geometry - -import kotlin.math.absoluteValue -import kotlin.math.atan2 -import kotlin.math.pow -import kotlin.math.sign -import kotlin.math.sin -import kotlin.math.cos -import space.kscience.kmath.geometry.Euclidean2DSpace.vector -import space.kscience.kmath.geometry.Euclidean2DSpace.scale -import space.kscience.kmath.geometry.Euclidean2DSpace.add -import space.kscience.kmath.geometry.Euclidean2DSpace.distanceTo - - -//public class Segment( -// public val startPoint: DoubleVector2D, -// public val terminalPoint: DoubleVector2D, -// public val length: Double = startPoint.distanceTo(terminalPoint) -//) { -// public override operator fun equals(other: Any?): Boolean { -// return if (other is Segment) { -// startPoint.x.equalFloat(other.startPoint.x) && startPoint.y.equalFloat(other.startPoint.y) && -// terminalPoint.x.equalFloat(other.terminalPoint.x) && terminalPoint.y.equalFloat(other.terminalPoint.y) -// } else { -// false -// } -// } -//} - -//public const val maxFloatDelta: Double = 0.000001 -//public fun Double.equalFloat(other: Double): Boolean = kotlin.math.abs(this - other) < maxFloatDelta - - - -//public fun tangentsToCircles( -// startCircle: Circle2D, -// terminalCircle: Circle2D -//): kotlin.collections.MutableMap> { -// val R1 = startCircle.radius -// val R2 = terminalCircle.radius -// val line = LineSegment(startCircle.center, terminalCircle.center) -// val d = line.begin.distanceTo(line.end) -// val angle1 = atan2(terminalCircle.center.x - startCircle.center.x, terminalCircle.center.y - startCircle.center.y) -// var r: Double -// var angle2: Double -// val routes = mapOf("RSR" to Pair(R1, R2), -// "RSL" to Pair(R1, -R2), -// "LSR" to Pair(-R1, R2), -// "LSL" to Pair(-R1, -R2)) -// val segments = mutableMapOf>() -// for ((route, r1r2) in routes) { -// val r1 = r1r2.first -// val r2 = r1r2.second -// r = if (r1.sign == r2.sign) { -// r1.absoluteValue - r2.absoluteValue -// } else { -// r1.absoluteValue + r2.absoluteValue -// } -// val L = (d * d - r * r).pow(0.5) -// angle2 = if (r1.absoluteValue > r2.absoluteValue) { -// angle1 + r1.sign * atan2(r.absoluteValue, L) -// } else { -// angle1 - r2.sign * atan2(r.absoluteValue, L) -// } -// val W = vector(-cos(angle2), sin(angle2)) -// segments[route] = LineSegment(add(startCircle.center, scale(W, r1)), -// add(terminalCircle.center, scale(W, r2))) -// } -// return segments -//} \ No newline at end of file diff --git a/kmath-geometry/src/commonTest/kotlin/space/kscience/kmath/geometry/TangentTest.kt b/kmath-geometry/src/commonTest/kotlin/space/kscience/kmath/geometry/TangentTest.kt index fd3002e19..214495dfe 100644 --- a/kmath-geometry/src/commonTest/kotlin/space/kscience/kmath/geometry/TangentTest.kt +++ b/kmath-geometry/src/commonTest/kotlin/space/kscience/kmath/geometry/TangentTest.kt @@ -5,6 +5,7 @@ package space.kscience.kmath.geometry +import space.kscience.kmath.geometry.Euclidean2DSpace.equalLineSegments import space.kscience.kmath.geometry.Euclidean2DSpace.vector import kotlin.test.Test import kotlin.test.assertEquals @@ -15,7 +16,12 @@ class TangentTest { fun tangent() { val c1 = Circle2D(vector(0.0, 0.0), 1.0) val c2 = Circle2D(vector(4.0, 0.0), 1.0) - val routes = arrayListOf("RSR", "RSL", "LSR", "LSL") + val routes = arrayListOf( + DubinsRoutes.RSR, + DubinsRoutes.RSL, + DubinsRoutes.LSR, + DubinsRoutes.LSL + ) val segments = arrayListOf>( LineSegment(begin = vector(0.0, 1.0), end = vector(4.0, 1.0)),