-
Notifications
You must be signed in to change notification settings - Fork 0
/
drawing.cxx
114 lines (95 loc) · 3.65 KB
/
drawing.cxx
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
#include <complex>
#include <iostream>
#include "drawing.hxx"
#include "fractalMath.hxx"
#include "color.hxx"
double escript::Painter::getStartingVerticalPosition() {
double position = -1.0 * complexHalfHeight;
position *= getZoomFactor();
position += verticalOffset;
return position;
}
double escript::Painter::getStartingHorizontalPosition() {
double position = -1.0 * complexHalfWidth;
position *= getZoomFactor();
position += horizontalOffset;
return position;
}
double escript::Painter::getZoomFactor() {
return 1.0 / zoomLevel;
}
escript::Painter::Painter(escript::Window& targetWindow) : targetWindow(targetWindow){
complexHalfHeight = 1.1;
zoomLevel = 1.0;
panValue = 0.25;
horizontalOffset = 0;
verticalOffset = 0;
complexHalfWidth = complexHalfHeight * targetWindow.getDrawingWidth() / targetWindow.getDrawingHeight();
}
std::vector<std::vector<escript::Color>> escript::Painter::getMandelbrotSetPixels() {
const auto rowCount = targetWindow.getDrawingHeight();
const auto columnCount = targetWindow.getDrawingWidth();
const double zoomFactor = 1.0 / zoomLevel;
const double horizontalStep = 2.0 * complexHalfWidth * zoomFactor / columnCount;
const double verticalStep = 2.0 * complexHalfHeight * zoomFactor / rowCount;
double horizontalPosition = getStartingHorizontalPosition();
double verticalPosition = getStartingVerticalPosition();
const Color catppuccinPink(0xf5, 0xbd, 0xe6);
const Color catppuccinBase(0x1e, 0x20, 0x30);
// std::cout << horizontalPosition << " " << verticalPosition <<
// " " << horizontalStep << " " << verticalStep << std::endl;
using rowT = std::vector<Color>;
std::vector<rowT> pixels(rowCount, rowT(columnCount, Color(0x00, 0x00, 0x00)));
for(rowT& row : pixels) {
for(Color& pixel : row) {
auto currentNumber = std::complex<double>(horizontalPosition, verticalPosition);
int maxIter = 50;
int escapeTime = Mandelbrot::iterationsUntilEscape(currentNumber, maxIter);
// double value = -1.0 * std::exp(-escapeTime / maxIter) * 0xFF;
double value = 1.0 * escapeTime / maxIter;
pixel = Color::interpolate(catppuccinBase, catppuccinPink, value);
//pixel = Color(0xFF, 0xAA, value);
horizontalPosition += horizontalStep;
}
horizontalPosition = getStartingHorizontalPosition();
verticalPosition += verticalStep;
}
return pixels;
}
void escript::Painter::drawPixels(std::vector<std::vector<escript::Color>> pixels) {
const auto pink = Color(0x55, 0x55, 0x55);
const auto height = targetWindow.getDrawingHeight();
const auto width = targetWindow.getDrawingWidth();
targetWindow.clearScreen(pink);
for(int i = 0; i < height; ++i) {
for(int j = 0; j < width; ++j) {
Color color = pixels[i][j];
targetWindow.drawPixel(color, j, height - i);
}
}
targetWindow.presentBuffer();
}
void escript::Painter::zoomIn() {
zoomLevel += 1;
}
void escript::Painter::zoomOut() {
if(zoomLevel >= 2){
zoomLevel -= 1;
}
}
void escript::Painter::panRight() {
double change = complexHalfWidth * panValue * (1/zoomLevel);
horizontalOffset += change;
}
void escript::Painter::panLeft() {
double change = complexHalfWidth * panValue * (1/zoomLevel);
horizontalOffset -= change;
}
void escript::Painter::panUp() {
double change = complexHalfHeight * panValue * (1/zoomLevel);
verticalOffset += change;
}
void escript::Painter::panDown() {
double change = complexHalfHeight * panValue * (1/zoomLevel);
verticalOffset -= change;
}