-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathl_system.cpp
100 lines (82 loc) · 1.98 KB
/
l_system.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include "l_system.h"
#include <map>
std::string L_SYSTEM::get_genetic_code( const int iteration )
{
std::string initial_string;
if ( iteration == 0 )
{
return m_axiom;
}
else if ( iteration == 1 )
{
initial_string = m_axiom;
}
else
{
initial_string = get_genetic_code( iteration - 1 );
}
std::string new_string;
for ( auto c : initial_string )
{
if ( m_rules.find( c ) != m_rules.end() )
{
new_string += m_rules.at( c );
}
else
{
new_string += c;
}
}
return new_string;
}
void L_SYSTEM::create( const int iteration )
{
m_draw_lines.clear();
const auto& code = get_genetic_code( iteration );
MATH_VECTOR_2D starting_point{ 0.0f, 0.0f };
MATH_VECTOR_2D ending_point{ 0.0f, m_growth_rate };
std::vector< MATH_VECTOR_2D > saved_starting_states;
std::vector< MATH_VECTOR_2D > saved_ending_states;
for ( auto c : code )
{
if ( c == 'F' )
{
m_draw_lines.push_back( starting_point );
m_draw_lines.push_back( ending_point );
MATH_VECTOR_2D direction = ending_point - starting_point;
starting_point = ending_point;
ending_point += direction;
}
else if ( c == '+' )
{
ending_point.Translate( starting_point * -1.0F );
ending_point.RotateD( m_rotation_degree );
ending_point.Translate( starting_point );
}
else if ( c == '-' )
{
ending_point.Translate( starting_point * -1.0F );
ending_point.RotateD( -m_rotation_degree );
ending_point.Translate( starting_point );
}
else if ( c == '[' )
{
saved_starting_states.push_back( starting_point );
saved_ending_states.push_back( ending_point );
}
else if ( c == ']' )
{
starting_point = saved_starting_states[ saved_starting_states.size() - 1 ];
ending_point = saved_ending_states[ saved_ending_states.size() - 1 ];
saved_starting_states.pop_back();
saved_ending_states.pop_back();
}
}
}
void L_SYSTEM::draw()
{
for ( size_t i = 0; i < m_draw_lines.size(); i += 2 )
{
GRAPHICS_UTILITY::draw_line( m_draw_lines[ i ], m_draw_lines[ i + 1 ] );
}
}