header_utils
Loading...
Searching...
No Matches
ellipse.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 tellipse
19 {
20 glm::tvec2<T> center;
21 glm::tvec2<T> radii;
22
23 auto const& centroid() const { return center; }
24
25 bool contains(glm::tvec2<T> pt) const
26 {
27 const auto d = pt - center;
28 return (d.x * d.x) / (radii.x * radii.x) + (d.y * d.y) / (radii.y * radii.y) <= T(1);
29 }
30
31 T edge_length() const
32 {
35 const auto a = std::max(radii.x, radii.y);
36 const auto b = std::min(radii.x, radii.y);
37 const auto h = ((a - b) * (a - b)) / ((a + b) * (a + b));
38 return glm::pi<T>() * (a + b) * (1 + ((3 * h) / (10 + sqrtf(4 - 3 * h))));
39 }
40
41 glm::tvec2<T> edge_point_alpha(T t) const
42 {
43 throw "unimplemented";
44 }
45
46 T calculate_area() const { return glm::pi<T>() * radii.x * radii.y; }
47 glm::tvec2<T> edge_point(T t) const { return edge_point_alpha(t / edge_length()); }
48 trec2<T> bounding_box() const { return trec2<T>{center - radii, center + radii}; }
49
50 glm::tvec2<T> closest_point_to(glm::tvec2<T> pt) const
51 {
53 throw "unimplemented";
54 }
55 };
56
57 using ellipse = tellipse<float>;
58
60
61 static_assert(area_shape<float, ellipse>);
62}
A shape for which the concept of an "area" has a finite interpretation.
constexpr __contains_fn contains
contains(range, el)
Definition ranges.h:264
Represents a 2D, axis-aligned ellipse area_shape.
Definition ellipse.h:19
Represents a 2D rectangle polygon_shape.
Definition rec2.h:31