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 "point.hpp"
12#include "quad.hpp"
13
14namespace hi { inline namespace v1 {
15
18class circle {
19public:
20 constexpr circle(circle const& other) noexcept = default;
21 constexpr circle(circle&& other) noexcept = default;
22 constexpr circle& operator=(circle const& other) noexcept = default;
23 constexpr circle& operator=(circle&& other) noexcept = default;
24
25 constexpr circle() noexcept : _v()
26 {
27 hi_axiom(holds_invariant());
28 }
29
30 [[nodiscard]] constexpr explicit circle(f32x4 v) noexcept : _v(v)
31 {
32 hi_axiom(holds_invariant());
33 }
34
35 [[nodiscard]] constexpr explicit operator f32x4() const noexcept
36 {
37 return _v;
38 }
39
40 [[nodiscard]] constexpr circle(point3 point, float radius) noexcept : _v(f32x4{point})
41 {
42 _v.w() = radius;
43 hi_axiom(holds_invariant());
44 }
45
46 [[nodiscard]] constexpr circle(float radius) noexcept : _v()
47 {
48 _v.w() = radius;
49 hi_axiom(holds_invariant());
50 }
51
52 [[nodiscard]] constexpr circle(aarectangle square) noexcept
53 {
54 hilet square_ = f32x4{square};
55
56 // center=(p3 + p0)/2, radius=(p3 - p0)/2
57 _v = (addsub<0b0011>(square_.zwzw(), square_.xyxy()) * 0.5f).xy0w();
58 hi_axiom(holds_invariant());
59 }
60
61 [[nodiscard]] constexpr bool empty() const noexcept
62 {
63 return _v.w() == 0.0f;
64 }
65
66 [[nodiscard]] explicit operator bool() const noexcept
67 {
68 return not empty();
69 }
70
71 [[nodiscard]] constexpr float radius() const noexcept
72 {
73 return _v.w();
74 }
75
76 [[nodiscard]] constexpr float diameter() const noexcept
77 {
78 return radius() * 2.0f;
79 }
80
81 [[nodiscard]] constexpr point3 center() const noexcept
82 {
83 return point3{_v.xyz1()};
84 }
85
86 [[nodiscard]] constexpr friend circle operator+(circle const& lhs, float rhs) noexcept
87 {
88 return circle{lhs._v + insert<3>(f32x4{}, rhs)};
89 }
90
91 [[nodiscard]] constexpr friend circle operator-(circle const& lhs, float rhs) noexcept
92 {
93 return circle{lhs._v - insert<3>(f32x4{}, rhs)};
94 }
95
96 [[nodiscard]] constexpr friend circle operator*(circle const& lhs, float rhs) noexcept
97 {
98 return circle{lhs._v * insert<3>(f32x4::broadcast(1.0f), rhs)};
99 }
100
101 [[nodiscard]] constexpr friend point3 midpoint(circle const& rhs) noexcept
102 {
103 return point3{rhs.center()};
104 }
105
106 [[nodiscard]] constexpr friend aarectangle bounding_rectangle(circle const& rhs) noexcept
107 {
108 hilet p = rhs._v.xyxy();
109 hilet r = neg<0b0011>(rhs._v.wwww());
110 return aarectangle{p + r};
111 }
112
119 [[nodiscard]] friend constexpr circle align(aarectangle haystack, circle needle, alignment alignment) noexcept
120 {
121 hilet x = [&] {
122 if (alignment == horizontal_alignment::left) {
123 return haystack.left() + needle.radius();
124
125 } else if (alignment == horizontal_alignment::right) {
126 return haystack.right() - needle.radius();
127
128 } else if (alignment == horizontal_alignment::center) {
129 return haystack.center();
130
131 } else {
133 }
134 }();
135
136 hilet y = [&] {
137 if (alignment == vertical_alignment::bottom) {
138 return haystack.bottom() + needle.radius();
139
140 } else if (alignment == vertical_alignment::top) {
141 return haystack.top() - needle.radius();
142
143 } else if (alignment == vertical_alignment::middle) {
144 return haystack.middle();
145
146 } else {
148 }
149 }();
150
151 return circle{point2{x, y}, needle.radius()};
152 }
153
154private:
155 // Stored as a center point (x, y, z), and radius (w).
156 f32x4 _v;
157
158 [[nodiscard]] constexpr bool holds_invariant() const noexcept
159 {
160 return _v.w() >= 0.0f;
161 }
162};
163
164}} // namespace hi::v1
#define hi_no_default(...)
This part of the code should not be reachable, unless a programming bug.
Definition assert.hpp:279
#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
@ other
The gui_event does not have associated data.
DOXYGEN BUG.
Definition algorithm.hpp:13
geometry/margins.hpp
Definition cache.hpp:11
Class which represents an axis-aligned rectangle.
Definition axis_aligned_rectangle.hpp:27
A type defining a 2D circle.
Definition circle.hpp:18
friend constexpr circle align(aarectangle haystack, circle needle, alignment alignment) noexcept
Align a rectangle within another rectangle.
Definition circle.hpp:119