Code Monkey home page Code Monkey logo

ascii-fluid-simulation-deobfuscated's Introduction

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.

ascii-fluid-simulation-deobfuscated's People

Contributors

davidedc avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.