Plotting Lissajous Figures

Thank you for reading this.

I’ve spent most of the day on this and although the code basically works the result is pretty rough. Can anyone suggest how I might smooth out the resulting curve?

float a = 0.0;
float inc = TWO_PI/25.0; // I don't understand why the inc value is 2*Pi / 25.  
float prev_x = 0, prev_y = 50, x, y;

void setup()
{
  size(400, 200);
  stroke(255, 0, 0);
  noLoop();
}

void draw()
{
  for (int i=0; i<160; i=i+5)
  {
    //x = i;
    y = 50 + cos(a *3) * 40.0; // 3 times the frequency of the x axis
    x = 50 + sin(a) * 40.0;

    line(prev_x, prev_y, x, y);
    prev_x = x;
    prev_y = y;
    a = a + inc;
  }
}
1 Like

can change to

i++;

but still get lines, just more of them
( sorry in your case with a smaller inc )


but processing can do curves

!!! need a start and end control point !!!


but as a first start a other design

int nx = 3,ny = 1;
float zoom=100;

void setup() {
  size(400, 400);
  stroke(0,120,0);
  strokeWeight(3);
}

float a = TAU/360;

void draw() {
  background(0,0,80);
  translate(width/2,height/2);
  nx = (int)map(mouseX,0,width,1,10);
  ny = (int)map(mouseY,0,height,1,10);
  for (int i=0; i < 360; i++)
    point(cos(a*i*nx) * zoom,sin(a*i*ny) * zoom);
}

now with curve

int nx = 3,ny = 1;
float zoom=100;
float a = TAU/360;

void setup() {
  size(400, 400);
  stroke(0,120,0);
  strokeWeight(3);
  noFill();
}

void draw() {
  background(0,0,80);
  translate(width/2,height/2);
  nx = (int)map(mouseX,0,width,1,10);
  ny = (int)map(mouseY,0,height,1,10);
  beginShape();
  curveVertex(cos(0) * zoom,sin(0) * zoom);
  for (int i=0; i < 360; i++)
    curveVertex(cos(a*i*nx) * zoom,sin(a*i*ny) * zoom);
  curveVertex(cos(a*360*nx) * zoom,sin(a*360*ny) * zoom);
  endShape(CLOSE);
}

2 Likes

not sure if these resources can help

2 Likes

Hello,

https://processing.org/examples/sinewave.html
https://processing.org/tutorials/trig/
https://processing.org/reference/TAU.html I prefer to use TAU; TAU = TWO_PI

I always start with points and work from there when plotting.

I did not join lines in this example:

float a = 0.0;
float x, y;

void setup()
  {
  size(400, 300);
  stroke(255, 255, 0);
  strokeWeight(4);
  }

void draw()
  {
  background(0);
  translate(width/2, height/2);
  int step = 200;
  for (int i=0; i<step; i++)
    {
    a =i*TAU/step;
    y = 100*cos(a*3); // 3 times the frequency of the x axis
    x = 100*sin(a);
    point(x, y); 
    }
  }

:slight_smile:

1 Like

Thank you all for taking the time to reply.

kll, I wasn’t aware of processing curves function. Thank you for your examples.

paulgoux, I’m a subscriber to the Coding Challenge channel. Lots of useful stuff there.

glv, thank you for your example. My first effort did use the point function but the displayed points were so tiny. I think I can now see why. A combination of strokeWeight(4) and the number of points.

3 Likes

Hello,

Thank you so very much for your kind and thoughtful response to all of those that helped you.

:slight_smile: