Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
Loading...
Searching...
No Matches
spirograph.c File Reference

Implementation of Spirograph More...

#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
Include dependency graph for spirograph.c:

Macros

#define _USE_MATH_DEFINES
 required for MSVC compiler
 

Functions

void spirograph (double *x, double *y, double l, double k, size_t N, double rot)
 Generate spirograph curve into arrays x and y such that the i^th point in 2D is represented by (x[i],y[i]).
 
void test (void)
 Test function to save resulting points to a CSV file.
 
int main (int argc, char **argv)
 Main function.
 

Detailed Description

Implementation of Spirograph

Author
Krishna Vedala

Implementation of the program is based on the geometry shown in the figure below:

Spirograph geometry from Wikipedia

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

Main function.

272{
273 test();
274
275#ifdef USE_GLUT
276 glutInit(&argc, argv);
277 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
278 glutCreateWindow("Spirograph");
279 glutInitWindowSize(400, 400);
280 // glutIdleFunc(glutPostRedisplay);
281 glutTimerFunc(animation_speed, timer_cb, 0);
282 glutKeyboardFunc(keyboard_cb);
283 glutDisplayFunc(test2);
284 glutMainLoop();
285#endif
286
287 return 0;
288}
void test2()
Definition k_means_clustering.c:356
void test(void)
Test function to save resulting points to a CSV file.
Definition spirograph.c:74
Here is the call graph for this function:

◆ spirograph()

void spirograph ( double *  x,
double *  y,
double  l,
double  k,
size_t  N,
double  rot 
)

Generate spirograph curve into arrays x and y such that the i^th point in 2D is represented by (x[i],y[i]).

The generating function is given by:

\begin{eqnarray*} x &=& R\left[ (1-k) \cos (t) + l\cdot k\cdot\cos \left(\frac{1-k}{k}t\right) \right]\\ y &=& R\left[ (1-k) \sin (t) - l\cdot k\cdot\sin \left(\frac{1-k}{k}t\right) \right] \end{eqnarray*}

where

  • \(R\) is the scaling parameter that we will consider \(=1\)
  • \(l=\frac{\rho}{r}\) is the relative distance of marker from the centre of inner circle and \(0\le l\le1\)
  • \(\rho\) is physical distance of marker from centre of inner circle
  • \(r\) is the radius of inner circle
  • \(k=\frac{r}{R}\) is the ratio of radius of inner circle to outer circle and \(0<k<1\)
  • \(R\) is the radius of outer circle
  • \(t\) is the angle of rotation of the point i.e., represents the time parameter

Since we are considering ratios, the actual values of \(r\) and \(R\) are immaterial.

Parameters
[out]xoutput array containing absicca of points (must be pre-allocated)
[out]youtput array containing ordinates of points (must be pre-allocated)
lthe relative distance of marker from the centre of inner circle and \(0\le l\le1\)
kthe ratio of radius of inner circle to outer circle and \(0<k<1\)
Nnumber of sample points along the trajectory (higher = better resolution but consumes more time and memory)
num_rotthe number of rotations to perform (can be fractional value)
58{
59 double dt = rot * 2.f * M_PI / N;
60 double t = 0.f, R = 1.f;
61 const double k1 = 1.f - k;
62
63 for (size_t dk = 0; dk < N; dk++, t += dt)
64 {
65 x[dk] = R * (k1 * cos(t) + l * k * cos(k1 * t / k));
66 y[dk] = R * (k1 * sin(t) - l * k * sin(k1 * t / k));
67 }
68}

◆ test()

void test ( void  )

Test function to save resulting points to a CSV file.

75{
76 size_t N = 500;
77 double l = 0.3, k = 0.75, rot = 10.;
78 char fname[50];
79 snprintf(fname, 50, "spirograph_%.2f_%.2f_%.2f.csv", l, k, rot);
80 FILE *fp = fopen(fname, "wt");
81 if (!fp)
82 {
83 perror(fname);
84 exit(EXIT_FAILURE);
85 }
86
87 double *x = (double *)malloc(N * sizeof(double));
88 double *y = (double *)malloc(N * sizeof(double));
89
90 spirograph(x, y, l, k, N, rot);
91
92 for (size_t i = 0; i < N; i++)
93 {
94 fprintf(fp, "%.5g, %.5g", x[i], y[i]);
95 if (i < N - 1)
96 {
97 fputc('\n', fp);
98 }
99 }
100
101 fclose(fp);
102
103 free(x);
104 free(y);
105}
#define malloc(bytes)
This macro replace the standard malloc function with malloc_dbg.
Definition malloc_dbg.h:18
#define free(ptr)
This macro replace the standard free function with free_dbg.
Definition malloc_dbg.h:26
void spirograph(double *x, double *y, double l, double k, size_t N, double rot)
Generate spirograph curve into arrays x and y such that the i^th point in 2D is represented by (x[i],...
Definition spirograph.c:57
Here is the call graph for this function: