One and one - it turns out two. All alone - here you are, and there I am.
People are always alone doubly alone with themselves.
If something could bring them closer together, it would immediately be one of the two.
Let mathematics add hearts - to make our way to the end.
Williams Jay, "Heroes Nowhere"
Probably, the post should be called "How to draw an animated heart for Valentine's Day, using math for other purposes." I rejected this title in favor of a more poetic one: after all, a wonderful romantic holiday is coming, which we, the IT people and other nyards, should meet fully armed. I will immediately show you the result, and under Habrakat there will be many letters about how I achieved this result.

')
Disclaimer
I realize that a beautiful flashing heart can be done without the slightest knowledge of mathematics. But is it interesting?
Step 1. Parameterize the heart.
To begin with, we need a mathematical object that even remotely resembles a heart. Fortunately, for me this step was trivial: a couple of years ago I discovered a wonderful formula for just such a case (for aesthetic reasons, the graph in the figure is stretched horizontally, in fact it should fit between -1 and 1).

The formula was found from the following considerations: take an ordinary circle
(x = cos (t); y = sin (t)) and imagine that it consists of jelly, while being rigidly attached to the ordinate axis. Now let's “blow” to it from below: add to the coordinate of the game some function w (x) = w (x (t)), equal to zero for x = 0, monotonously increasing for x> 0 and even in x. After such a “blow”, the halves of the circle will move upwards, forming the “bulges” of the heart, and due to the rigid attachment to the Y axis, the lower tail and the upper dent are formed. In this case, w (x (t)) = | x |
1/2 = | cos (t) |
1/2 . You can try the other “blow function” on your own and see what comes of it.
Step 2. From parametric assignment to implicit function.
For our insidious plan, the parametric equation (x (t); y (t)) is inconvenient; preferably it would be an implicit task of the form f (x, y) = 0. Well, everything is in our hands. So, the pants are turning:
x = cos (t)
y = sin (t) + | cos (t) | 1/2
y - | x | 1/2 = sin (t)
(y - | x | 1/2 ) 2 + x 2 = 1
f (x, y) = (y - | x | 1/2 ) 2 + x 2 - 1 = 0
Step 3. From an implicit function to a function of two variables. Color function.
Having f (x, y) in our hands, we can finally fulfill our dream: to draw a beautiful color picture. For this we need another function: the color function. It must take a real argument r and return an integer value from 0 to 255. It is also desirable that it be monotone on each semi-axis and have a maximum at the zero point. As such a function, you can take, for example, this:
c (r) = max ([255 - 100 * | r |], 0)
Here 100 is the “magic number”, later we will completely replace it with the “good programming style” by the parameter.
Now for each point (x, y) we can set the color as rgb (c (f (x, y)), 0, 0). Those points that previously belonged directly to the “heart” graphics turned bright red (note the fixed light contour on the gif). As you move away from the graph, the color will fade until it turns black at some distance from it.
Step 4. Add a parameter, create an animation.
Now replace the magic number 100 with the parameter k. The new color feature looks like this:
c (r, k) = max ([255 - k * | r |], 0)
Let k be some function of time. Then for each point of the image at each moment of time we can calculate its color (which is, in fact, the mathematical definition of animation). At first I wanted to take something like k (t) = 80 (sin (t) +1). Then, however, I realized that with a large number of gif frames, it would weigh more than 640 kilobytes. On the other hand, with a small number of frames it makes no sense to bother with the analytical task k (t). As a result, in order to achieve heart pulsation, I successively assigned k values ​​of 80, 90, 100, 110, 120, 110, 100, 90, and then the images generated for these values ​​were combined into a cyclic GIF. In general, everything.
Conclusion
Unfortunately, I was not able to arrange a surprise for my girlfriend: she sneakily crept behind me just as I was generating frames for the animation. However, she liked it.
Artists, designers and other comrades with a keen sense of beauty will surely say that a heart could be more beautiful. In part, I agree with them: the picture is not without flaws. However, her true beauty is in mathematical rigor. My girlfriend appreciated it. And you?