-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3d_fractals.cpp
More file actions
63 lines (55 loc) · 2.36 KB
/
Copy path3d_fractals.cpp
File metadata and controls
63 lines (55 loc) · 2.36 KB
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
//
// Created by kobedb on 20.05.22.
//
#include "3d_fractals.h"
#include "kdb_render_utils.h"
#include "3d_figures_drawing_engine.h"
namespace KDBRenderUtils {
void generateFractal(const Figure& inFig, Figure& fractal, int nr_iterations, double scale)
{
if(nr_iterations <= 0) {
fractal.addFigure(inFig);
fractal.color = inFig.color;
return;
}
for(int i = 0; i < inFig.vertices.size(); i++) {
Figure scaled = inFig;
applyTransformation(scaled, scaling(1.0f/scale));
applyTransformation(scaled, translation( inFig.vertices[i] - scaled.vertices[i] ));
generateFractal(scaled, fractal, nr_iterations-1, scale);
}
}
void generateMengerSponge(const Vector3D& inLowerLeft , double inSideLength, Figure& mengerSponge, int nr_iterations)
{
if(nr_iterations <= 0) {
Figure theSubCube = createCube(magenta);
applyTransformation(theSubCube, scaling(inSideLength/2.0)); // scale with theSubCube's side length divided by side length of cube created by createCube()
applyTransformation(theSubCube, translation( inLowerLeft - theSubCube.vertices[0] ));
mengerSponge.addFigure(theSubCube);
return;
}
for(int depth = 0; depth < 3; depth++) {
for(int height = 0; height < 3; height++) {
for(int width = 0; width < 3; width++) {
if(
(width == 1 && height == 1) ||
(depth == 1 && height == 1) ||
(width == 1 && depth == 1)
)
continue;
//calculate cube corner coordinates of subCube
double subCubeSideLength = inSideLength / 3.0;
Vector3D subCubeLowerLeft = inLowerLeft + subCubeSideLength * Vector3D::vector(depth, width, height);
generateMengerSponge(subCubeLowerLeft, subCubeSideLength, mengerSponge, nr_iterations - 1);
}
}
}
}
Figure createMengerSponge(int nr_iterations, const NormColor& color )
{
Figure mengerSponge;
generateMengerSponge(createCube(color).vertices[0], 2, mengerSponge, nr_iterations);
mengerSponge.color = color;
return mengerSponge;
}
};