-
Notifications
You must be signed in to change notification settings - Fork 0
/
FractalViewer.java
275 lines (233 loc) · 8.36 KB
/
FractalViewer.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
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
/**
* Displays a fractal on it's canvas. Has a runtime adjustable width and height.
* Pixel size can only be selected at compile time/when programming. Includes an
* indicator showing where the next potential zoom would be. Is continuously
* doing all the calculations for which points are part of the fractal or not.
*
* Different from FractalViewer2 in that it supports fractals like the
* Mandelbrot Set, which requires doing different sets of calculations depending
* on your starting position.
*
* @author Samuel Lieberman
*
*/
public class FractalViewer extends AbstractFractalViewer{
private static final long serialVersionUID = 160672894922481585L;
private static final int START_WIDTH = 400;
private static final int START_HEIGHT = 400;
private static final int PIXEL_SIZE = 2;
private static final double ZOOM_FACTOR = 2;
private static final double INDICATOR_THICKNESS = 5;
private static final Color INDICATOR_COLOR = new Color(255, 255, 255);
private static final Color CONVERGE_COLOR = new Color(0, 0, 0);
//private static final Color ERROR_COLOR = new Color(255, 0, 0);
private static final Color[] COLOR_PATTERN = {
new Color(255, 0, 0),
new Color(255, 255, 0),
new Color(0, 255, 127),
new Color(0, 127, 127),
new Color(0, 0, 255),
new Color(127, 0, 127),
};
private static final int ITERATIONS_PER_COLOR = 2;
private RecursiveFractal fractal;
private double fracDiameter;
private double fracOverComp;
//private double compOverFrac;
private double fracWidth;
private double fracHeight;
private Complex fracCenter;
private Complex fracTopLeft;
//private Complex fracBottomRight;
private Thread drawThread = null;
private Repainter repainter = null;
private Complex[][] pixelPositions;
private Complex[][] pixelValues;
private boolean[][] pixelConverges;
private Color[][] pixelColors;
private boolean mouseIsIn;
private int mouseX;
private int mouseY;
private int iterations;
private Color currentColor;
public FractalViewer() {
mouseIsIn = false;
addMouseMotionListener(new MotionDetector());
addMouseListener(new ZoomDetector());
addComponentListener(new ResizeDetector());
setPreferredSize(new Dimension(START_WIDTH, START_HEIGHT));
}
public void start(RecursiveFractal fractal) {
this.fractal = fractal;
initForPosition(fractal.getInitialScreenDiameter(), fractal.getInitialScreenCenter());
}
private void initForPosition(double fracDiameter, Complex fracCenter) {
if (drawThread != null && repainter != null) {
repainter.end();
try {
drawThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
setPosition(fracDiameter, fracCenter);
iterations = 0;
initPixelValues();
repainter = new Repainter();
drawThread = new Thread(repainter);
drawThread.start();
}
private void setPosition(double fracDiameter, Complex fracCenter) {
int compWidth = getWidth();
int compHeight = getHeight();
//int compCenterX = compWidth/2;//rounded down to the nearest pixel
//int compCenterY = compHeight/2;//rounded down to the nearest pixel
int compDiameter;
if (compWidth < compHeight) {
compDiameter = compWidth;
}else {
compDiameter = compHeight;
}
this.fracDiameter = fracDiameter;
fracOverComp = fracDiameter/compDiameter;
//compOverFrac = compDiameter/fracDiameter;
fracWidth = compWidth*fracOverComp;
fracHeight = compHeight*fracOverComp;
this.fracCenter = fracCenter;
fracTopLeft = new Complex(fracCenter.re() - fracWidth/2, fracCenter.im() - fracHeight/2, ImMath.coordinateSystem.CARTISAN);
//fracBottomRight = new Complex(fracCenter.re() + fracWidth/2, fracCenter.im() + fracHeight/2, ImMath.coordinateSystem.CARTISAN);
}
private Complex compPosToFracPos(int compX, int compY) {
return new Complex(fracTopLeft.re() + compX*fracOverComp, fracTopLeft.im() + compY*fracOverComp, ImMath.coordinateSystem.CARTISAN);
}
private void initPixelValues() {
int pixelsWidth = (int) Math.ceil(getWidth()/(double)(PIXEL_SIZE));
int pixelsHeight = (int) Math.ceil(getHeight()/(double)(PIXEL_SIZE));
pixelPositions = new Complex[pixelsWidth][pixelsHeight];
pixelValues = new Complex[pixelsWidth][pixelsHeight];
pixelConverges = new boolean[pixelsWidth][pixelsHeight];
pixelColors = new Color[pixelsWidth][pixelsHeight];
for (int pixX = 0; pixX < pixelsWidth; pixX++) {
for (int pixY = 0; pixY < pixelsHeight; pixY++) {
int compX = pixX*PIXEL_SIZE;
int compY = pixY*PIXEL_SIZE;
Complex positionValue = new Complex(fracTopLeft.re() + compX*fracOverComp, fracTopLeft.im() + compY*fracOverComp);
pixelPositions[pixX][pixY] = positionValue;
pixelValues[pixX][pixY] = fractal.start(positionValue);
pixelConverges[pixX][pixY] = true;
pixelColors[pixX][pixY] = CONVERGE_COLOR;
}
}
}
private void incrementPixelValues() {
for (int x = 0; x < pixelValues.length; x++) {
for (int y = 0; y < pixelValues[x].length; y++) {
if (pixelConverges[x][y]) {
Complex oldValue = pixelValues[x][y];
Complex newValue = fractal.step(oldValue, pixelPositions[x][y]);
pixelValues[x][y] = newValue;
if (fractal.diverges(newValue, iterations)) {
pixelConverges[x][y] = false;
pixelColors[x][y] = currentColor;
}
}
}
}
}
private void incrementColor() {
//cycles through each of the colors switching every 10 iterations
currentColor = COLOR_PATTERN[(iterations/ITERATIONS_PER_COLOR)%COLOR_PATTERN.length];
}
protected void incrementIterations() {
iterations++;
incrementColor();
incrementPixelValues();
}
@Override
protected void paintComponent(Graphics g) {
for (int pixX = 0; pixX < pixelColors.length; pixX++) {
for (int pixY = 0; pixY < pixelColors[pixX].length; pixY++) {
int compX = pixX*PIXEL_SIZE;
int compY = pixY*PIXEL_SIZE;
g.setColor(pixelColors[pixX][pixY]);
g.fillRect(compX, compY, PIXEL_SIZE, PIXEL_SIZE);//draws a point
}
}
if (mouseIsIn) {
int compWidth = getWidth();
int compHeight = getHeight();
int indicatorWidth = (int) (compWidth/ZOOM_FACTOR);
int indicatorHeight = (int) (compHeight/ZOOM_FACTOR);
g.setColor(INDICATOR_COLOR);
for (int i = 0; i < INDICATOR_THICKNESS; i++) {
g.drawRect(mouseX-indicatorWidth/2-i, mouseY-indicatorHeight/2-i, indicatorWidth+2*i, indicatorHeight+2*i);
}
}
}
private class MotionDetector implements MouseMotionListener {
@Override
public void mouseDragged(MouseEvent arg0) {}
@Override
public void mouseMoved(MouseEvent arg0) {
mouseX = arg0.getX();
mouseY = arg0.getY();
repaint();
}
}
private class ZoomDetector implements MouseListener {
@Override
public void mouseClicked(MouseEvent arg0) {
Complex newCenter = compPosToFracPos(mouseX, mouseY);
double newFracDiameter;
if (arg0.isControlDown()) {
newFracDiameter = fracDiameter*ZOOM_FACTOR;
}else {
newFracDiameter = fracDiameter/ZOOM_FACTOR;
}
initForPosition(newFracDiameter, newCenter);
}
@Override
public void mouseEntered(MouseEvent arg0) {
mouseIsIn = true;
}
@Override
public void mouseExited(MouseEvent arg0) {
mouseIsIn = false;
repaint();
}
@Override public void mousePressed(MouseEvent arg0) {}
@Override public void mouseReleased(MouseEvent arg0) {}
}
private class ResizeDetector implements ComponentListener {
@Override public void componentHidden(ComponentEvent arg0) {}
@Override public void componentMoved(ComponentEvent arg0) {}
@Override
public void componentResized(ComponentEvent arg0) {
if (fracCenter != null) {
initForPosition(fracDiameter, fracCenter);
}
}
@Override public void componentShown(ComponentEvent arg0) {}
}
private class Repainter implements Runnable{
private boolean end = false;
public void end() {
end = true;
}
@Override
public void run() {
while (!end) {
incrementIterations();
repaint();
}
}
}
}