-
Notifications
You must be signed in to change notification settings - Fork 1
/
sierpinskiTriangle.cpp
60 lines (50 loc) · 1.46 KB
/
sierpinskiTriangle.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
// sierpinski triangle
#include <string>
#include <iostream>
#include <fstream>
#include <cmath>
std::string triangleA(int n);
std::string triangleB(int n);
std::string drawTriangle(int n){
//attach different segments together to create full snowflake
return triangleA(n) + "+ " + triangleB(n) + "+ " + triangleB(n);
}
std::string triangleA(int n){
// base case
if (n == 0) {
return "F ";
}
// this is where we call the recursive method
else {
std::string A = triangleA(n-1);
std::string B = triangleB(n-1);
return A + "+ " + B + "- " + A + "- " + B + "+ " + A;
}
}
std::string triangleB(int n){
// base case
if (n == 0) {
return "F ";
}
// this is where we call the recursive method
else {
std::string A = triangleA(n-1);
std::string B = triangleB(n-1);
//triangleA already concatenates strings A and B //just concatenate B twice to get both sides
return B + B;
}
}
void save(std::string filename, std::string Lsystem){
//write commands to file
std::ofstream file;
file.open(filename);
file << Lsystem;
file.close();
}
int main(int argc, char **argv){
//amount of times it goes
int n = std::stoi(argv[1]);
std::string Lsystem = drawTriangle(n);
// save("triangle" + std::to_string(n) + ".txt", Lsystem);
save("l-system.txt", Lsystem);
}