Skip to content

Instantly share code, notes, and snippets.

@HotelCalifornia
Created March 15, 2020 03:06
Show Gist options
  • Save HotelCalifornia/93d1275a1b07fe06f9fb2ded68b530d3 to your computer and use it in GitHub Desktop.
Save HotelCalifornia/93d1275a1b07fe06f9fb2ded68b530d3 to your computer and use it in GitHub Desktop.
in lieu of doing anything remotely useful, i decided to do this
#include <array>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <utility>
namespace circles {
namespace {
template <std::size_t radius, std::size_t cx, std::size_t cy, std::size_t N, std::size_t... I>
constexpr std::array<std::pair<double, double>, N> make_circle(std::index_sequence<I...>) {
return std::array<std::pair<double, double>, N>{{
{cx + radius * std::cos(((double)I / N) * 2 * M_PI), cy + radius * std::sin(((double)I / N) * 2 * M_PI)}...
}};
}
}; // <anonymous>
template <
std::size_t radius,
std::int32_t ox = 0,
std::int32_t oy = 0,
std::size_t resolution = 1000,
typename Indices=std::make_index_sequence<resolution>
>
struct Circle {
using Point = std::pair<double, double>;
static constexpr std::array<Point, resolution> points = make_circle<radius, ox, oy, resolution>(Indices{});
};
}; // <circles>
int main() {
for (auto p : circles::Circle<1, 0, 0, 20>::points) {
std::cout << "(" << p.first << ", " << p.second << ")" << std::endl;
}
}
@HotelCalifornia
Copy link
Author

$ g++ -o circles -std=c++17 -lm circles.cpp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment