summaryrefslogtreecommitdiff
path: root/utility/maths.cpp
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2021-02-27 20:15:28 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2021-02-27 20:22:35 +0000
commitb0b2d52a2f25d623be2498e31df9939286383722 (patch)
treea7d8c08601983976864bfeea87c6a4193a28f987 /utility/maths.cpp
parentfind_arc_centre given vectors (diff)
downloadilt-b0b2d52a2f25d623be2498e31df9939286383722.tar.bz2
ilt-b0b2d52a2f25d623be2498e31df9939286383722.tar.xz
ilt-b0b2d52a2f25d623be2498e31df9939286383722.zip
Calculate the radius to join to point+direction vector pairs
This uses a mental formula that was derived using symbolabs.com, it works but there just has to be simpler form of it!
Diffstat (limited to 'utility/maths.cpp')
-rw-r--r--utility/maths.cpp30
1 files changed, 30 insertions, 0 deletions
diff --git a/utility/maths.cpp b/utility/maths.cpp
index 866097a..75346a0 100644
--- a/utility/maths.cpp
+++ b/utility/maths.cpp
@@ -83,3 +83,33 @@ find_arc_centre(glm::vec2 as, glm::vec2 ad, glm::vec2 bs, glm::vec2 bd)
}
throw std::runtime_error("no intersection");
}
+
+float
+find_arcs_radius(glm::vec2 start, glm::vec2 ad, glm::vec2 end, glm::vec2 bd)
+{
+ // Short name functions for big forula
+ auto sq = [](auto v) {
+ return v * v;
+ };
+ auto sqrt = [](float v) {
+ return std::sqrt(v);
+ };
+
+ // Calculates path across both arcs along the normals... pythagorean theorem... for some known radius r
+ // (2r)^2 = ((m + (X*r)) - (o + (Z*r)))^2 + ((n + (Y*r)) - (p + (W*r)))^2
+ // According to symbolabs.com equation tool, that solves for r to give:
+ // r=(-2 m X+2 X o+2 m Z-2 o Z-2 n Y+2 Y p+2 n W-2 p W-sqrt((2 m X-2 X o-2 m Z+2 o Z+2 n Y-2 Y p-2 n W+2 p W)^(2)-4
+ // (X^(2)-2 X Z+Z^(2)+Y^(2)-2 Y W+W^(2)-4) (m^(2)-2 m o+o^(2)+n^(2)-2 n p+p^(2))))/(2 (X^(2)-2 X Z+Z^(2)+Y^(2)-2 Y
+ // W+W^(2)-4))
+
+ // These exist cos limitations of online formula rearrangement, and I'm OK with that.
+ const auto &m {start.x}, &n {start.y}, &o {end.x}, &p {end.y};
+ const auto &X {ad.x}, &Y {ad.y}, &Z {bd.x}, &W {bd.y};
+
+ return (2 * m * X - 2 * X * o - 2 * m * Z + 2 * o * Z + 2 * n * Y - 2 * Y * p - 2 * n * W + 2 * p * W
+ - sqrt(sq(-2 * m * X + 2 * X * o + 2 * m * Z - 2 * o * Z - 2 * n * Y + 2 * Y * p + 2 * n * W
+ - 2 * p * W)
+ - (4 * (sq(X) - 2 * X * Z + sq(Z) + sq(Y) - 2 * Y * W + sq(W) - 4)
+ * (sq(m) - 2 * m * o + sq(o) + sq(n) - 2 * n * p + sq(p)))))
+ / (2 * (sq(X) - 2 * X * Z + sq(Z) + sq(Y) - 2 * Y * W + sq(W) - 4));
+}