sKit  0.0.9
shapes.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <SDL3/SDL.h>
4 
5 #include <cmath>
6 #include <numbers>
7 #include <stdexcept>
8 #include <vector>
9 
10 #include "../math.hpp"
11 
12 namespace sKit {
13 namespace geom {
14 
17 template <typename T, typename U>
18 struct Geometry {
19  std::vector<T> vertices;
20  std::vector<U> indices;
21 };
22 
27 template <typename T, typename U>
28 auto circle(float x, float y, float radius, unsigned int resolution = 72)
29  -> Geometry<T, U> {
30  throw std::runtime_error("");
31 };
32 
34 template <>
36  float x,
37  float y,
38  float radius,
39  unsigned int resolution
40 ) -> Geometry<SDL_Vertex, int>;
41 
42 auto arc(
43  float x,
44  float y,
45  float radius,
46  float theta,
47  float width,
48  float height,
49  unsigned int resolution = 100,
50  SDL_FColor col = {}
51 ) -> Geometry<SDL_Vertex, int>;
52 
54 namespace vertex {
55 
68 template <typename T>
69 auto arc(
70  float x,
71  float y,
72  float radius,
73  float theta,
74  float width,
75  float height,
76  unsigned int resolution = 100
77 ) -> std::vector<T> {
78  static constexpr auto tau = (std::numbers::pi_v<float> * 2.f);
79  const float resf = static_cast<float>(resolution);
80  const unsigned int arcRes = round((width / tau) * resf);
81  std::vector<T> vertices(arcRes * 2 + 3);
82  for (unsigned int i = 0; i <= arcRes; i++) {
83  {
84  const float a = sKit::math::map(i, 0, arcRes, theta, theta + width);
85  const float px = cos(a) * (radius - height) + x;
86  const float py = sin(a) * (radius - height) + y;
87  vertices[i] = T {px, py};
88  }
89  {
90  const float a = sKit::math::map(i, 0, arcRes, theta + width, theta);
91  const float px = cos(a) * radius + x;
92  const float py = sin(a) * radius + y;
93  vertices[arcRes + i + 1] = T {px, py};
94  }
95  }
96  vertices[vertices.size() - 1] = vertices[0];
97  return vertices;
98 };
99 
100 } // namespace vertex
101 
102 } // namespace geom
103 } // namespace sKit
sKit::geom::Geometry::indices
std::vector< U > indices
Definition: shapes.hpp:20
sKit::geom::circle< SDL_Vertex, int >
auto circle< SDL_Vertex, int >(float x, float y, float radius, unsigned int resolution) -> Geometry< SDL_Vertex, int >
sKit::geom::circle
auto circle(float x, float y, float radius, unsigned int resolution=72) -> Geometry< T, U >
constructs data usable for rendering a filled circle. The vertices vector contains the center vertex ...
Definition: shapes.hpp:28
sKit
Definition: camera.hpp:8
sKit::geom::arc
auto arc(float x, float y, float radius, float theta, float width, float height, unsigned int resolution, SDL_FColor col) -> Geometry< SDL_Vertex, int >
Definition: shapes.cpp:27
sKit::geom::vertex::arc
auto arc(float x, float y, float radius, float theta, float width, float height, unsigned int resolution=100) -> std::vector< T >
Definition: shapes.hpp:69
sKit::math::map
auto map(float value, float inputMin, float inputMax, float outputMin, float outputMax, bool clamp) -> float
Given a value and an input range, map the value to an output range.
Definition: math.cpp:10
sKit::geom::Geometry
Definition: shapes.hpp:18
sKit::geom::Geometry::vertices
std::vector< T > vertices
Definition: shapes.hpp:19