HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
point2.hpp
1// Copyright Take Vos 2021-2022.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "vector2.hpp"
8#include "extent2.hpp"
9#include "../SIMD/module.hpp"
10#include "../utility/module.hpp"
11#include <format>
12#include <concepts>
13
14namespace hi::inline v1 {
15
23class point2 {
24public:
25 using array_type = simd<float, 4>;
26 using value_type = array_type::value_type;
27
28 constexpr point2(point2 const&) noexcept = default;
29 constexpr point2(point2&&) noexcept = default;
30 constexpr point2& operator=(point2 const&) noexcept = default;
31 constexpr point2& operator=(point2&&) noexcept = default;
32
35 [[nodiscard]] constexpr explicit operator array_type() const noexcept
36 {
37 return _v;
38 }
39
42 [[nodiscard]] constexpr explicit point2(array_type const& other) noexcept : _v(other)
43 {
44 hi_axiom(holds_invariant());
45 }
46
49 [[nodiscard]] constexpr point2() noexcept : _v(0.0f, 0.0f, 0.0f, 1.0f) {}
50
55 [[nodiscard]] constexpr point2(float x, float y) noexcept : _v(x, y, 0.0f, 1.0f) {}
56
60 [[nodiscard]] constexpr float& x() noexcept
61 {
62 return _v.x();
63 }
64
68 [[nodiscard]] constexpr float& y() noexcept
69 {
70 return _v.y();
71 }
72
76 [[nodiscard]] constexpr float x() const noexcept
77 {
78 return _v.x();
79 }
80
84 [[nodiscard]] constexpr float y() const noexcept
85 {
86 return _v.y();
87 }
88
89 constexpr point2& operator+=(vector2 const& rhs) noexcept
90 {
91 return *this = *this + rhs;
92 }
93
94 constexpr point2& operator-=(vector2 const& rhs) noexcept
95 {
96 return *this = *this - rhs;
97 }
98
104 [[nodiscard]] constexpr friend point2 operator+(point2 const& lhs, vector2 const& rhs) noexcept
105 {
106 return point2{lhs._v + array_type{rhs}};
107 }
108
114 [[nodiscard]] constexpr friend point2 operator+(vector2 const& lhs, point2 const& rhs) noexcept
115 {
116 return point2{array_type{lhs} + rhs._v};
117 }
118
124 [[nodiscard]] constexpr friend point2 operator-(point2 const& lhs, vector2 const& rhs) noexcept
125 {
126 return point2{lhs._v - array_type{rhs}};
127 }
128
134 [[nodiscard]] constexpr friend vector2 operator-(point2 const& lhs, point2 const& rhs) noexcept
135 {
136 return vector2{lhs._v - rhs._v};
137 }
138
144 [[nodiscard]] constexpr friend bool operator==(point2 const& lhs, point2 const& rhs) noexcept
145 {
146 return equal(lhs._v, rhs._v);
147 }
148
149 [[nodiscard]] friend constexpr point2 midpoint(point2 const& lhs, point2 const& rhs) noexcept
150 {
151 return point2{midpoint(lhs._v, rhs._v)};
152 }
153
154 [[nodiscard]] friend constexpr point2 reflect(point2 const& lhs, point2 const& rhs) noexcept
155 {
156 return point2{reflect_point(lhs._v, rhs._v)};
157 }
158
164 [[nodiscard]] friend constexpr point2 min(point2 const& lhs, point2 const& rhs) noexcept
165 {
166 return point2{min(lhs._v, rhs._v)};
167 }
168
174 [[nodiscard]] friend constexpr point2 max(point2 const& lhs, point2 const& rhs) noexcept
175 {
176 return point2{max(lhs._v, rhs._v)};
177 }
178
181 [[nodiscard]] friend constexpr point2 round(point2 const& rhs) noexcept
182 {
183 return point2{round(rhs._v)};
184 }
185
188 [[nodiscard]] friend constexpr point2 ceil(point2 const& rhs) noexcept
189 {
190 return point2{ceil(rhs._v)};
191 }
192
195 [[nodiscard]] friend constexpr point2 floor(point2 const& rhs) noexcept
196 {
197 return point2{floor(rhs._v)};
198 }
199
202 [[nodiscard]] friend constexpr point2 ceil(point2 const& lhs, extent2 rhs) noexcept
203 {
204 hilet rhs_ = array_type{rhs}.xy11();
205 return point2{ceil(lhs._v / rhs_) * rhs_};
206 }
207
210 [[nodiscard]] friend constexpr point2 floor(point2 const& lhs, extent2 rhs) noexcept
211 {
212 hilet rhs_ = array_type{rhs}.xy11();
213 return point2{floor(lhs._v / rhs_) * rhs_};
214 }
215
216 [[nodiscard]] friend float distance(point2 const& lhs, point2 const& rhs) noexcept
217 {
218 return hypot(rhs - lhs);
219 }
220
224 [[nodiscard]] constexpr bool holds_invariant() const noexcept
225 {
226 return _v.z() == 0.0f and _v.w() != 0.0f;
227 }
228
229 [[nodiscard]] friend std::string to_string(point2 const& rhs) noexcept
230 {
231 return std::format("<{}, {}>", rhs._v.x(), rhs._v.y());
232 }
233
234 friend std::ostream& operator<<(std::ostream& lhs, point2 const& rhs) noexcept
235 {
236 return lhs << to_string(rhs);
237 }
238
239private:
240 array_type _v;
241};
242
243} // namespace hi::inline v1
244
245template<typename CharT>
246struct std::formatter<hi::point2, CharT> {
247 auto parse(auto& pc)
248 {
249 return pc.end();
250 }
251
252 auto format(hi::point2 const& t, auto& fc)
253 {
254 return std::vformat_to(fc.out(), "<{}, {}>", std::make_format_args(t.x(), t.y()));
255 }
256};
Defined the geo::extent, extent2 and extent3 types.
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:253
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
DOXYGEN BUG.
Definition algorithm.hpp:13
geometry/margins.hpp
Definition cache.hpp:11
A high-level geometric point Part of the high-level vec, point, mat and color types.
Definition point2.hpp:23
friend constexpr point2 min(point2 const &lhs, point2 const &rhs) noexcept
Mix the two points and get the lowest value of each element.
Definition point2.hpp:164
constexpr bool holds_invariant() const noexcept
Check if the point is valid.
Definition point2.hpp:224
constexpr friend vector2 operator-(point2 const &lhs, point2 const &rhs) noexcept
Find the vector between two points.
Definition point2.hpp:134
constexpr friend bool operator==(point2 const &lhs, point2 const &rhs) noexcept
Compare if two points are equal.
Definition point2.hpp:144
friend constexpr point2 ceil(point2 const &lhs, extent2 rhs) noexcept
Round the coordinates of a point toward the top-right with the given granularity.
Definition point2.hpp:202
constexpr float & y() noexcept
Access the y element from the point.
Definition point2.hpp:68
friend constexpr point2 floor(point2 const &lhs, extent2 rhs) noexcept
Round the coordinates of a point toward the left-bottom with the given granularity.
Definition point2.hpp:210
friend constexpr point2 round(point2 const &rhs) noexcept
Round the coordinates of a point toward nearest integer.
Definition point2.hpp:181
constexpr point2(float x, float y) noexcept
Construct a 3D point from x, y and z elements.
Definition point2.hpp:55
constexpr friend point2 operator-(point2 const &lhs, vector2 const &rhs) noexcept
Move a point backward along the vector.
Definition point2.hpp:124
constexpr float & x() noexcept
Access the x element from the point.
Definition point2.hpp:60
friend constexpr point2 max(point2 const &lhs, point2 const &rhs) noexcept
Mix the two points and get the highest value of each element.
Definition point2.hpp:174
friend constexpr point2 ceil(point2 const &rhs) noexcept
Round the coordinates of a point toward the right-top.
Definition point2.hpp:188
constexpr float y() const noexcept
Access the y element from the point.
Definition point2.hpp:84
constexpr point2(array_type const &other) noexcept
Construct a point from a array_type-simd.
Definition point2.hpp:42
constexpr friend point2 operator+(point2 const &lhs, vector2 const &rhs) noexcept
Move a point along a vector.
Definition point2.hpp:104
constexpr friend point2 operator+(vector2 const &lhs, point2 const &rhs) noexcept
Move a point along a vector.
Definition point2.hpp:114
friend constexpr point2 floor(point2 const &rhs) noexcept
Round the coordinates of a point toward the left-bottom.
Definition point2.hpp:195
constexpr float x() const noexcept
Access the x element from the point.
Definition point2.hpp:76
constexpr point2() noexcept
Construct a point at the origin of the coordinate system.
Definition point2.hpp:49