header_utils
Loading...
Searching...
No Matches
circle.h
1
4
5#pragma once
6
7#include "geometry_common.h"
8#include "shape_concepts.h"
9#include <glm/ext/scalar_constants.hpp>
10
11namespace ghassanpl::geometry
12{
15
17 template <std::floating_point T>
18 struct tcircle
19 {
20 glm::tvec2<T> center;
21 T radius{};
22
23 auto const& centroid() const { return center; }
24
25 constexpr bool is_valid() const noexcept
26 {
27 return radius >= T{};
28 }
29
30 constexpr tcircle valid() const noexcept
31 {
32 return { center, std::abs(radius) };
33 }
34
35 constexpr bool contains(glm::tvec2<T> pt) const noexcept
36 {
37 const auto d = pt - center;
38 return (d.x * d.x) / (radius * radius) + (d.y * d.y) / (radius * radius) <= T(1);
39 }
40
41 constexpr T edge_length() const noexcept
42 {
43 return glm::pi<T>() * 2 * radius;
44 }
45
46 constexpr glm::tvec2<T> edge_point_alpha(T t) const
47 {
48 const auto angle = t * glm::pi<T>() * 2;
49 return center + glm::tvec2<T>{cos(angle), sin(angle)} * radius;
50 }
51
52 constexpr T calculate_area() const noexcept { return glm::pi<T>() * radius * radius; }
53 constexpr glm::tvec2<T> edge_point(T t) const noexcept { return edge_point_alpha(t / edge_length()); }
54 constexpr trec2<T> bounding_box() const noexcept { return trec2<T>::from_center_and_half_size(center, {radius, radius}); }
55
56 glm::tvec2<T> closest_point_to(glm::tvec2<T> pt) const
57 {
58 return center + glm::normalize(pt - center) * radius;
59 }
60 };
61
64
66
67 static_assert(area_shape<float, circle>);
68}
A shape for which the concept of an "area" has a finite interpretation.
constexpr __contains_fn contains
contains(range, el)
Definition ranges.h:264
tcircle< float > circle
A circle with float cordinates.
Definition circle.h:63
Represents a 2D circle area_shape.
Definition circle.h:19
Represents a 2D rectangle polygon_shape.
Definition rec2.h:31