diff --git a/src/typed-geometry/feature/random.hh b/src/typed-geometry/feature/random.hh index 6a33d48cefbd70d88145fdf289e85d21ffc983eb..c2263acfb3fe7635b6766b095f03117c2a1f863f 100644 --- a/src/typed-geometry/feature/random.hh +++ b/src/typed-geometry/feature/random.hh @@ -3,4 +3,5 @@ #include <typed-geometry/functions/random/gaussian.hh> #include <typed-geometry/functions/random/random.hh> #include <typed-geometry/functions/random/random_choice.hh> +#include <typed-geometry/functions/random/shuffle.hh> #include <typed-geometry/functions/random/uniform.hh> diff --git a/src/typed-geometry/functions/objects/intersection.hh b/src/typed-geometry/functions/objects/intersection.hh index ce0a2fe61429195af8391ee5178d13614d5434b5..570dae545c7db327df6862ee2a5308fbd8343e8d 100644 --- a/src/typed-geometry/functions/objects/intersection.hh +++ b/src/typed-geometry/functions/objects/intersection.hh @@ -540,6 +540,14 @@ template <class ScalarT> return {}; } +template <class ScalarT> +[[nodiscard]] constexpr pos<2, ScalarT> intersection(line<2, ScalarT> const& l0, line<2, ScalarT> const& l1) +{ + auto M = tg::mat<2, 2, ScalarT>::from_cols(l0.dir, -l1.dir); + auto t = inverse(M) * (l1.pos - l0.pos); + return l0[t.x]; +} + template <int D, class ScalarT> [[nodiscard]] constexpr optional<aabb<D, ScalarT>> intersection(aabb<D, ScalarT> const& a, aabb<D, ScalarT> const& b) { diff --git a/src/typed-geometry/functions/random/shuffle.hh b/src/typed-geometry/functions/random/shuffle.hh new file mode 100644 index 0000000000000000000000000000000000000000..703aa10ebd9492445bb08aaeb55f95a4b7d55e93 --- /dev/null +++ b/src/typed-geometry/functions/random/shuffle.hh @@ -0,0 +1,22 @@ +#pragma once + +#include <typed-geometry/types/span.hh> + +namespace tg +{ +template <class Rng, class T> +constexpr void shuffle(Rng& rng, span<T> range) +{ + for (size_t i = 1; i < range.size(); ++i) + { + auto j = rng() % (i + 1); + if (i != j) + detail::swap(range[i], range[j]); + } +} +template <class Rng, class Range> +constexpr void shuffle(Rng& rng, Range& range) +{ + shuffle(rng, tg::span(range)); +} +}