forked from Michal-MK/TurtleGraphics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLSystem.cs
More file actions
46 lines (38 loc) · 935 Bytes
/
Copy pathLSystem.cs
File metadata and controls
46 lines (38 loc) · 935 Bytes
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
using System.Collections.Generic;
using System.Text;
namespace TurtleGraphicsCode {
public class LSystem {
public string Sentence { get; private set; }
public IRule Setup { get; }
public LSystem(IRule setup, int generations) {
Setup = setup;
Sentence = Setup.Axiom;
for (int i = 0; i < generations; i++) {
Sentence = Generate();
}
}
public string Generate() {
StringBuilder newSentence = new StringBuilder();
foreach (char c in Sentence) {
if (Setup.Rules.ContainsKey(c)) {
newSentence.Append(Setup.Rules[c]);
}
else {
newSentence.Append(c);
}
}
Sentence = newSentence.ToString();
return Sentence;
}
public Turtle Draw(bool fullScreen) {
Turtle t = Setup.Turtle ?? new Turtle();
t.FullScreen = fullScreen;
foreach (char c in Sentence) {
if (Setup.Actions.ContainsKey(c)) {
Setup.Actions[c].Invoke(t);
}
}
return t;
}
}
}