-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fractal.cpp
170 lines (151 loc) · 4.51 KB
/
Fractal.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "Fractal.h"
#include <cmath>
//-------- class Fractal --------//
/**
* @brief sets the abstract fractal to hold the dimensions it must have, called only from one of
* it's sons
* @param dims the dimensions of the fractal
*/
Fractal::Fractal(int dims) : dimensions(dims)
{
}
/**
* @brief this resets the vector to the wanted size, with the value of not printing anything
* in each cell
* @param size the size of the wanted vector (will be set to size*size)
*/
void Fractal::resetVector(int size)
{
f.resize(size);
for (int i = 0; i < size; i++)
{
f[i].resize(size);
for (int j = 0; j < size; j++)
{
f[i][j] = NOT_DROW;
}
}
}
/**
* @brief sets the vector to hold the whole fractal, recursively
* @param rowIdx the row index to start filling at
* @param colIdx the column index to start filling at
* @param thisSize the root size of the current fractal's smallest size (when dim=1)
* @param dims the dimension of the wanted fractal
* @param places the points that should be filled (at the dimension of 1, for other
* dimensions must be multiplied)
* @param pointNum the number of points in @param places
*/
void Fractal::setVector(int rowIdx, int colIdx, int thisSize, int dims, const Point *places, int
pointNum)
{
if (dims == 0)
{
f[rowIdx][colIdx] = TO_DROW;
}
else
{
dims = dims - 1;
int diff_idx = std::pow(thisSize, dims);
for (int i = 0; i < pointNum; i++)
{
Point point = places[i];
setVector(rowIdx + diff_idx * point.row, colIdx + diff_idx * point.col, thisSize,
dims, places, pointNum);
}
}
}
/**
* @brief prints the fractal
* @param size the root size of the fractal
*/
void Fractal::toPrint(int size) const
{
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
std::cout << f[i][j];
}
std::cout << std::endl;
}
std::cout << std::endl;
}
//-------- class SierpinskiCarpet --------//
/**
* @brief the constructor of this class, which resets the dimensions and vector as given from
* the user
* @param dims the dimensions of the fractal
*/
SierpinskiCarpet::SierpinskiCarpet(int dims) : Fractal(dims)
{
resetVector(std::pow(CARPET_SIZE, dimensions));
}
/**
* @brief this sets the vector to hold the whole fractal, and prints it (using other methods)
*/
void SierpinskiCarpet::makeAndPrint()
{
setVector(0, 0, CARPET_SIZE, dimensions, _points, CARPET_NUM_POINTS);
toPrint(std::pow(CARPET_SIZE, dimensions));
}
//-------- class SierpinskiTriangle --------//
/**
* @brief the constructor of this class, which resets the dimensions and vector as given from
* the user
* @param dims the dimensions of the fractal
*/
SierpinskiTriangle::SierpinskiTriangle(int dims) : Fractal(dims)
{
resetVector(std::pow(TRIANGLE_SIZE, dimensions));
}
/**
* @brief this sets the vector to hold the whole fractal, and prints it (using other methods)
*/
void SierpinskiTriangle::makeAndPrint()
{
setVector(0, 0, TRIANGLE_SIZE, dimensions, _points, TRIANGLE_NUM_POINTS);
toPrint(std::pow(TRIANGLE_SIZE, dimensions));
}
//-------- class VicsekFractal --------//
/**
* @brief the constructor of this class, which resets the dimensions and vector as given from
* the user
* @param dims the dimensions of the fractal
*/
VicsekFractal::VicsekFractal(int dims) : Fractal(dims)
{
resetVector(std::pow(VICSEK_SIZE, dimensions));
}
/**
* @brief this sets the vector to hold the whole fractal, and prints it (using other methods)
*/
void VicsekFractal::makeAndPrint()
{
setVector(0, 0, VICSEK_SIZE, dimensions, _points, VICSEK_NUM_POINTS);
toPrint(std::pow(VICSEK_SIZE, dimensions));
}
//-------- class FractalFactory --------//
/**
* @brief the static method that makes the fractal from the given index and dimension
* @param index the index of the wanted fractal, should be from 1 to 3
* @param dims the dimensions of the wanted fractal, should be from 1 to 6
* @note there is memory allocated in this method that should be freed!
*/
Fractal *FractalFactory::makeFractal(int index, int dims)
{
if (index == CARPET_INDEX)
{
return new SierpinskiCarpet(dims);
}
if (index == TRIANGLE_INDEX)
{
return new SierpinskiTriangle(dims);
}
if (index == VICSEK_INDEX)
{
return new VicsekFractal(dims);
}
// this won't happen because we validate the information before we send it here
exit(EXIT_FAILURE);
}