sKit  0.0.9
shapes.cpp
Go to the documentation of this file.
1 #include "shapes.hpp"
2 
3 namespace sKit {
4 namespace geom {
5 
6 template <>
7 auto circle<SDL_Vertex>(float x, float y, float radius, unsigned int resolution)
9  std::vector<SDL_Vertex> vertices;
10  std::vector<int> indices;
11  vertices.push_back(SDL_Vertex {.position = {x, y}, .color = {1, 1, 1, 1}});
12  auto resf = static_cast<float>(resolution);
13  for (int i = 0; i < resolution; i++) {
14  float xp = cos(i / resf * std::numbers::pi_v<float> * 2.f) * radius +
15  vertices[0].position.x;
16  float yp = sin(i / resf * std::numbers::pi_v<float> * 2.f) * radius +
17  vertices[0].position.y;
18  vertices.emplace_back(
19  SDL_Vertex {.position = {xp, yp}, .color = {1, 1, 1, 1}}
20  );
21  indices.push_back(0);
22  indices.push_back(i + 1);
23  indices.push_back(((i + 2) % resolution) + 1);
24  }
25  return Geometry {vertices, indices};
26 }
27 auto arc(
28  float x,
29  float y,
30  float radius,
31  float theta,
32  float width,
33  float height,
34  unsigned int resolution,
35  SDL_FColor col
37  static constexpr auto tau = std::numbers::pi_v<float> * 2.f;
38  const float resf = static_cast<float>(resolution);
39  const unsigned int arcRes = round((width / tau) * resf);
40  std::vector<SDL_Vertex> vertices;
41  std::vector<int> indices;
42  /* if (height > 1 && width > (tau / resf) && */
43  /* arcRes != 0) { // dont return arcs smaller than a pixel */
44  vertices.resize(arcRes * 2 + 2);
45  for (unsigned int i = 0; i <= arcRes; i++) {
46  const float a = sKit::math::map(i, 0, arcRes, theta, theta + width);
47  {
48  const float px = cos(a) * (radius - height) + x;
49  const float py = sin(a) * (radius - height) + y;
50  vertices[i * 2] = SDL_Vertex {.position = {px, py}, .color = col};
51  }
52  {
53  const float px = cos(a) * (radius + 2) + x;
54  const float py = sin(a) * (radius + 2) + y;
55  vertices[i * 2 + 1] = SDL_Vertex {.position = {px, py}, .color = col};
56  }
57  }
58  for (unsigned int i = 0; i < arcRes * 2; ++i) {
59  indices.push_back(i);
60  indices.push_back(i + 1);
61  indices.push_back(i + 2);
62  }
63  /* } */
64 
65  return Geometry {.vertices = vertices, .indices = indices};
66 };
67 
68 } // namespace geom
69 } // namespace sKit
shapes.hpp
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::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::circle< SDL_Vertex >
auto circle< SDL_Vertex >(float x, float y, float radius, unsigned int resolution) -> Geometry< SDL_Vertex, int >
Definition: shapes.cpp:7
sKit::geom::Geometry::vertices
std::vector< T > vertices
Definition: shapes.hpp:19