non-existence of tangents throws exception

This commit is contained in:
Artyom Degtyarev 2023-02-16 18:47:42 +03:00
parent ed4aa47913
commit cc0fb2a718
2 changed files with 43 additions and 13 deletions

View File

@ -38,6 +38,7 @@ public fun Circle2D.tangentsToCircle(
} else { } else {
r1.absoluteValue + r2.absoluteValue r1.absoluteValue + r2.absoluteValue
} }
if (d * d > r * r) {
val l = (d * d - r * r).pow(0.5) val l = (d * d - r * r).pow(0.5)
angle2 = if (r1.absoluteValue > r2.absoluteValue) { angle2 = if (r1.absoluteValue > r2.absoluteValue) {
angle1 + r1.sign * atan2(r.absoluteValue, l) angle1 + r1.sign * atan2(r.absoluteValue, l)
@ -53,5 +54,9 @@ public fun Circle2D.tangentsToCircle(
) )
) )
} }
else {
throw Exception("Circles should not be")
}
}
} }
} }

View File

@ -13,6 +13,7 @@ import space.kscience.kmath.geometry.equalsLine
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertEquals import kotlin.test.assertEquals
import kotlin.test.assertTrue import kotlin.test.assertTrue
import kotlin.test.assertFailsWith
class TangentTest { class TangentTest {
@Test @Test
@ -53,4 +54,28 @@ class TangentTest {
assertTrue(segments[i].equalsLine(Euclidean2DSpace, tangentMapValues[i])) assertTrue(segments[i].equalsLine(Euclidean2DSpace, tangentMapValues[i]))
} }
} }
@Test
fun nonExistingTangents() {
assertFailsWith<Exception> {
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<Exception> {
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<Exception> {
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<Exception> {
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)
}
}
} }