
Plotting hearts with Python and C - cfper
https://github.com/susam/heart
======
nathell
"Could I have that big red fluffy arse?" "Do you mean this plush heart?" "No.
I'm a cardiologist, I've been operating on human hearts for the last 20 years,
I know how a heart looks like, and I would very much like that red fluffy
arse."

Seriously, though, it's a little heartwarming bouquet.

------
nurettin
Definitely reminds me of my highschool days. Except my gift was written in
turbo pascal and could rotate around the Y axis without any flicker on a 486
DX. Drawing a heart on a TI-86 was too easy, so I had to up my game.

------
krknc
Found a bug:
[https://github.com/susam/heart/issues/1](https://github.com/susam/heart/issues/1)

------
userbinator
I was expecting it to be the same algorithm in two different languages, but
the Python version is a mathematical description whereas the C version is RLE-
compressed.

Relatedly, there is this video on how to procedurally generate a 3D heart:

[https://www.youtube.com/watch?v=aNR4n0i2ZlM](https://www.youtube.com/watch?v=aNR4n0i2ZlM)

------
nmake
The two functions from Python one: y = sqrt(1 - |x|) * sqrt(|x|), y = (-3/2) *
sqrt(1 - sqrt(|x|)). How do they produce the desired output? Is there a way to
construct such functions?

~~~
jihadjihad
Here's some info that might be handy:
[http://mathworld.wolfram.com/Cardioid.html](http://mathworld.wolfram.com/Cardioid.html)

------
ajay-d
Here's a widget to play with:
[https://love.imaginary.org/](https://love.imaginary.org/)

The time actually has a write up on the formula too
[https://www.nytimes.com/2019/02/14/science/math-algorithm-
va...](https://www.nytimes.com/2019/02/14/science/math-algorithm-
valentine.html)

------
matplotter
The Python graph uses two equations.

y = sqrt(1 - |x|) * sqrt(|x|)

y = (-3/2) * sqrt(1 - sqrt(|x|))

Why do these equations work and look like heart? How does one make such
equations?

~~~
jihadjihad
A brief math overview can be found here:
[http://mathworld.wolfram.com/Cardioid.html](http://mathworld.wolfram.com/Cardioid.html)

~~~
susam
A cardoid is a popular way of plotting a heart shaped curve. However, I
mentioned in another comment in this thread that the heart plotted with Python
and Matplotlib in this repository is not made of cardoid. Instead, it is made
of semicircles and quartic curves. Here's the comment with more details:
[https://news.ycombinator.com/item?id=19162360](https://news.ycombinator.com/item?id=19162360).

------
kenforthewin
cute.

------
tom_mellior
Ugh.

    
    
        for (i = 0; i < sizeof love / sizeof *love; i++)
            if (love[i] < 4)
                printf("%s", say[love[i]]);
            else
                for (j = 0; j < love[i] / 4; j++)
                    putchar(show[love[i] % 4]);
    

Yes, C doesn't force you to use braces here, but this is really overdoing it.

And I say this as someone who does like Python's indentation-based syntax.
Doing the same in C is not idiomatic.

~~~
userbinator
That's the same style in K&R, which is about as "idiomatic" as it gets. I
don't see anything wrong with it.

~~~
moccachino
What's wrong with it is that it's really really easy to make a mistake and
introduce a subtle bug that will be hard to spot. Especially now since
languages like python have been developed that have made people used to
indentation-defined code.

It's the style in K&R? Ok, so the literal inventor of the language was certain
he wouldn't make this kind of mistake, that still doesn't mean I trust myself
not to.

~~~
neatcoder
Omitting braces for single statements is quite idiomatic in the C development
world, whether we like it or not. If we don't trust ourselves that we wouldn't
make this kind of mistake, use the right tooling: 'gcc -Wall' (since GCC 6) or
'clang-format' can easily detect misleading indentation.

