Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
spirograph Namespace Reference

Functions

template<std::size_t N>
void spirograph (std::array< std::pair< double, double >, N > *points, double l, double k, double rot)
 
void test ()
 Test function to save resulting points to a CSV file.
 

Detailed Description

Functions related to spirograph.cpp

Function Documentation

◆ spirograph()

template<std::size_t N>
void spirograph::spirograph ( std::array< std::pair< double, double >, N > * points,
double l,
double k,
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.

Template Parameters
Nnumber of points = size of array
Parameters
[out]pointsArray of 2D points represented as std::pair
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\)
rotthe number of rotations to perform (can be fractional value)
71 {
72 double dt = rot * 2.f * M_PI / N;
73 double R = 1.f;
74 const double k1 = 1.f - k;
75 int32_t step = 0;
76
77#ifdef _OPENMP
78#pragma omp for
79#endif
80 for (step = 0; step < N; step++) {
81 double t = dt * 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;
86 }
87}
T cos(T... args)
T sin(T... args)
Here is the call graph for this function:

◆ test()

void spirograph::test ( )

Test function to save resulting points to a CSV file.

93 {
94 const size_t N = 500;
95 double l = 0.3, k = 0.75, rot = 10.;
97 fname << std::setw(3) << "spirograph_" << l << "_" << k << "_" << rot
98 << ".csv";
99 std::ofstream fp(fname.str());
100 if (!fp.is_open()) {
101 perror(fname.str().c_str());
102 exit(EXIT_FAILURE);
103 }
104
106
107 spirograph(&points, l, k, rot);
108
109 for (size_t i = 0; i < N; i++) {
110 fp << points[i].first << "," << points[i].first;
111 if (i < N - 1) {
112 fp << '\n';
113 }
114 }
115
116 fp.close();
117}
T setw(T... args)
T str(T... args)
Here is the call graph for this function: