HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
circle.hpp
Go to the documentation of this file.
1// Copyright Take Vos 2021.
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
8#pragma once
9
10#include "point2.hpp"
11#include "aarectangle.hpp"
12#include "quad.hpp"
13#include "../macros.hpp"
14#include <exception>
15#include <compare>
16
17hi_export_module(hikogui.geometry : circle);
18
19hi_export namespace hi { inline namespace v1 {
20
23class circle {
24public:
25 using array_type = f32x4;
26
27 constexpr circle(circle const& other) noexcept = default;
28 constexpr circle(circle&& other) noexcept = default;
29 constexpr circle& operator=(circle const& other) noexcept = default;
30 constexpr circle& operator=(circle&& other) noexcept = default;
31
32 constexpr circle() noexcept : _v()
33 {
34 hi_axiom(holds_invariant());
35 }
36
37 [[nodiscard]] constexpr explicit circle(f32x4 v) noexcept : _v(v)
38 {
39 hi_axiom(holds_invariant());
40 }
41
42 [[nodiscard]] constexpr explicit operator f32x4() const noexcept
43 {
44 return _v;
45 }
46
47 [[nodiscard]] constexpr circle(point3 point, float radius) noexcept : _v(f32x4{point})
48 {
49 _v.w() = radius;
50 hi_axiom(holds_invariant());
51 }
52
53 [[nodiscard]] constexpr circle(float radius) noexcept : _v()
54 {
55 _v.w() = radius;
56 hi_axiom(holds_invariant());
57 }
58
59 [[nodiscard]] constexpr circle(aarectangle square) noexcept
60 {
61 auto const square_ = f32x4{square};
62
63 // center=(p3 + p0)/2, radius=(p3 - p0)/2
64 _v = (addsub_mask<0b0011>(square_.zwzw(), square_.xyxy()) * array_type::broadcast(0.5f)).xy0w();
65 hi_axiom(holds_invariant());
66 }
67
68 [[nodiscard]] constexpr bool empty() const noexcept
69 {
70 return _v.w() == 0.0f;
71 }
72
73 [[nodiscard]] explicit operator bool() const noexcept
74 {
75 return not empty();
76 }
77
78 [[nodiscard]] constexpr float radius() const noexcept
79 {
80 return _v.w();
81 }
82
83 [[nodiscard]] constexpr float diameter() const noexcept
84 {
85 return radius() * 2.0f;
86 }
87
88 [[nodiscard]] constexpr point3 center() const noexcept
89 {
90 return point3{_v.xyz1()};
91 }
92
93 [[nodiscard]] constexpr friend circle operator+(circle const& lhs, float rhs) noexcept
94 {
95 return circle{lhs._v + f32x4{0.0f, 0.0f, 0.0f, rhs}};
96 }
97
98 [[nodiscard]] constexpr friend circle operator-(circle const& lhs, float rhs) noexcept
99 {
100 return circle{lhs._v - f32x4{0.0f, 0.0f, 0.0f, rhs}};
101 }
102
103 [[nodiscard]] constexpr friend circle operator*(circle const& lhs, float rhs) noexcept
104 {
105 return circle{lhs._v * f32x4{1.0f, 1.0f, 1.0f, rhs}};
106 }
107
108 [[nodiscard]] constexpr friend point3 midpoint(circle const& rhs) noexcept
109 {
110 return point3{rhs.center()};
111 }
112
113 [[nodiscard]] constexpr friend aarectangle bounding_rectangle(circle const& rhs) noexcept
114 {
115 auto const p = rhs._v.xyxy();
116 auto const r = neg_mask<0b0011>(rhs._v.wwww());
117 return aarectangle{p + r};
118 }
119
126 [[nodiscard]] friend constexpr circle align(aarectangle haystack, circle needle, alignment alignment) noexcept
127 {
128 auto const x = [&] {
130 return haystack.left() + needle.radius();
131
133 return haystack.right() - needle.radius();
134
136 return haystack.center();
137
138 } else {
139 hi_no_default();
140 }
141 }();
142
143 auto const y = [&] {
145 return haystack.bottom() + needle.radius();
146
147 } else if (alignment == vertical_alignment::top) {
148 return haystack.top() - needle.radius();
149
151 return haystack.middle();
152
153 } else {
154 hi_no_default();
155 }
156 }();
157
158 return circle{point2{x, y}, needle.radius()};
159 }
160
161private:
162 // Stored as a center point (x, y, z), and radius (w).
163 f32x4 _v;
164
165 [[nodiscard]] constexpr bool holds_invariant() const noexcept
166 {
167 return _v.w() >= 0.0f;
168 }
169};
170
171}} // namespace hi::v1
@ middle
Align to the vertical-middle.
@ bottom
Align to the bottom.
@ top
Align to the top.
@ right
Align the text to the right side.
@ left
Align the text to the left side.
@ center
Align the text in the center.
@ other
The gui_event does not have associated data.
The HikoGUI namespace.
Definition array_generic.hpp:20
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
Class which represents an axis-aligned rectangle.
Definition aarectangle.hpp:33
Horizontal/Vertical alignment combination.
Definition alignment.hpp:244
A type defining a 2D circle.
Definition circle.hpp:23
friend constexpr circle align(aarectangle haystack, circle needle, alignment alignment) noexcept
Align a rectangle within another rectangle.
Definition circle.hpp:126