Creative Coding Blog

Maths, coding and art

Motifs on Spirographs

In a previous post we saw how to draw simple Spirographs. In this article, instead of just tracing the curve, we will draw a shape at regular points along the curve. We call this repeated shape a motif. What we will do here is similar to the Motifs on Lissajous figures article.

Line motif

Here is an image created using a line motif:

Here is the code

void motif()
{
    noFill();
    strokeWeight(2);
    stroke(128, 196, 255, 16);
    line(-100, 0, 100, 0);
}

int GCD(int a, int b)
{
   if (b==0)
     return a;
   return GCD(b,a%b);
}

void spiro(float cx, float cy, int A, int B, int D)
{
    for (float t = 0; t < TWO_PI*B/GCD(A, B); t += 0.01)
    {
        float x = cx + (A - B) * cos(t) + D * cos(t*(A - B)/B);
        float y = cy + (A - B) * sin(t) - D * sin(t*(A - B)/B);
        pushMatrix();
        translate(x, y);
        rotate(10*t);
        motif();
        popMatrix();    }
}

void setup()
{
    size(600, 600);
}

void draw()
{
    clear();
    background(0);
    noFill();
    strokeWeight(2);
    
    spiro(300, 300, 240, 64, 30);
}

As with the Lissajous figure example, at each point along the curve we draw a line. The line is transparent, and it rotates as we move along the curve.