Skip to content

Instantly share code, notes, and snippets.

@makoConstruct
Created March 26, 2017 00:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save makoConstruct/1a63c4c877db7e33a049c105320d5f3d to your computer and use it in GitHub Desktop.
Save makoConstruct/1a63c4c877db7e33a049c105320d5f3d to your computer and use it in GitHub Desktop.
only part of the logic for animating rectangular point and line generata as a series of strokes
void FolkBodySpec::prepAnimation(){
//start by identifying clusters
auto groupColorings = fillCopies(itemTrayDimensionX*itemTrayDimensionY, 0);
u8 nextColoring = 1;
vector<Coord> centralNode;
overRegion(itemTrayDimensionX, itemTrayDimensionY, [&](Coord c){
uint ik = itemOffset(c);
if(locatedItems[ik]){
u8 gc;
if(!groupColorings[ik]){
centralNode.push_back(c);
groupColorings[ik] = gc = nextColoring;
++nextColoring;
}else{
gc = groupColorings[ik];
}
//extend coloring to connected ones
if(horizontalConnectionFrom(c)){
Coord right = c+Coord(1,0);
groupColorings[itemOffset(right)] = gc;
}
if(verticalConnectionFrom(c)){
Coord above = c+Coord(0,1);
groupColorings[itemOffset(above)] = gc;
}
// centralNode[gc-1] = c;
}
});
//from now on we'll use the groupColorings to flag visitedness. 0 means visited.
//now, over each starting node
for(Coord sn : centralNode){
vector<pair<Coord,Coord>> remainders;
if(horizontalConnectionFrom(sn)){
remainders.push_back({sn, sn.east()});
}
if(verticalConnectionFrom(sn)){
remainders.push_back({sn, sn.south()});
}
if(remainders.size() == 0){
lineAnimations.push_back({{sn}});
}else{
vector<vector<Coord>> lines;
//over each remainder until the lines are done
do{
vector<Coord> line;
auto visit = [&](Coord p){
line.push_back(p);
groupColorings[itemOffset(p)] = 0;
}
auto p = remainders.pop();
visit(p.first);
Coord prev = p.first;
Coord cur = p.second;
while(true){
visit(cur);
while(true){
//loop over neighbors connected to cur that aren't prev
vector<Coord> neighbors;
if(horizontalConnectionFrom(cur) && cur.east() != prev){ neighbors.push_back(cur.east()); }
if(verticalConnectionFrom(cur) && cur.north() != prev){ neighbors.push_back(cur.north()); }
if(verticalConnectionFrom(cur.south()) && cur.south() != prev){ neighbors.push_back(cur.south()); }
if(horizontalConnectionFrom(cur.west()) && cur.west() != prev){ neighbors.push_back(cur.west()); }
if(neighbors.size() == 0){
goto doneLine;
}else{
bool foundOneUnvisitedNeighbor = false;
for(Coord neigh : neighbors){
if(groupColorings[itemOffset(neigh)]){
if(foundOneUnvisitedNeighbor){
remainders.push_back({cur, neigh});
}else{
prev = cur;
cur = neigh;
foundOneUnvisitedNeighbor = true;
}
}else{
//remove the remainder connecting to that, I think there must have been one, I'd assert it if that were easy
filterInPlace(remainders, [&](pair<Coord, Coord> p){ return p.first == neigh && p.second == cur; });
visit(neigh);
goto doneLine;
}
}
}
}
}
doneLine:
lines.emplace_back(move(line));
}while(!remainders.empty());
lineAnimations.emplace_back(move(lines));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment