From cc0fb2a718a3981332328b75ce2a86b4610b161d Mon Sep 17 00:00:00 2001 From: Artyom Degtyarev Date: Thu, 16 Feb 2023 18:47:42 +0300 Subject: [PATCH] non-existence of tangents throws exception --- .../kscience/kmath/trajectory/tangent.kt | 31 +++++++++++-------- .../kscience/kmath/trajectory/TangentTest.kt | 25 +++++++++++++++ 2 files changed, 43 insertions(+), 13 deletions(-) diff --git a/kmath-trajectory/src/commonMain/kotlin/space/kscience/kmath/trajectory/tangent.kt b/kmath-trajectory/src/commonMain/kotlin/space/kscience/kmath/trajectory/tangent.kt index cf5bcd11d..945b140c2 100644 --- a/kmath-trajectory/src/commonMain/kotlin/space/kscience/kmath/trajectory/tangent.kt +++ b/kmath-trajectory/src/commonMain/kotlin/space/kscience/kmath/trajectory/tangent.kt @@ -38,20 +38,25 @@ public fun Circle2D.tangentsToCircle( } 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)) - put( - route, - LineSegment( - center + w * r1, - other.center + w * r2 + if (d * d > r * r) { + 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)) + put( + route, + LineSegment( + center + w * r1, + other.center + w * r2 + ) ) - ) + } + else { + throw Exception("Circles should not be") + } } } } \ No newline at end of file diff --git a/kmath-trajectory/src/commonTest/kotlin/space/kscience/kmath/trajectory/TangentTest.kt b/kmath-trajectory/src/commonTest/kotlin/space/kscience/kmath/trajectory/TangentTest.kt index 42442a55f..3e87b47de 100644 --- a/kmath-trajectory/src/commonTest/kotlin/space/kscience/kmath/trajectory/TangentTest.kt +++ b/kmath-trajectory/src/commonTest/kotlin/space/kscience/kmath/trajectory/TangentTest.kt @@ -13,6 +13,7 @@ import space.kscience.kmath.geometry.equalsLine import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertTrue +import kotlin.test.assertFailsWith class TangentTest { @Test @@ -53,4 +54,28 @@ class TangentTest { assertTrue(segments[i].equalsLine(Euclidean2DSpace, tangentMapValues[i])) } } + + @Test + fun nonExistingTangents() { + assertFailsWith { + val c1 = Circle2D(vector(0.0, 0.0), 10.0) + val c2 = Circle2D(vector(0.0, 0.0), 1.0) + val segments = c1.tangentsToCircle(c2) + } + assertFailsWith { + val c1 = Circle2D(vector(0.0, 0.0), 1.0) + val c2 = Circle2D(vector(0.0, 0.0), 10.0) + val segments = c1.tangentsToCircle(c2) + } + assertFailsWith { + val c1 = Circle2D(vector(0.0, 0.0), 1.0) + val c2 = Circle2D(vector(2.0, 0.0), 1.0) + val segments = c1.tangentsToCircle(c2) + } + assertFailsWith { + val c1 = Circle2D(vector(0.0, 0.0), 1.0) + val c2 = Circle2D(vector(0.5, 0.0), 1.0) + val segments = c1.tangentsToCircle(c2) + } + } } \ No newline at end of file