Twist-and-Flip
Yet another chaotic algorithm is the twist-and-flip algorithm. Typically,
you do something similar to the following process:
- Choose an initial rotator point r and a set {zk}
of other points, usually in a line or some other plane figure.
- Let zk(n) denote point z0 after
n iterations. For each point:
- Rotate point zk(n-1)
around the rotator point r, but not a constant amount (this is
the twisting part). The amount a point is rotated will be some function of the
distance between z(n-1)k and r.
- Flip the point through the origin; that is, if zi was
(xi, yi), then it becomes (-xi,
-yi).
- You have the "lather" and the "rinse" of the algorithm;
now, repeat.
So that's one standard algorithm. Just for fun, I decided to expand on this
and make something so random-seeming that it might just get me extra-credit, so
I wrote a complex number class and used that as the basis (The x-axis is the
real part and the y-axis is the imaginary part for each z
C).
My algorithm is as follows:
- The rotator point r is at (-0.2 + 0.6i).
- The line to be iterated upon is 100,000 points long and has endpoints as
determined by the user's initial two mouse clicks.
- For each point zk, let modk be the
modulus of (z0 - r) (that is, the distance between the
rotator and the rotatee) and let argk be the
counter-clockwise angle between the x-axis and zk. Then:
- Multiply zk by 1 / argk.
- Rotate this new point by (modk / pi) radians to obtain
the new point, zk+1.
- And of course, repeat for as many iterations as desired.
My algorithm uses some nice properties of complex numbers that make twisting
and flipping fairly easy. Let's assume we have to copmlex numbers z and
w, which we shall express in polar form as z = rz
cis Az and w = rw cis Aw
(sorry, but there are no thetas on the keyboard!). Conveniently, we can prove
(but will not here) that wz = rwrz cis
(Aw+Az). You can see how this has some nice
properties for twisting and flipping!
Okay, Gary, I hear you thinking, this is a great algorithm. But
can I see your code? How about some pictures? Of course you can! Here they
are:
|
- My code: twistflip.cpp
- My complex class which I used in my code:
complex.h
- Pictures! Both an animated GIF with 10
iterations and the individual iterations themselves.
|