From 838e5f77478e5648769439e191e3ff0a8e6405ab Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 22 Feb 2021 20:12:59 +0000 Subject: Add meandering support Add rail links between existing nodes and arbitrary points --- utility/maths.cpp | 28 ++++++++++++++++++++++++++++ utility/maths.h | 2 ++ 2 files changed, 30 insertions(+) (limited to 'utility') diff --git a/utility/maths.cpp b/utility/maths.cpp index 0bd3eac..74a3085 100644 --- a/utility/maths.cpp +++ b/utility/maths.cpp @@ -3,6 +3,8 @@ #include #include #include +#include +#include glm::mat4 flat_orientation(const glm::vec3 & diff) @@ -57,3 +59,29 @@ Arc::Arc(const glm::vec3 & centre3, const glm::vec3 & e0p, const glm::vec3 & e1p }()) { } + +std::pair +find_arc_centre(glm::vec2 as, float entrys, glm::vec2 bs, float entrye) +{ + if (as == bs) { + return {as, false}; + } + for (const auto lr : {1.F, -1.F}) { // left or right turn (maybe possible with removal of positve check below) + const auto perps = entrys + (half_pi * lr); + const auto perpe = entrye - (half_pi * lr); + const glm::vec2 ad {std::sin(perps), std::cos(perps)}; + const glm::vec2 bd {std::sin(perpe), std::cos(perpe)}; + + const auto dx = bs.x - as.x; + const auto dy = bs.y - as.y; + const auto det = bd.x * ad.y - bd.y * ad.x; + if (det != 0) { // near parallel line will yield noisy results + const auto u = (dy * bd.x - dx * bd.y) / det; + const auto v = (dy * ad.x - dx * ad.y) / det; + if (u >= 0 && v >= 0) { + return {as + ad * u, lr < 0}; + } + } + } + throw std::runtime_error("no intersection"); +} diff --git a/utility/maths.h b/utility/maths.h index f2114ef..996708f 100644 --- a/utility/maths.h +++ b/utility/maths.h @@ -47,4 +47,6 @@ arc_length(const Arc & arc) float normalize(float ang); +std::pair find_arc_centre(glm::vec2 start, float entrys, glm::vec2 end, float entrye); + #endif -- cgit v1.2.3