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