Skip to main content

Warm-up curves · trigonometry

Lissajous figure

Two sine waves at different speeds trace a knot that loops back on itself.

Write lissajous(a, b, n). Return n + 1 points [x, y] along a Lissajous figure, and call show(points) after adding each point so the curve draws as it grows.

For i from 0 to n, let t = 2 * Math.PI * i / n. The point is [Math.cos(a * t), Math.sin(b * t)]. Both coordinates stay between -1 and 1, and because t goes all the way round, the last point lands on the first.

Example: lissajous(1, 1, 4) walks a circle in four steps: [[1, 0], [0, 1], [-1, 0], [0, -1], [1, 0]], give or take tiny float noise. Answers are checked to within 0.000000001.

Tests you can see

  • lissajous(1, 1, 4)[[1,0],[6.123233995736766e-17,1],[-1,1.2246467991473532e-16],[-1.8369701987210297e-16,-1],[1,-2.4492935982947064e-16]]
  • lissajous(1, 2, 8)[[1,0],[0.7071067811865476,1],[6.123233995736766e-17,1.2246467991473532e-16],[-0.7071067811865475,-1],[-1,-2.4492935982947064e-16],[-0.7071067811865477,1],[-1.8369701987210297e-16,3.6739403974420594e-16],[0.7071067811865474,-1],[1,-4.898587196589413e-16]]
  • lissajous(3, 2, 120)[[1,0],[0.9876883405951378,0.10452846326765346],[0.9510565162951535,0.20791169081775931],[0.8910065241883679,0.3090169943749474],[0.8090169943749475,0.40673664307580015],[0.7071067811865476,0.49999999999999994],[0.5877852522924731,0.5877852522924731],[0.45399049973954686,0.6691306063588582],[0.30901699437494745,0.7431448254773942],[0.15643446504023092,0.8090169943749475],[6.123233995736766e-17,0.8660254037844386],[-0.1564344650402306,0.9135454576426009],[-0.30901699437494734,0.9510565162951535],[-0.45399049973954675,0.9781476007338057],[-0.587785252292473,0.9945218953682733],[-0.7071067811865471,1],[-0.8090169943749473,0.9945218953682734],[-0.8910065241883678,0.9781476007338057],[-0.9510565162951535,0.9510565162951536],[-0.9876883405951377,0.913545457642601],[-1,0.8660254037844387],[-0.9876883405951378,0.8090169943749475],[-0.9510565162951538,0.7431448254773945],[-0.8910065241883679,0.6691306063588583],[-0.8090169943749475,0.5877852522924732],[-0.7071067811865477,0.49999999999999994],[-0.5877852522924732,0.40673664307580004],[-0.4539904997395469,0.3090169943749475],[-0.30901699437494756,0.20791169081775931],[-0.15643446504023018,0.10452846326765329],[-1.0718754395722282e-15,5.66553889764798e-16],[0.15643446504022981,-0.10452846326765305],[0.30901699437494723,-0.20791169081775907],[0.45399049973954664,-0.3090169943749473],[0.5877852522924729,-0.4067366430758002],[0.7071067811865474,-0.4999999999999997],[0.8090169943749473,-0.587785252292473],[0.8910065241883678,-0.6691306063588582],[0.9510565162951535,-0.743144825477394],[0.9876883405951377,-0.8090169943749473],[1,-0.8660254037844385],[0.9876883405951379,-0.9135454576426005],[0.9510565162951536,-0.9510565162951535],[0.8910065241883683,-0.9781476007338056],[0.8090169943749481,-0.9945218953682733],[0.7071067811865477,-1],[0.5877852522924734,-0.9945218953682734],[0.45399049973954625,-0.9781476007338056],[0.30901699437494773,-0.9510565162951536],[0.15643446504023203,-0.9135454576426011],[3.061616997868383e-16,-0.8660254037844386],[-0.15643446504022968,-0.8090169943749476],[-0.3090169943749471,-0.743144825477394],[-0.4539904997395473,-0.6691306063588581],[-0.5877852522924729,-0.5877852522924734],[-0.7071067811865479,-0.49999999999999967],[-0.8090169943749472,-0.40673664307580015],[-0.8910065241883681,-0.3090169943749476],[-0.951056516295154,-0.20791169081775898],[-0.9876883405951378,-0.10452846326765342],[-1,-1.133107779529596e-15],[-0.9876883405951379,0.10452846326765293],[-0.9510565162951542,0.2079116908177585],[-0.8910065241883685,0.3090169943749472],[-0.8090169943749477,0.4067366430757997],[-0.7071067811865485,0.4999999999999993],[-0.5877852522924735,0.5877852522924729],[-0.45399049973954797,0.6691306063588578],[-0.30901699437494784,0.7431448254773942],[-0.1564344650402304,0.8090169943749472],[-4.286263797015736e-16,0.8660254037844384],[0.1564344650402313,0.9135454576426009],[0.309016994374947,0.9510565162951535],[0.4539904997395456,0.9781476007338055],[0.5877852522924728,0.9945218953682733],[0.7071067811865466,1],[0.8090169943749471,0.9945218953682734],[0.8910065241883672,0.9781476007338058],[0.9510565162951534,0.9510565162951536],[0.9876883405951378,0.9135454576426008],[1,0.8660254037844392],[0.9876883405951377,0.8090169943749476],[0.9510565162951543,0.7431448254773952],[0.8910065241883669,0.6691306063588576],[0.8090169943749478,0.5877852522924734],[0.7071067811865472,0.4999999999999998],[0.587785252292475,0.40673664307580104],[0.4539904997395481,0.3090169943749478],[0.3090169943749496,0.20791169081776084],[0.15643446504023054,0.10452846326765265],[5.51091059616309e-16,3.6739403974420594e-16],[-0.1564344650402312,-0.10452846326765369],[-0.3090169943749469,-0.2079116908177584],[-0.4539904997395471,-0.30901699437494706],[-0.5877852522924741,-0.4067366430758004],[-0.707106781186549,-0.5000000000000007],[-0.8090169943749471,-0.5877852522924728],[-0.8910065241883663,-0.669130606358857],[-0.9510565162951529,-0.7431448254773936],[-0.9876883405951375,-0.8090169943749472],[-1,-0.8660254037844387],[-0.987688340595138,-0.9135454576426005],[-0.9510565162951543,-0.9510565162951534],[-0.8910065241883686,-0.9781476007338054],[-0.8090169943749478,-0.9945218953682734],[-0.7071067811865474,-1],[-0.5877852522924722,-0.9945218953682733],[-0.4539904997395482,-0.9781476007338058],[-0.30901699437494806,-0.9510565162951538],[-0.15643446504023417,-0.9135454576426015],[1.1028010998692062e-15,-0.8660254037844383],[0.15643446504022931,-0.8090169943749477],[0.3090169943749468,-0.7431448254773941],[0.45399049973954697,-0.669130606358859],[0.587785252292474,-0.5877852522924735],[0.7071067811865489,-0.4999999999999999],[0.8090169943749491,-0.40673664307579954],[0.8910065241883679,-0.3090169943749479],[0.9510565162951539,-0.20791169081775923],[0.9876883405951374,-0.10452846326765454],[1,-2.266215559059192e-15]]

More tests are hidden. The server runs them all before a time counts.

Call show() while your code works and the run plays back here.

javascriptlissajous()

Tab indents · Cmd/Ctrl+Enter runs