CGAssignment
Tree Class Reference

Public Member Functions

 Tree (string _str, Point _pos, double _len, double _dir, int _gen, int _col, int _width)
 
void Process ()
 

Constructor & Destructor Documentation

Tree::Tree ( string  _str,
Point  _pos,
double  _len,
double  _dir,
int  _gen,
int  _col,
int  _width 
)
inline
42  { //constructor function for the Tree class
43  Pos = _pos;
44  tempPos=_pos;
45  Length = _len;
46  Dir = _dir;
47  c_index = _col;
48  Generation = _gen;
49  Str = _str;
50  w_index = _width;
51  for (Generation = _gen; Generation < MAX_GENERATIONS; Generation++){
52  Str.clear();
53  for (auto Char : _str){
54  if (isalpha(Char)){
55  if (Rule.find(Char) != Rule.end()) copy(Rule[Char].begin(), Rule[Char].end(), back_inserter(Str));
56  else Str.push_back(Char);
57  }
58  else{
59  Str.push_back(Char);
60  }
61  }
62  cout << Generation << ' ' << Str << endl;
63  _str = Str;
64  }
65  }
const double MAX_GENERATIONS
Definition: driver3.cpp:8
map< char, string > Rule
Definition: driver3.cpp:32

Member Function Documentation

void Tree::Process ( )
inline
66  { // main function to check the characters and draw the fractal structure recursively
67  if (Generation > MAX_GENERATIONS) return;
68  int i = 0, n = Str.size();
69  while (i < n){ //while i is not the last character
70  char Ch = Str[i]; // get the ith character of the string
71  switch(Ch){
72  case OPENBRACKET : { //get the longest substring with the starting and the closing brackets
73  int OpenBrackets = 1;
74  int j = i;
75  while (OpenBrackets){
76  i++;
77  if (Str[i] == CLOSEDBRACKET) OpenBrackets--;
78  else if (Str[i] == OPENBRACKET) OpenBrackets++;
79  }
80  Tree child = *(new Tree(Str.substr(j + 1, i-j-1), Pos, Length, Dir, Generation, c_index, w_index));
81  child.Process();
82  break;
83  }
84  case TURNLEFT : { //rotate the branch left by defined angle
85  Dir += ROTATE_ANGLE;
86  if (Dir >= 360) Dir -= 360; // if the angle increases beyond 360, subtract 360
87  break;
88  }
89  case TURNRIGHT : { //rotate the branch right by defined angle
90  Dir -= ROTATE_ANGLE;
91  if (Dir < 0) Dir += 360; // if angle decreases beyond 0, add 360
92  break;
93  }
94  case COLORCHANGE : {
95  if(Generation <= 3)
96  {
97  c_index = 2;
98  break;
99  }
100  else
101  {
102  c_index = Str[++i] - '0';
103  break;
104  }
105  }
106  case WIDTH : {
107  w_index = Str[++i] - '0';
108  }
109  case CIRCLE : {
110  if(Generation == MAX_GENERATIONS){
111  DU->setColor(DU->Colors[c_index].R, DU->Colors[c_index].G, DU->Colors[c_index].B);
112  DU->changePointSize(1);
113  DU->fillCircle(tempPos.X, tempPos.Y,1);
114  }
115  break;
116  }
117  default : {
118  Point NewPos = *(new Point(Pos.X + cos((M_PI / 180.0) * Dir) * Length, Pos.Y + sin((M_PI / 180.0) * Dir) * Length));
119  DU->setColor(DU->Colors[c_index].R, DU->Colors[c_index].G, DU->Colors[c_index].B);
120  DU->changePointSize(w_index);
121  DU->drawLine((int)Pos.X, (int)Pos.Y, (int)NewPos.X, (int)NewPos.Y);
122  Pos = NewPos;
123  break;
124  }
125  }
126  i++;
127  }
128  }
Tree(string _str, Point _pos, double _len, double _dir, int _gen, int _col, int _width)
Definition: driver3.cpp:42
double R
Definition: DrawUtil.cpp:9
double G
Definition: DrawUtil.cpp:10
#define COLORCHANGE
Definition: driver3.cpp:15
const double MAX_GENERATIONS
Definition: driver3.cpp:8
#define OPENBRACKET
Definition: driver3.cpp:13
RGB Colors[4]
Definition: DrawUtil.cpp:15
#define TURNLEFT
Definition: driver3.cpp:16
Definition: driver3.cpp:24
void Process()
Definition: driver3.cpp:66
void setColor(double r, double g, double b)
Definition: DrawUtil.cpp:317
DrawUtil * DU
Definition: driver3.cpp:23
void fillCircle(int x, int y, int r)
Definition: DrawUtil.cpp:284
double X
Definition: driver3.cpp:25
#define CLOSEDBRACKET
Definition: driver3.cpp:14
#define CIRCLE
Definition: driver3.cpp:19
Definition: driver3.cpp:33
const double ROTATE_ANGLE
Definition: driver3.cpp:9
double Y
Definition: driver3.cpp:26
double B
Definition: DrawUtil.cpp:11
#define WIDTH
Definition: driver3.cpp:20
#define TURNRIGHT
Definition: driver3.cpp:17
void drawLine(int x1, int y1, int x2, int y2)
Definition: DrawUtil.cpp:124
void changePointSize(int psize)
Definition: DrawUtil.cpp:322

The documentation for this class was generated from the following file: