wolfsir
Joined 10 years ago
Joined 10 years ago
One advantage of LifeHash using a small world-size is that the pattern inevitably becomes stable (repeats a state) after a reasonable number of iterations. I do have a hard-coded limit for the number of iterations, but it's just a backstop and large enough so I never see it hit in practice. A much larger, more complex simulation could go on for much longer, and since I really want to end up with a static icon, I want to limit the time and complexity of computing it.
Another advantage of using a small world size is that the initial 16x16 cell grid includes exactly 256 cells, which is the same number of bits in a SHA256 hash. Any hash, visual or otherwise, should be extremely sensitive to initial conditions, so a small change in input yields a massive change in output. Having the same number of cells as hash bits means every single bit counts with equal significance to the outcome.
The basic concept is to take a SHA256 hash of the input data (which can be any data including another hash) and then use the 256-bit digest as a 16x16 pixel "seed" for running the cellular automata known as Conway’s Game of Life.
After the pattern becomes stable (or begins repeating) the resulting history is used to compile a grayscale image of all the states from the first to last generation. Using Game of Life provides visual structure to the resulting image, even though it was seeded with entropy.
Some bits of the initial hash are then used to deterministically apply symmetry and color to the icon to add beauty and quick recognizability.
CGFloat, depending on the runtime, may either be a Float or a Double, so explicitly converting to and from it is required.
Rather than create a byzantine set of rules about whether it is "safe" to convert from one type to another, Swift wisely requires that all type conversions be explicit.