Created
May 5, 2016 16:35
-
-
Save PtrMan/f789f17f7848e39db2eac1dcad8da570 to your computer and use it in GitHub Desktop.
CA fluid simulation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // part of the code useful for a stochastic fluid dynamics simulation based on a triangle grid of celular automata | |
| typedef unsigned uint; | |
| struct Dot { | |
| bool current, next; | |
| uint nextCounters; // number of potential particles which want to occupy this place | |
| }; | |
| //bool unevenLineInZDirection = ((x) % 2) ^ ((y) % 2); | |
| const uint NUMBER_OF_NEIGHBORS = 8; | |
| Dot *searchDotWithIndexWhichIsNotNextOccupied(Dot *neighbors[NUMBER_OF_NEIGHBORS], uint usedNeighbors, uint index) { | |
| uint currentIndex = 0; | |
| for( uint i = 0; i < usedNeighbors; i++ ) { | |
| if( !neighbors[i]->next ) { | |
| if( currentIndex == index ) { | |
| return neighbors[i]; | |
| } | |
| currentIndex++; | |
| } | |
| } | |
| // TODO< throw something > | |
| return (Dot*)0; | |
| } | |
| uint countUnoccupiedNeighbors(Dot *neighbors[NUMBER_OF_NEIGHBORS], uint usedNeighbors) { | |
| uint neightborOccupiedCounter = 0; | |
| for( uint i = 0; i < usedNeighbors; i++ ) { | |
| if( !neighbors[i]->next ) { | |
| neightborOccupiedCounter++; | |
| } | |
| } | |
| return neightborOccupiedCounter; | |
| } | |
| void moveToNeighborByRandom(Dot *currentDot, Dot *neighbors[NUMBER_OF_NEIGHBORS], uint usedNeighbors, uint rng) { | |
| if( !currentDot->current ) { | |
| return; | |
| } | |
| uint neightborOccupiedCounter = countUnoccupiedNeighbors(neighbors, usedNeighbors); | |
| if( neightborOccupiedCounter == usedNeighbors ) { | |
| // if all neighbors are occupied we can't move it to a enightbor node | |
| } | |
| else { | |
| // choose another dot and move the current dot to it | |
| uint destinationIndex = rng % neightborOccupiedCounter; | |
| Dot *notOccupiedDestination = searchDotWithIndexWhichIsNotNextOccupied(neighbors, usedNeighbors, destinationIndex); | |
| notOccupiedDestination->nextCounters++; | |
| currentDot->next = false; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment