All Projects → davidedc → Ascii-fluid-simulation-deobfuscated

davidedc / Ascii-fluid-simulation-deobfuscated

Licence: other
the best de-obfuscated version of Yusuke Endoh's "Most complex ASCII fluid" obfuscated C code competition 2012 entry

Programming Languages

c
50402 projects - #5 most used programming language

What

The best de-obfuscated versions of Yusuke Endoh's "Most complex ASCII fluid" obfuscated C code competition 2012 entry. The original source is here. There are a couple of other de-obfuscated versions online, but they only basically de-macro and break down some constructs.

These go the full way. There are three versions:

  1. "asciiFluidSimulation". Preserves the "single array of complex numbers" approach. Removes all pointer arithmetic and provides extended commentary on the technique used to calculate and render the fluid simulation.

  2. "asciiFluidSimulationWithoutComplexNumbers". Same as 1 but also removes all use of complex numbers, just uses standard doubles instead (using complex numbers for vectors is clever and compact but convoluted), and separate arrays are used for each data field of particles.

  3. "asciiFluidSimulationWithStructsWithoutComplexNumbers". Same as 2 but particles data are now cleanly put into a struct.

Also see a browser-based implementation here.

How to run

gcc asciiFluidSimulation.c -o asciiFluidSimulation

then run it with one of the provided example .txt files (most of them from Yusuke Endoh) like so:

./asciiFluidSimulation < ./examples/terraces.txt

P.S. add the -O3 flag when compiling (gcc -O3 ...) to really speed up the simulation.

Examples

Column:

Terraces:

Pour-out:

Clock:

How does it work?

...it uses a version of the "Smoothed-Particle Hydrodynamics" (SPH) method.

Briefly: each particle has an associated velocity vector. At each step, the system calculates the "density" of each particle (a scalar). Then it calculates the total force (due to gravity, the distance from and the density of all the other particles). Finally, it modifies the velocity of each particle due to the calculated resulting force, and updates its position according to the velocity.

More in depth:

...it first calculates the "density" of each particle, which is just a number (a scalar). The density of each particle is calculated by checking how close it is to each other particles, and it gives a sense of how "compressed" the particle is. For example a particle in the middle of a cluster of particles has high density, while a particle on the edge has low density. The density is useful because it measures how much a particle is free to move. So a force applied to a low-density particle will move it much more than the same force applied to a high-density particle.

Next, it calculates the actual force to be applied to each particle. The total force on a particle is a vector that adds the gravity to a repulsion force from each other particle (particles tend to "spread away" from each other). The force between two particles is affected by their distance and their densities.

Then, it calculates the new velocity and the new position of each particle based on its force vector.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].