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