|
Algorithms_in_C
1.0.0
Set of algorithms implemented in C.
|
Implementation of Spirograph More...
#include <math.h>#include <stdbool.h>#include <stdio.h>#include <stdlib.h>#include <time.h>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]). More... | |
| void | test (void) |
| Test function to save resulting points to a CSV file. | |
| int | main (int argc, char **argv) |
| Main function. | |
Implementation of Spirograph
Implementation of the program is based on the geometry shown in the figure below:
| 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
Since we are considering ratios, the actual values of \(r\) and \(R\) are immaterial.
| [out] | x | output array containing absicca of points (must be pre-allocated) |
| [out] | y | output array containing ordinates of points (must be pre-allocated) |
| l | the relative distance of marker from the centre of inner circle and \(0\le l\le1\) | |
| k | the ratio of radius of inner circle to outer circle and \(0<k<1\) | |
| N | number of sample points along the trajectory (higher = better resolution but consumes more time and memory) | |
| num_rot | the number of rotations to perform (can be fractional value) |