Showing posts with label curve. Show all posts
Showing posts with label curve. Show all posts

2017-04-23

Dragon curve

Fig. 1. A forest...? a whirlpool...? a blaze? of Dragons. What do you think a collective noun for these should be?
You fold a paper strip absent-mindedly and a wondrous creature emerges...

If you read The Jurassic Park by M. Crichton (and I’m sure you did), you sure have noticed how each of the chapters started with a page featuring an interesting drawing. It began at first with a simple set of lines, but as you progressed in the book, it got more and more convoluted. I couldn’t figure out the rule behind it at the time, and I found the explanation only years later.

Fig. 2. Dragon curve animation by 碳酸鈣 (available at  this
 Wiki page under CC license).
The lines form a fractal, which is also called Heighway dragon, the Jurassic Park dragon, or simply a dragon curve (see fig. 2). It’s quite a nice thing to doodle! There are a few ways you can do this and the choice is yours:


1. The paper strip folding method
Take a strip of paper. Start folding it: right over left, and keep repeating: right over left, right over left… You won’t be able to do that more than 4-5 times. Don't worry! You can always make more components and connect them end to end  to get more complicated shapes (more iterations of the fractal).
Unfold  each of the components and crease all the folds so they all form right angles (see fig. 3). Place it on a flat surface, and voila!

Fig. 3. How to make the Dragon Curve by folding the paper strips.


2. The pen-and-paper method
You can very well draw the dragon on the paper, starting with a straight line segment and making proper right or left 90 degree turns. There is a neat method that can be used for the determination of the turns in the sequence. Let's start with an example (R marks a right turn and L - a left turn):

1st iteration:  R,
2nd iteration:  R_R_L,
3rd:            RRL_R_RLL,
4th:            RRLRRLL_R_RRLLRLL,


And so on… As you can see, in each of the iterations you take the previous one, add an R (right 90 degree turn) at the end, and then add the previous iteration again, but this time  both reversing i, and swapping R’s and L’s (see fig. 4).
Fig. 4. Drawing the Dragon Curve.


3. The computer friendly method
You can calculate the n-th turn direction by evaluating the following expression:

if ((n & -n) << 1) & n == 0 then:
  turn ← R;
else:
  turn ← L;

& is a bitwise and operator, << is left shift and you have to remember the -n should be encoded as two-complement. Also, the n-th full iteration is a sequence of 2n-1 turns, so you should probably calculate all of these! :)

For example, the direction of 42nd turn is:

((00101010 & 11010110) << 1) & 00101010 =
= 00000100 & 00101010 = 0,
so the 42nd turn is R.

The figure 1 comes from an old program I wrote in C to explore the possibilities offered by the Dragon curves. You can find this program here.

If you ever want to summon a dragon, just use your imagination! 



Note. This was originally posted on my Tumblr blog: http://dagothar.tumblr.com/post/26697353061/the-jurassic-park-fractal-if-you-read-the. I think this is a much better place for it though, and I'm pretty sure you haven't seen it yet anyway!

2017-04-14

Lissajous

Lissajous curves are a family of curves which can describe a complex harmonic motion. They are described with the following parametric equations:

x(t) = Ax sin (ωx t)
y(t) = Ay sin (ωy t + φ)

The parameters Ai define the amplitudes of the oscillations, the ωi are the frequencies and the φ is the phase offset between the two. The curve is typically ploted through the whole rotation: t = 0, ..., 2π.

The Lissajous curves could be produced by harmonographs (an apparatus that has a pendulum with some writing device attached to it), or on oscilloscopes, where the horizontal and vertical electromagnets would drag the electron beam along the screen. The general shape of the curve is very much dependent on the ratio between the frequencies ωx and ωy. The ratio defines the time it takes for the two orthogonal oscillations to coincide and thus the curve itself becomes more crossed and twisted. The change in phases φ in turn makes the curve rotate.

Lissajous curves are typically defined in two dimensions, but nothing bars an extension into 3D. In three dimensions the equations become:

x(t) = Ax sin (ωx t + φx)
y(t) = Ay sin (ωy t + φy)
z(t) = Az sin (ωz t + φz)

The parameters are quite alike as the ones described above. The only difference is the introduction of separate phase offsets for different axes.

I was quite interested to see the Lissajous curves in 3D. The changing of phase parameters while considering 2D equations often results in the plots twisting as if they were only a projection of a higher-dimensional entity which rotates. Adding the third dimension we can confirm it is indeed the case.

I made a Lissajous 3D curve generator, which is available HERE
The source code can be found on GitHub: https://github.com/dagothar/lissajous

The application is shown in fig. 1 below. You can change the parameters of the curve on the right: the amplitudes and the frequencies for the respective axes (X, Y and Z) can be selected with the combo boxes, while the phase shifts are set using the sliders. You can rotate the 3D view on the left by dragging the mouse around it.

Fig. 1. Lissajous curve generator.


And here's a small gallery of examples.

Fig. 2. The most basic curve is just a circle (ωx=1, ωy=1, ωz=1).
Changing the phase turns it into an ellipse.
Fig. 3. The knot seems to rotate in the plane when changing the phase parameter  (ωx=2, ωy=3, ωz=0, φx=0, φy=0:2π, φz=3.14).
Fig. 4. The same knot as above with the third dimension added. The 3D curve projects into the same image.
The rotation is now clearly visible.
Fig. 5. Changing the ratio between the frequencies ωx and ωy.

Fig. 6. This one looks like a twister (ωx=9, ωy=10, ωz=1, φx=0, φy=1.57, φz=3.14).

Fig. 7. Orbits? (ωx=24, ωy=25, ωz=22, φx=0, φy=1.57, φz=3.14).

Some of these shapes are quite surprising. What interesting and beautiful shapes can you find?