opynsim
Unofficial C++ Documentation
Loading...
Searching...
No Matches
rect_functions.h
Go to the documentation of this file.
1#pragma once
2
6
7#include <concepts>
8#include <functional>
9#include <ranges>
10
11namespace osc { struct Circle; }
12
13namespace osc
14{
15 // returns the aspect ratio of `rect`
16 inline float aspect_ratio_of(const Rect& rect)
17 {
18 const auto d = rect.dimensions();
19 return d.x() / d.y();
20 }
21
22 // returns a `Rect` that tightly bounds `x` (i.e. a `Rect` with an area of zero)
23 constexpr Rect bounding_rect_of(const Vector2& x)
24 {
25 return Rect::from_point(x);
26 }
27
28 // returns a `Rect` that tightly bounds `x` and `y`
29 inline Rect bounding_rect_of(const Rect& x, const Vector2& y)
30 {
31 const auto corners = x.corners();
32 return Rect::from_corners(elementwise_min(corners.min, y), elementwise_max(corners.max, y));
33 }
34
35 // returns a `Rect` that tightly bounds `x` and `y`
36 inline Rect bounding_rect_of(const Rect& x, const Rect& y)
37 {
38 const auto x_corners = x.corners();
39 const auto y_corners = y.corners();
40 return Rect::from_corners(
43 );
44 }
45
46 // returns a `Rect` that tightly bounds the `Vector2`s projected from `r`
47 template<
48 std::ranges::input_range R,
49 typename Proj = std::identity
50 >
51 requires std::convertible_to<typename std::projected<std::ranges::iterator_t<R>, Proj>::value_type, const Vector2&>
52 constexpr Rect bounding_rect_of(R&& r, Proj proj = {})
53 {
54 auto it = std::ranges::begin(r);
55 const auto last = std::ranges::end(r);
56 if (it == last) {
57 return Rect{}; // empty range
58 }
59
60 Rect rv = bounding_rect_of(std::invoke(proj, *it));
61 while (++it != last) {
62 rv = bounding_rect_of(rv, std::invoke(proj, *it));
63 }
64 return rv;
65 }
66
67 // returns a `Rect` that tightly bounds `circle`
69
70 // returns a `Rect` that has its bounds clamped between `min` and `max` (inclusive)
71 Rect clamp(const Rect&, const Vector2& min, const Vector2& max);
72}
Definition rect.h:17
static constexpr Rect from_point(const Vector2 &point)
Definition rect.h:21
static Rect from_corners(const Vector2 &p1, const Vector2 &p2)
Definition rect.h:34
RectCorners corners() const
Definition rect.h:89
Vector2 dimensions() const
Definition rect.h:49
constexpr reference x()
Returns (*this)[0]
Definition vector.h:157
Definition custom_decoration_generator.h:5
constexpr Rect bounding_rect_of(const Vector2 &x)
Definition rect_functions.h:23
constexpr auto min(Angle< Rep1, Units1 > x, Angle< Rep2, Units2 > y) -> std::common_type_t< decltype(x), decltype(y)>
Definition angle.h:267
constexpr Angle< Rep, Units > clamp(const Angle< Rep, Units > &v, const AngleMin &min, const AngleMax &max)
Definition angle.h:303
Vector< float, 2 > Vector2
Definition vector.h:636
constexpr auto max(Angle< Rep1, Units1 > x, Angle< Rep2, Units2 > y) -> std::common_type_t< decltype(x), decltype(y)>
Definition angle.h:283
constexpr U to(T &&value)
Definition conversion.h:56
constexpr Vector< T, N > elementwise_max(const Vector< T, N > &x, const Vector< T, N > &y)
Definition common_functions.h:125
constexpr Vector< T, N > elementwise_min(const Vector< T, N > &x, const Vector< T, N > &y)
Definition common_functions.h:111
constexpr FloatingPointResult aspect_ratio_of(Vector< T, 2 > v)
Definition geometric_functions.h:81
Definition circle.h:10