23#define _USE_MATH_DEFINES
69template <std::
size_t N>
70void spirograph(std::array<std::pair<double, double>, N> *points,
double l,
71 double k,
double rot) {
72 double dt = rot * 2.f * M_PI / N;
74 const double k1 = 1.f - k;
80 for (step = 0; step < N; step++) {
82 double first = R * (k1 * std::cos(t) + l * k * std::cos(k1 * t / k));
83 double second = R * (k1 * std::sin(t) - l * k * std::sin(k1 * t / k));
84 points[0][step].first = first;
85 points[0][step].second = second;
95 double l = 0.3, k = 0.75, rot = 10.;
96 std::stringstream fname;
97 fname << std::setw(3) <<
"spirograph_" << l <<
"_" << k <<
"_" << rot
99 std::ofstream fp(fname.str());
101 perror(fname.str().c_str());
105 std::array<std::pair<double, double>, N> points;
109 for (
size_t i = 0; i < N; i++) {
110 fp << points[i].first <<
"," << points[i].first;
120static bool paused = 0;
121static const int animation_speed = 25;
123static const double step = 0.01;
124static double l_ratio = step * 10;
125static double k_ratio = step;
126static const double num_rot = 20.;
130static inline void glutBitmapString(
void *font,
char *message) {
131 for (
char *ch = message; *ch !=
'\0'; ch++) glutBitmapCharacter(font, *ch);
145void display_graph(
const std::array<std::pair<double, double>, N> &points,
146 double l,
double k) {
147 glClearColor(1.0f, 1.0f, 1.0f,
149 glClear(GL_COLOR_BUFFER_BIT);
152 glColor3f(0.f, 0.f, 1.f);
155 for (
size_t i = 1; i <
N; i++) {
156 glVertex2f(points[i - 1].first, points[i - 1].second);
157 glVertex2f(points[i].first, points[i].second);
161 glColor3f(0.f, 0.f, 0.f);
162 std::stringstream buffer;
163 buffer << std::setw(3) <<
"l = " <<
l;
164 glRasterPos2f(-.85, .85);
165 glutBitmapString(GLUT_BITMAP_TIMES_ROMAN_24,
166 const_cast<char *
>(buffer.str().c_str()));
169 buffer << std::setw(3) <<
"k = " <<
k;
170 glRasterPos2f(-.85, .70);
171 glutBitmapString(GLUT_BITMAP_TIMES_ROMAN_24,
172 const_cast<char *
>(buffer.str().c_str()));
182 const size_t N = 5000;
184 static bool direction1 =
true;
185 static bool direction2 =
true;
187 std::array<std::pair<double, double>,
N> points;
189 spirograph(&points, l_ratio, k_ratio, num_rot);
190 display_graph(points, l_ratio, k_ratio);
197 if (k_ratio >= (1.f - step))
202 if (k_ratio <= step) {
206 if (l_ratio >= (1.f - step))
225void timer_cb(
int t) {
226 glutTimerFunc(animation_speed, timer_cb, 0);
237void keyboard_cb(
unsigned char key,
int x,
int y) {
268int main(
int argc,
char **argv) {
272 glutInit(&argc, argv);
273 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
274 glutCreateWindow(
"Spirograph");
275 glutInitWindowSize(400, 400);
277 glutTimerFunc(spirograph::animation_speed, spirograph::timer_cb, 0);
278 glutKeyboardFunc(spirograph::keyboard_cb);
279 glutDisplayFunc(spirograph::test2);
double k(double x)
Another test function.
double l(double x)
Another test function.
static void test2()
Self-implementations, 2nd test.
void test()
Test function to save resulting points to a CSV file.
constexpr uint32_t N
A struct to represent sparse table for min() as their invariant function, for the given array A....