Creative Coding Blog

Maths, coding and art

Lissajous II - overlaying curves in Processing

Following on from the earlier article Lissajous figures with Processing, we will now see how to create more interesting images by overlaying several related curves.

Shrinking curves

In the image below we draw 4 versions of the Lissajous curve with A = 5 and B = 2. In each case the radius of the curve is reduced:

Here is the complete code. It uses the lissajous function from the original article:

void lissajous(float r, float cx, float cy, float A, float B, float P)
{
    PShape s = createShape();
    s.beginShape();
    for (float t = 0; t < TWO_PI; t += 0.01)
    {
        float x = cx + r * cos(A*t + P);
        float y = cy + r * sin(B*t);
        s.vertex(x, y);
    }
    s.endShape(CLOSE);
    
    shape(s);
}

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

void draw()
{
    clear();
    background(255);
    noFill();
    strokeWeight(1);
    
    stroke(255, 0, 0);
    lissajous(65, 75, 75, 3, 2, 0);
    lissajous(55, 225, 75, 3, 2, 0);
    lissajous(45, 375, 75, 3, 2, 0);
    lissajous(35, 525, 75, 3, 2, 0);
}

Overlaying the curves

You can get quite a pleasing effect by having lots of curves, overlaid on the same image:

We have done a couple of other things here too:

  • Set the background to black (which always adds some drama).
  • Set the curve colour to light blue with transparency.

The blue colour is (196, 196, 255, 100). The final value, 100, means that the colour is about 40% opaque. This means that the colour looks darker than it really is (it is transparent, so some of the black background shows through). However, where multiple curves overlap, in the central X shape, the combination of several transparent lines covers more of the black, making the area look brighter.

Here is the updated draw function. The loop counts i from 0 to 24, so the size of the shapes 250 - i*10 steps down from 250 to 10 in steps of 10. And of course we have increased the size of the image in the setup function to 600 by 600.

void draw()
{
    clear();
    background(0);
    noFill();
    strokeWeight(2);
    
    for (int i = 0; i < 25; i++)
    {
      stroke(196, 196, 255, 100);
      lissajous(250-i*10, 300, 300, 5, 2, 0);
    }
}