HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
circle.hpp
1
2#pragma once
3
4#include "point.hpp"
5#include "axis_aligned_rectangle.hpp"
6#include "quad.hpp"
7
8namespace hi::inline v1 {
9
10class circle {
11public:
12 constexpr circle(circle const &other) noexcept = default;
13 constexpr circle(circle &&other) noexcept = default;
14 constexpr circle &operator=(circle const &other) noexcept = default;
15 constexpr circle &operator=(circle &&other) noexcept = default;
16
17 constexpr circle() noexcept : _v()
18 {
19 hi_axiom(holds_invariant());
20 }
21
22 [[nodiscard]] constexpr explicit circle(f32x4 v) noexcept : _v(v)
23 {
24 hi_axiom(holds_invariant());
25 }
26
27 [[nodiscard]] constexpr explicit operator f32x4() const noexcept
28 {
29 return _v;
30 }
31
32 [[nodiscard]] constexpr circle(point3 point, float radius) noexcept :
33 _v(f32x4{point})
34 {
35 _v.w() = radius;
36 hi_axiom(holds_invariant());
37 }
38
39 [[nodiscard]] constexpr circle(aarectangle square) noexcept
40 {
41 auto square_ = f32x4{square};
42
43 // center=(p3 + p0)/2, radius=(p3 - p0)/2
44 _v = (addsub<0b0011>(square_.zwzw(), square_.xyxy()) * 0.5f).xy0w();
45 hi_axiom(holds_invariant());
46 }
47
48 [[nodiscard]] constexpr bool empty() const noexcept
49 {
50 return _v.w() == 0.0f;
51 }
52
53 [[nodiscard]] explicit operator bool () const noexcept
54 {
55 return not empty();
56 }
57
58 [[nodiscard]] constexpr float radius() const noexcept
59 {
60 return _v.w();
61 }
62
63 [[nodiscard]] constexpr point3 center() const noexcept
64 {
65 return point3{_v.xyz1()};
66 }
67
68 [[nodiscard]] constexpr friend circle operator+(circle const &lhs, float rhs) noexcept
69 {
70 return circle{lhs._v + insert<3>(f32x4{}, rhs)};
71 }
72
73 [[nodiscard]] constexpr friend circle operator-(circle const &lhs, float rhs) noexcept
74 {
75 return circle{lhs._v - insert<3>(f32x4{}, rhs)};
76 }
77
78 [[nodiscard]] constexpr friend circle operator*(circle const &lhs, float rhs) noexcept
79 {
80 return circle{lhs._v * insert<3>(f32x4::broadcast(1.0f), rhs)};
81 }
82
83 [[nodiscard]] constexpr friend point3 midpoint(circle const &rhs) noexcept
84 {
85 return point3{rhs.center()};
86 }
87
88 [[nodiscard]] constexpr friend aarectangle bounding_rectangle(circle const &rhs) noexcept
89 {
90 auto p = rhs._v.xyxy();
91 auto r = neg<0b0011>(rhs._v.wwww());
92 return aarectangle{p + r};
93 }
94
95private:
96 // Stored as a center point (x, y, z), and radius (w).
97 f32x4 _v;
98
99 [[nodiscard]] constexpr bool holds_invariant() const noexcept
100 {
101 return _v.w() >= 0.0f;
102 }
103};
104
105
106}
107
@ center
Align the text in the center.
Class which represents an axis-aligned rectangle.
Definition axis_aligned_rectangle.hpp:20
Definition circle.hpp:10