-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMandelbrot_Visualizer.pde
153 lines (132 loc) · 3.68 KB
/
Mandelbrot_Visualizer.pde
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
import interfascia.*;
//Main settings
int height = 500;
int width = 600;
int yMargin = 50;
//GUI Settings
GUIController controller;
IFButton resetButton;
boolean mouseOver = true;
PFont font, titleFont;
//Calculation Settings
double EPSILON = 1e-12;
double xStart = -3;
double xEnd = 1.5;
double yStart = -1.5;
double yEnd = 1.5;
int maxIterations = 2000;
int INFINITY = 18;
float muLimit = 1;
//Zoom rectangle settings
float xProportion = 0.5;
float yProportion = 0.5;
boolean calculateFractal = true;
void setup()
{
size(600, 500);
controller = new GUIController(this);
resetButton = new IFButton("Reset", width - 400, 10, 40);
resetButton.addActionListener(this);
controller.add(resetButton);
font = createFont("Arial", 12);
titleFont = createFont("Arial", 20);
textFont(font);
pixelDensity(1);
loadPixels();
}
void draw()
{
if (calculateFractal)
{
for (int x = 0; x < width; x++)
{
for (int y = yMargin; y < height; y++)
{
double a = map(x, 0, width, xStart, xEnd);
double b = map(y, 0 + yMargin, height, yStart, yEnd);
double cReal = a;
double cIm = b;
int iteration = 0;
while (iteration < maxIterations)
{
double zSquaredReal = (a*a) - (b*b);
double zSquaredIm = 2 * a * b;
a = zSquaredReal + cReal;
b = zSquaredIm + cIm;
double modulus = Math.sqrt((a*a) + (b*b));
if (modulus > INFINITY)
break;
iteration++;
}
colorMode(HSB, 360, 100, 100);
double modulus = Math.sqrt((a*a) + (b*b));
double mu = iteration + 1 - (Math.log(Math.log(modulus)))/log(2.0);
if (Double.isNaN(mu)) {
mu = 0;
}
float hue = (float) map(mu, 0, muLimit, 0, 5);
float saturation = 100;
float lightness = 100;
if (iteration == maxIterations)
lightness = 0;
int pixelIndex = (y*width) + x;
pixels[pixelIndex] = color(hue, saturation, lightness);
}
}
calculateFractal = false;
}
updatePixels();
fill(0);
textFont(font);
text("Real Limits: " + String.format("%.2f", xStart) + " - " + String.format("%.2f", xEnd), 10, 15);
text("Imaginary Limits: " + String.format("%.2f", yStart) + " - " + String.format("%.2f", yEnd), 10, 27);
textFont(titleFont);
text("MANDELBROT EXPLORER", width - 300, 20);
rectMode(CENTER);
stroke(0);
fill(0, 0, 0, 0);
if (focused && mouseOver)
{
rect(mouseX, mouseY, width*xProportion, height*yProportion);
}
}
public void mouseExited() {
mouseOver= false;
}
public void mouseEntered() {
mouseOver= true;
}
void zoom(int x, int y)
{
xStart = map(x - ((width*xProportion)/2.0), 0, width, xStart, xEnd);
xEnd = map(x + ((width*xProportion)/2.0), 0, width, xStart, xEnd);
yStart = map(y - ((height*yProportion)/2.0), 0, height, yStart, yEnd);
yEnd = map(y + ((height*yProportion)/2.0), 0, height, yStart, yEnd);
calculateFractal = true;
}
void mousePressed()
{
zoom(mouseX, mouseY);
}
void actionPerformed(GUIEvent e)
{
if (e.getSource() == resetButton)
{
xStart = -3;
xEnd = 1.5;
yStart = -1.5;
yEnd = 1.5;
calculateFractal = true;
}
}
double map(double valueCoord1,
double startCoord1, double endCoord1,
double startCoord2, double endCoord2)
{
if (Math.abs(endCoord1 - startCoord1) < EPSILON) {
throw new ArithmeticException("/ 0");
}
double offset = startCoord2;
double ratio = (endCoord2 - startCoord2) / (endCoord1 - startCoord1);
return ratio * (valueCoord1 - startCoord1) + offset;
}