tangentsToCircle fixed

This commit is contained in:
Artyom Degtyarev 2023-02-15 15:06:47 +03:00
parent bef317677c
commit d535a2155f
5 changed files with 25 additions and 88 deletions

View File

@ -18,7 +18,11 @@ public data class Circle2D(
public val radius: Double public val radius: Double
) )
public fun Circle2D.tangentsToCircle(other: Circle2D): kotlin.collections.MutableMap<String, LineSegment<DoubleVector2D>> { public enum class DubinsRoutes {
RSR, RSL, LSR, LSL
}
public fun Circle2D.tangentsToCircle(other: Circle2D): Map<DubinsRoutes, LineSegment<DoubleVector2D>> {
val R1 = this.radius val R1 = this.radius
val R2 = other.radius val R2 = other.radius
val line = LineSegment(this.center, other.center) 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) val angle1 = atan2(other.center.x - this.center.x, other.center.y - this.center.y)
var r: Double var r: Double
var angle2: Double var angle2: Double
val routes = mapOf("RSR" to Pair(R1, R2), val routes = mapOf(
"RSL" to Pair(R1, -R2), DubinsRoutes.RSR to Pair(R1, R2),
"LSR" to Pair(-R1, R2), DubinsRoutes.RSL to Pair(R1, -R2),
"LSL" to Pair(-R1, -R2)) DubinsRoutes.LSR to Pair(-R1, R2),
val segments = mutableMapOf<String, LineSegment<DoubleVector2D>>() DubinsRoutes.LSL to Pair(-R1, -R2))
val segments = mutableMapOf<DubinsRoutes, LineSegment<DoubleVector2D>>()
for ((route, r1r2) in routes) { for ((route, r1r2) in routes) {
val r1 = r1r2.first val r1 = r1r2.first
val r2 = r1r2.second val r2 = r1r2.second

View File

@ -85,6 +85,13 @@ public object Euclidean2DSpace : GeometrySpace<DoubleVector2D>,
override fun scale(a: DoubleVector2D, value: Double): DoubleVector2D = vector(a.x * value, a.y * value) 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 override fun DoubleVector2D.dot(other: DoubleVector2D): Double = x * other.x + y * other.y
public fun equalLineSegments(line1: LineSegment<DoubleVector2D>, line2: LineSegment<DoubleVector2D>): 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 xAxis: DoubleVector2D = vector(1.0, 0.0)
public val yAxis: DoubleVector2D = vector(0.0, 1.0) public val yAxis: DoubleVector2D = vector(0.0, 1.0)
} }

View File

@ -27,12 +27,6 @@ public fun <V : Vector> LineSegment<V>.line(algebra: GeometrySpace<V>): Line<V>
Line(begin, end - begin) Line(begin, end - begin)
} }
public fun equalLineSegments(line1: LineSegment<DoubleVector2D>, line2: LineSegment<DoubleVector2D>): 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): public fun Double.equalFloat(other: Double, maxFloatDelta: Double = 0.000001):
Boolean = kotlin.math.abs(this - other) < maxFloatDelta Boolean = kotlin.math.abs(this - other) < maxFloatDelta

View File

@ -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<String, LineSegment<DoubleVector2D>> {
// 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<String, LineSegment<DoubleVector2D>>()
// 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
//}

View File

@ -5,6 +5,7 @@
package space.kscience.kmath.geometry package space.kscience.kmath.geometry
import space.kscience.kmath.geometry.Euclidean2DSpace.equalLineSegments
import space.kscience.kmath.geometry.Euclidean2DSpace.vector import space.kscience.kmath.geometry.Euclidean2DSpace.vector
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertEquals import kotlin.test.assertEquals
@ -15,7 +16,12 @@ class TangentTest {
fun tangent() { fun tangent() {
val c1 = Circle2D(vector(0.0, 0.0), 1.0) val c1 = Circle2D(vector(0.0, 0.0), 1.0)
val c2 = Circle2D(vector(4.0, 0.0), 1.0) val c2 = Circle2D(vector(4.0, 0.0), 1.0)
val routes = arrayListOf<String>("RSR", "RSL", "LSR", "LSL") val routes = arrayListOf<DubinsRoutes>(
DubinsRoutes.RSR,
DubinsRoutes.RSL,
DubinsRoutes.LSR,
DubinsRoutes.LSL
)
val segments = arrayListOf<LineSegment<DoubleVector2D>>( val segments = arrayListOf<LineSegment<DoubleVector2D>>(
LineSegment<DoubleVector2D>(begin = vector(0.0, 1.0), LineSegment<DoubleVector2D>(begin = vector(0.0, 1.0),
end = vector(4.0, 1.0)), end = vector(4.0, 1.0)),