From b0b2d52a2f25d623be2498e31df9939286383722 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 27 Feb 2021 20:15:28 +0000 Subject: 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! --- utility/maths.cpp | 30 ++++++++++++++++++++++++++++++ utility/maths.h | 1 + 2 files changed, 31 insertions(+) (limited to 'utility') 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)); +} diff --git a/utility/maths.h b/utility/maths.h index 2ce5d94..b0048c3 100644 --- a/utility/maths.h +++ b/utility/maths.h @@ -55,5 +55,6 @@ float normalize(float ang); std::pair find_arc_centre(glm::vec2 start, float entrys, glm::vec2 end, float entrye); std::pair find_arc_centre(glm::vec2 start, glm::vec2 ad, glm::vec2 end, glm::vec2 bd); +float find_arcs_radius(glm::vec2 start, glm::vec2 ad, glm::vec2 end, glm::vec2 bd); #endif -- cgit v1.2.3