Skip to content

Instantly share code, notes, and snippets.

@alexesDev
Created September 22, 2012 14:02
Show Gist options
  • Select an option

  • Save alexesDev/3766252 to your computer and use it in GitHub Desktop.

Select an option

Save alexesDev/3766252 to your computer and use it in GitHub Desktop.
Simple L-System
struct State
{
vector3df rotate;
vector3df prevPoint;
State() : rotate(vector3df()) {}
State(const State& other) : rotate(other.rotate), prevPoint(other.prevPoint) { }
};
typedef std::vector<line3df> Lines;
typedef std::stack<State> States;
typedef std::map<char, std::string> Rules;
typedef std::function< void() > ActionFunction;
typedef std::map<char, ActionFunction> Actions;
std::string command;
Lines lines;
Rules rules;
Actions actions;
// Start data
command = "F";
rules['F'] = "F[+F]F[-F][F]";
float angle = 20;
State currentState;
States states;
states.push(currentState);
vector3df point;
actions['F'] = [&states, &lines]()
{
vector3df point = states.top().prevPoint + vector3df(0, 10, 0);
point.rotateYZBy(states.top().rotate.X, states.top().prevPoint);
point.rotateXZBy(states.top().rotate.Y, states.top().prevPoint);
point.rotateXYBy(states.top().rotate.Z, states.top().prevPoint);
lines.push_back(line3df(point, states.top().prevPoint));
states.top().prevPoint = point;
};
actions['+'] = [&states, &angle](){ states.top().rotate.X += angle; };
actions['-'] = [&states, &angle](){ states.top().rotate.X -= angle; };
actions['&'] = [&states, &angle](){ states.top().rotate.Y += angle; };
actions['^'] = [&states, &angle](){ states.top().rotate.Y -= angle; };
actions['\\'] = [&states, &angle](){ states.top().rotate.Z += angle; };
actions['/'] = [&states, &angle](){ states.top().rotate.Z -= angle; };
actions['['] = [&states, &angle](){ states.push(State(states.top())); };
actions[']'] = [&states, &angle](){ states.pop(); };
for(int i = 0; i < 5; ++i)
{
std::string temp = "";
for(auto it = command.begin(); it != command.end(); ++it)
{
if(rules.find(*it) != rules.end())
temp += rules[*it];
else
temp += *it;
}
command = temp;
}
for(auto it = command.begin(); it != command.end(); ++it)
{
if(actions.find(*it) != actions.end())
actions[*it]();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment