Does someone maybe know a good l-system class? I’ve been googling and found some, but I’m wondering if someone knows or has a l-system lib in c++?
roxlu
Does someone maybe know a good l-system class? I’ve been googling and found some, but I’m wondering if someone knows or has a l-system lib in c++?
roxlu
i made a template L-Sys-Class.
the license is: i don’t care what you do with it, but if you find a bug, please post it here.
best
joerg
LSystem.h
/*
* LSystem.h
* abcdefg
*
* Created by joerg on 12.10.09.
* Copyright 2009 __MyCompanyName__. All rights reserved.
*
*/
#ifndef _LSYSTEM_H
#define _LSYSTEM_H
#include <vector>
using namespace std;
template<typename Element, class Sequence> class LSystem
{
class LSystemRule
{
Element head;
Sequence tail;
public:
LSystemRule(Element hd, Sequence tl) {head = hd; tail = tl;}
bool isMatch(Element hd) {return hd == head;}
Sequence &getTail() {return tail;}
};
Sequence state;
vector<LSystemRule *> rules;
vector<Element> heads;
vector<Sequence> tails;
public:
LSystem();
~LSystem();
void setState(Sequence st) {state = st;}
Sequence &getState() {return state;}
void addRule(Element hd, Sequence tl);
Sequence &replace(Element &hd);
void iterate();
};
void initLSystem();
#endif
LSystem.cpp
/*
* LSystem.cpp
* abcdefg
*
* Created by joerg on 12.10.09.
* Copyright 2009 __MyCompanyName__. All rights reserved.
*
*/
#include "LSystem.h"
#include <string>
template<class Element, class Sequence> LSystem<Element, Sequence>::LSystem()
{
}
template<class Element, class Sequence> LSystem<Element, Sequence>::~LSystem()
{
for(int i = 0; i < rules.size(); i++)
{
delete rules[i];
}
}
template<class Element, class Sequence> void LSystem<Element, Sequence>::addRule(Element hd, Sequence tl)
{
rules.push_back(new LSystemRule(hd, tl));
}
template<class Element, class Sequence> Sequence &LSystem<Element, Sequence>::replace(Element &hd)
{
static Sequence seq;
seq.clear();
for(int i = 0; i < rules.size(); i++)
{
if(rules[i]->isMatch(hd))
return rules[i]->getTail();
}
seq.push_back(hd);
return seq;
}
template<class Element, class Sequence> void LSystem<Element, Sequence>::iterate()
{
Sequence newstate;
for(int i = 0; i < state.size(); i++)
{
Sequence &replacement = replace(state[i]);
for(int e = 0; e < replacement.size(); e++)
{
newstate.push_back(replacement[e]);
}
}
state = newstate;
}
void initLSystem()
{
/*LSystem<char, string> test;
test.setState("geschmurgel");
test.addRule('e', "ebe");
test.addRule('u', "ubu");
test.iterate();
printf("state1: %s\n", test.getState().c_str());
test.iterate();
printf("state2: %s\n", test.getState().c_str());
test.iterate();
printf("state3: %s\n", test.getState().c_str());*/
}
Here some code that produces beautiful plants. I believe they are the nicest without using shaders or lights.
#pragma once
#include “ofMain.h”
class Renderer
{
public:
void setup();
void draw();
int angleDepart = 0;
const int kWindow_height = 1000;
const int kWindow_width = 1200;
void splitLine(int iX, int iY, int iThickness, float angle);
};
#include “renderer.h”
void Renderer::setup()
{
ofSetFrameRate(3);
ofSetWindowShape(kWindow_width, kWindow_height);
}
void Renderer::draw()
{
ofFill();
ofSetLineWidth(10);
ofClear(0);
ofScale(1.0f, -1.0f);
ofTranslate(kWindow_width / 2, -kWindow_height + 75);
ofLine(0,0,0, 300);
angleDepart -= 1;
splitLine(0, 300, 20, angleDepart); // faire varier le 20, en 10 et 20, c’est le plus intéressant
}
void Renderer::splitLine(int iX, int iY, int iThickness, float iAngle)
{
if (iThickness - 1 > 0)
{
ofPushMatrix();
ofTranslate(iX, iY);
ofRotateZ(iAngle);
ofSetLineWidth(iThickness - 1);
ofLine(0, 0, iX * 0.75, iY * 0.75);
splitLine(iX * 0.75, iY * 0.75, iThickness - 1, iAngle);
ofPopMatrix();
ofPushMatrix();
ofLine(0, 0, iX * 0.75, iY * 0.75);
ofSetColor(65, ofRandom(255), 0);
splitLine(iX * 0.75, iY * 0.75, iThickness - 2, -iAngle);
ofPopMatrix();
}
}
#include “ofMain.h”
#include “application.h”
int main( )
{
ofSetupOpenGL(512, 512, OF_WINDOW);
ofRunApp(new Application());
}