M,                                 ,,,SSX888XSS082XXX                    
        7r8.                            i                  .X20XXS0S            
           iZS2S27                      ,                           SX22.       
                  a0X8Xa                i                               :8      
                        i80XS8X7        ,                                 Z     
                                XSaXaS,                                 XZ      
                                      ,7Z2a2X                        XaX        
                                             iZa2aX              .2SZ           
                                        ;          ;Z2Xar    r2XZi              
                                        .               rMB0M;                  
                                        r         SSZS0r      S2Zi              
                                             ;8SXZ               .ZXZ           
                                        ZXa2X                        rZi        
                                   S8SZi.                               0       
                             ;ZaZ2i     :                                B      
                        .8X22           .                                 B     
                    ;aZS;               r                                B      
                 aaZ                    .                               M       
             iSXr                       i                            ,B2        
 ......,  :28i    .  ..,..... ........, ; ....,,..  ..,,.... .    i8SX   ....,,.
        .X;                             .                      SXZ.             
       2;                               ;                  XZ27                 
       W                                ,             r0SSr                     
       W                                i        ,82ZZ                          
        0                                   ;ZaZ2,                              
         aZ                            :@Xa2.                                   
           rSS                   iaXaaX                                         
              S2aX          ;a2a2.      ;                                       
                  7rSiiiX;S;            .                                       
                   Xi222;iS:            ;                                       
              ZZaS;         0XZSZ;      .                                       
           aar                    aZSZ2i                                        
        7Sr                             ZZaXaZr                                 
       M                                       X20ZXa7,                         
       M                                ,             ,aS8XSX.                  
        XaS.                            :                    :SZXaXX            
           ;22XS0S.                     ;                          .2ZXZ.       
                  i0SS0aX888X,                                           Ba     

Parametric Cartesian equation: x = asin(nt + c), y = bsin(t)
Can also be written in the form: x = acos(wxt - dx), y = acos(wyt - dy)

Lissajous curves or Lissajous figures are sometimes called Bowditch curves after Nathaniel Bowditch who studied them in 1815. They were studied in more detail (independently) by Jules-Antoine Lissajous in 1857.

Lissajous curves tend to look somewhat like the figures projected during laser light shows, and there is good reason for that. Lissajous would use sounds of different frequencies to vibrate a mirror. A beam of light reflected from the mirror would trace patterns which depended on the frequencies of the sounds. This is quite similar to the laser light shows of today, except Lissajous' setup used a simple beam of light instead of a powerful laser.

Before the days of digital frequency meters and phase-locked loops, Lissajous figures were used to determine the frequencies of sounds or radio signals. A signal of known frequency was applied to the horizontal axis of an oscilloscope, and the signal to be measured was applied to the vertical axis. The resulting Lissajous figure was a function of the ratio of the two frequencies. Compare the resulting figure to a figure from known frequencies, and you could then extrapolate the unknown frequency.

Lissajous figures often appeared as props in early science fiction movies. One of the most obvious examples can be found in the opening sequence of "The Outer Limits". The pattern of criss-cross lines is actually a Lissajous figure.

The curves can either loop back upon themselves, or repeat forever. They close upon themselves if and only if wx/wy is rational.

A few more examples of Lissajous curves...

    ,iii          :          .iii.        i                 :       77r         
   2i   X2        i         ai   r;       B                 i     ;W   W        
  7,      Z:      i       ;2      2       2                 i    ;Z     0       
  0        7i     i      Si        B       8                i    W       2      
 X.         7;    i     2,         2       0                i   M        8      
 0           rr   i    Z.           2      B                i  W          a     
 0            7i  i   X.            0      Z                i  @          2     
 a             S, i  ;r             0       Z               : M           Z     
.i              2 . iX              0       0                iS            0    
,.               a  X               a       8                M             8    
;: ,,,,,,,,,,,,,  MS  ,,,,,,,,,,,,, 8    ., 0  ,,,,,,,,,,,. M  ,,,,,,,,,,, 8  , 
i.                BS                Z        Z              @               0   
,,               a  a               Z        0             M                W   
.;              8 , .2              8        X            Z,.               M   
 a             a. i  iX             0        ,r           M :               a   
 0            S:  i   ;:            0         B          M  i                M  
 0           Xi   i    2:           S         2         .a  i                W  
 r,         2:    i     Xi         a           M        M   i                W  
  0        S,     i      rX        0           Z.      M    i                Z  
  ;i      0       i       .8      a             M     0i    i                 Z 
   Xr   aS        i         SX   Xi              M   M.     i                 B 
    .77X          ;           X7X                 2Sa       ;                 ; 
 7:    X7r       rar;       77r    ;i     ;         r7XX7:  :            7aa:   
 Xr: ,a   Zi   XZ .  Zr   72   0  2 0     i7      ZB.    .27r          2B.  7Z  
,. a 2     i; Z   i   :7 a.     8 7 a      8    iM          aS       :M      W  
i.  M       ,M    i     M        M  a       W  Xa           , X;    2S        M 
i. .0       2ii   i    Si;       M  a        r;X            i  .8  @:         M 
.: 8 8     X  S   i   i, 7:     8 X 8        7Z             i    S@           M 
 : S X     Z   B  i   Z   0    r: B 8        0X             i    M7           M 
 XX   B   B     8 i  B     8   B  i Z       ri X            i  :M  a.         W 
 SS   r: X      i.. 8      ;i 2    XS       M  X:           i .B    Z         M 
 ar    8 8       Z .;       S Z    SS      iX   W           ..W      0       iX 
 @      M         07         M     2:      M     M          S2        2      M  
 M .ii. M ,iiiii, MX :iiiii..M ,ii 8r      M iii. M .iiii, 2W ,iiiii:.iB ,i: M .
 Zi    Z 2       7.:        7 X    X7     :7       M      Xr           ,r    S  
 SS   :r Z       ;  B      .7 8    X2     M        ;Z    Z7 :           7i  M   
 XS   B   B     B :  0     B   0  :.a     @         ;X  8;  i            Z  Z   
 i 7 7.    S   8  i  ,X   Z    a  0 8     M          ,i0,   i             ;M    
.: 8 Z     2  ri  i   X  :7     0 X 8     M           MZ    i             0r    
i. ;X       Z X   i    Z S      iX. a     M          M  0   i            :rS    
i.  M        M    i     M        M  a     M        X0    8S ,           ;0  W   
:. S.r     ,S Z   i    2 8      S i Z     .8      8r      ;0r          8S    2  
 ;.r r;   r7   aX .  XS   ai   a: Z 8      W    aW          27S      Z0      2. 
 r:   ,ii;       i7:;      .;i;    ii       rrr7            .  riiii7         ; 

Of course, Lissajous curves aren't only popular in mathematics. Demo makers like, them, too. Why? Because you can use Lissajous curves (or Lissies) to create a fairly neat effect that takes up very little CPU time, something you don't have much of if you're coding a demo for, say a C64 or a Gameboy.

First, let's talk a look at what the mathematics actually mean. Listen to ink-:

Parametric Cartesian equation: x = asin(nθ + c), y = bsin(t)

So what does that mean? It means that while the x is tracing a sine wave with one period, y is tracing out a sine wave with a different period. If you were to plot a point (or a sprite) on (x,y) while slowly advancing t, your point (or sprite) would trace out the curve, flowing smoothly around, thanks to the curvy nature of a sine wave.

Clever readers will have gotten ahead of me. 'But wait,' you cry, 'you're going to have to compute sins. Floating point math is slow, and trigonometric functions are even slower!' True. But no one said we have to do the math in real-time. What we need are a couple of lookup table. Take a look at the following function:

x = (sin(2πθ/n) + 1) * (h/2)

Now, sample x at every integer θ from 0 to n-1, and what have you got? You have an array with n entries, which describes one cycle of a sine wave using integer values from 0 to h.

All right, now create two of these arrays, with a different value of n for each. Now you've got some lookup tables. Grab the first entry out of each table (let's call that x and y), and plot a point at (x,y). Now do the same with the second entry, and the third. If you get to the end of one table, wrap around to the begining. Easy, isn't it? And all you need it a couple of table lookups, no messy floating point math in real-time. This is important if your machine of choice has a 4MHz CPU and no floating point unit.

But enough talk. Let's see some code!

#define PI             3.14159

#define X_TABLE_SIZE   523
#define Y_TABLE_SIZE   751

#define X_SCREEN_SIZE  320
#define Y_SCREEN_SIZE  200

void Lissy(void)
   //our two lookup tables
   int xTable[X_TABLE_SIZE];
   int yTable[Y_TABLE_SIZE];
   int theta;
   int xTableOffset;
   int yTableOffset;
   //fill the lookup tables
   for(theta = 0; theta++; theta < X_TABLE_SIZE)
      xTable[theta] = (sin( (2 * PI * theta) / X_TABLE_SIZE ) + 1) *
                      (X_SCREEN_SIZE / 2);            
   for(theta = 0; theta++; theta < Y_TABLE_SIZE)
      xTable[theta] = (sin( (2 * PI * theta) / Y_TABLE_SIZE ) + 1) *
                      (Y_SCREEN_SIZE / 2);            
   //ok, now lets get onto the fun stuff!
   //Have a sprite trace a Lissajous curve
   //and don't let it stop!   
      //advance our offsets, with wraparound
      xTableOffset = (xTableOffset + 1) % X_TABLE_SIZE;
      yTableOffset = (yTableOffset + 1) % Y_TABLE_SIZE;
      //now, draw something
      MoveSprite(xTable[xTableOffset], yTable[yTableOffset];

So the obvious question now is, why the extremely odd numbers for X_TABLE_SIZE and Y_TABLE_SIZE? The answer is more math! Find the lowest common denominator of X_TABLE_SIZE and Y_TABLE_SIZE (hint, they're both prime numbers, so the LCD is X_TABLE_SIZE * Y_TABLE_SIZE, or 392773). That's how many frames we must run before both offset are 0 at the same time (and thus the curve repeats). By picking two prime numbers, we maximise the period of the Lissajous curve, and make our display more 'interesting.'

Why is this true? More math!

Let the size of the smaller table be x
Let the size of the larger table be y.
Let the offset into the smaller table be a.
Let the offset into the larger table be b.
Let the number of frames we've done be f.

Every time we increment f, we also increment increment a and b. Then we set if a is ever greater than x, we set a to 0. We do the same with b and y. It would also be correct to say that a is the remainder of (f/x) and b is the remainder of (f/y). Therefore, a will only be 0 when (f/x) is a whole number, and b will only be 0 when (f/x) is a whole number. The lowest common denominator of any two numbers, among other things, is the lowest number that, when divided by either of the two numbers gives a whole number result. Therefore a and b will both be 0 when f is equal to the LCD of x and y. It is easy to see that at that point the pattern will repeat.

So, in conclusion:

  • Lissajous curves can be used to make simple demoeffects.
  • Lookup tables are cool
  • Pick prime number as the size of your lookup table
  • Math is fun!

Log in or register to write something here or to contact authors.