-
Notifications
You must be signed in to change notification settings - Fork 9
/
FractalExplorer.java
315 lines (212 loc) · 7.35 KB
/
FractalExplorer.java
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/**
* A Fractal Explorer created in Java
* @author William Fiset, [email protected]
**/
import java.util.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.swing.*;
public class FractalExplorer extends JFrame {
static final int WIDTH = 600;
static final int HEIGHT = 600;
Canvas canvas;
BufferedImage fractalImage;
static final int MAX_ITER = 200;
static final double DEFAULT_ZOOM = 100.0;
static final double DEFAULT_TOP_LEFT_X = -3.0;
static final double DEFAULT_TOP_LEFT_Y = +3.0;
double zoomFactor = DEFAULT_ZOOM;
double topLeftX = DEFAULT_TOP_LEFT_X;
double topLeftY = DEFAULT_TOP_LEFT_Y;
// -------------------------------------------------------------------
public FractalExplorer() {
setInitialGUIProperties();
addCanvas();
canvas.addKeyStrokeEvents();
updateFractal();
this.setVisible(true);
}
// -------------------------------------------------------------------
public static void main(String[] args) {
new FractalExplorer();
}
// -------------------------------------------------------------------
private void addCanvas() {
canvas = new Canvas();
fractalImage = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
canvas.setVisible(true);
this.add(canvas, BorderLayout.CENTER);
} // addCanvas
// -------------------------------------------------------------------
private void setInitialGUIProperties() {
this.setTitle("Fractal Explorer");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(WIDTH, HEIGHT);
this.setResizable(false);
this.setLocationRelativeTo(null);
} // setInitialGUIProperties
// -------------------------------------------------------------------
private double getXPos(double x) {
return x/zoomFactor + topLeftX;
} // getXPos
// -------------------------------------------------------------------
private double getYPos(double y) {
return y/zoomFactor - topLeftY;
} // getYPos
// -------------------------------------------------------------------
/**
* Updates the fractal by computing the number of iterations
* for each point in the fractal and changing the color
* based on that.
**/
public void updateFractal() {
for (int x = 0; x < WIDTH; x++ ) {
for (int y = 0; y < HEIGHT; y++ ) {
double c_r = getXPos(x);
double c_i = getYPos(y);
int iterCount = computeIterations(c_r, c_i);
int pixelColor = makeColor(iterCount);
fractalImage.setRGB(x, y, pixelColor);
}
}
canvas.repaint();
} // updateFractal
// -------------------------------------------------------------------
/** Returns a posterized color based off of the iteration count
of a given point in the fractal **/
private int makeColor( int iterCount ) {
int color = 0b011011100001100101101000;
int mask = 0b000000000000010101110111;
int shiftMag = iterCount / 13;
if (iterCount == MAX_ITER)
return Color.BLACK.getRGB();
return color | (mask << shiftMag);
} // makeColor
// -------------------------------------------------------------------
private int computeIterations(double c_r, double c_i) {
/*
Let c = c_r + c_i
Let z = z_r + z_i
z' = z*z + c
= (z_r + z_i)(z_r + z_i) + (c_r + c_i)
= z_r² + 2*z_r*z_i - z_i² + c_r + c_i
z_r' = z_r² - z_i² + c_r
z_i' = 2*z_i*z_r + c_i
*/
double z_r = 0.0;
double z_i = 0.0;
int iterCount = 0;
// Modulus (distance) formula:
// √(a² + b²) <= 2.0
// a² + b² <= 4.0
while ( z_r*z_r + z_i*z_i <= 4.0 ) {
double z_r_tmp = z_r;
z_r = z_r*z_r - z_i*z_i + c_r;
z_i = 2*z_i*z_r_tmp + c_i;
// Point was inside the Mandelbrot set
if (iterCount >= MAX_ITER)
return MAX_ITER;
iterCount++;
}
// Complex point was outside Mandelbrot set
return iterCount;
} // computeIterations
// -------------------------------------------------------------------
private void moveUp() {
double curHeight = HEIGHT / zoomFactor;
topLeftY += curHeight / 6;
updateFractal();
} // moveUp
// -------------------------------------------------------------------
private void moveDown() {
double curHeight = HEIGHT / zoomFactor;
topLeftY -= curHeight / 6;
updateFractal();
} // moveDown
// -------------------------------------------------------------------
private void moveLeft() {
double curWidth = WIDTH / zoomFactor;
topLeftX -= curWidth / 6;
updateFractal();
} // moveLeft
// -------------------------------------------------------------------
private void moveRight() {
double curWidth = WIDTH / zoomFactor;
topLeftX += curWidth / 6;
updateFractal();
} // moveRight
// -------------------------------------------------------------------
private void adjustZoom( double newX, double newY, double newZoomFactor ) {
topLeftX += newX/zoomFactor;
topLeftY -= newY/zoomFactor;
zoomFactor = newZoomFactor;
topLeftX -= ( WIDTH/2) / zoomFactor;
topLeftY += (HEIGHT/2) / zoomFactor;
updateFractal();
} // adjustZoom
// -------------------------------------------------------------------
private class Canvas extends JPanel implements MouseListener {
public Canvas() {
addMouseListener(this);
}
@Override public Dimension getPreferredSize() {
return new Dimension(WIDTH, HEIGHT);
} // getPreferredSize
@Override public void paintComponent(Graphics drawingObj) {
drawingObj.drawImage( fractalImage, 0, 0, null );
} // paintComponent
@Override public void mousePressed(MouseEvent mouse) {
double x = (double) mouse.getX();
double y = (double) mouse.getY();
switch( mouse.getButton() ) {
// Left
case MouseEvent.BUTTON1:
adjustZoom( x, y, zoomFactor*2 );
break;
// Right
case MouseEvent.BUTTON3:
adjustZoom( x, y, zoomFactor/2 );
break;
}
} // mousePressed
public void addKeyStrokeEvents() {
KeyStroke wKey = KeyStroke.getKeyStroke(KeyEvent.VK_W, 0 );
KeyStroke aKey = KeyStroke.getKeyStroke(KeyEvent.VK_A, 0 );
KeyStroke sKey = KeyStroke.getKeyStroke(KeyEvent.VK_S, 0 );
KeyStroke dKey = KeyStroke.getKeyStroke(KeyEvent.VK_D, 0 );
Action wPressed = new AbstractAction() {
@Override public void actionPerformed(ActionEvent e) {
moveUp();
}
};
Action aPressed = new AbstractAction() {
@Override public void actionPerformed(ActionEvent e) {
moveLeft();
}
};
Action sPressed = new AbstractAction() {
@Override public void actionPerformed(ActionEvent e) {
moveDown();
}
};
Action dPressed = new AbstractAction() {
@Override public void actionPerformed(ActionEvent e) {
moveRight();
}
};
this.getInputMap().put( wKey, "w_key" );
this.getInputMap().put( aKey, "a_key" );
this.getInputMap().put( sKey, "s_key" );
this.getInputMap().put( dKey, "d_key" );
this.getActionMap().put( "w_key", wPressed );
this.getActionMap().put( "a_key", aPressed );
this.getActionMap().put( "s_key", sPressed );
this.getActionMap().put( "d_key", dPressed );
} // addKeyStrokeEvents
@Override public void mouseReleased(MouseEvent mouse){ }
@Override public void mouseClicked(MouseEvent mouse) { }
@Override public void mouseEntered(MouseEvent mouse) { }
@Override public void mouseExited (MouseEvent mouse) { }
} // Canvas
} // FractalExplorer