-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJuliaSetGenerator.java
248 lines (217 loc) · 8.77 KB
/
JuliaSetGenerator.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
/**
JuliaSetGenerator.java
Milestone 1 demo solution with DisplayWindow class
Requires prior implementation of:
-ComplexNumber.java constructors, accessors
-DisplayWindow.java constructor, map, mapX, mapY
*/
import java.awt.Color;
public class JuliaSetGenerator{
public static void main(String args[]){
int x = 800;
int y = 350;
//for(int i = 0; i < args.length; i++){
// System.out.println(args[i]);
//}
if(args.length > 0){
try {
x = Integer.parseInt(args[0]);
y = Integer.parseInt(args[1]);
}
catch (NumberFormatException e) {
x = 800;
y = 350;
}
}
StdDraw.setCanvasSize(x,y);
StdDraw.setXscale(0,x);
StdDraw.setYscale(0,y);
StdDraw.enableDoubleBuffering();
DisplayWindow leftWin= new DisplayWindow(0,50,x/2,y-100);
DisplayWindow rightWin = new DisplayWindow(x/2,50,x/2,y-100);
boolean julia = false;
ComplexNumber JC = new ComplexNumber();
leftWin.println();
drawMandelbrot(leftWin);
// rightWin.println();
while(true){
StdDraw.setPenColor(StdDraw.BLACK);
// StdDraw.line(200,0,200,300);
StdDraw.setPenColor(StdDraw.BLACK);
if((StdDraw.mouseX() < 400) && (StdDraw.mouseY() > 50)){
ComplexNumber lP = new ComplexNumber(leftWin.mapA(StdDraw.mouseX()), leftWin.mapB(StdDraw.mouseY()));
// StdDraw.text(StdDraw.mouseX()+50, StdDraw.mouseY()-50, mP.toStringR5());
// System.out.println(lP.toString());
if(StdDraw.mousePressed()){
System.out.println("LEFT RECENTER");
// leftWin.println();
leftWin.recenter(lP);
// leftWin.println();
JC = lP; // used as a save for the complex number clicked on
julia = true; //so the code knows when to initially draw the Julia set
drawMandelbrot(leftWin);
}
} else if((StdDraw.mouseX() > 400) && (StdDraw.mouseY() > 50)){
ComplexNumber rP = new ComplexNumber(rightWin.mapA(StdDraw.mouseX()), rightWin.mapB(StdDraw.mouseY()));
if(StdDraw.mousePressed()){
System.out.println("RIGHT RECENTER");
rightWin.recenter(rP);
drawJulia(rightWin, JC);
}
}//end click else if statement
if(StdDraw.hasNextKeyTyped() == true){
if(StdDraw.isKeyPressed(83)){ // 83 == S
System.out.println("Saved");
StdDraw.save("mandelbrotSave.png");
}
//-----------right Window---------
// for O; 79 = O
if(StdDraw.isKeyPressed(79) && StdDraw.isKeyPressed(16)){
System.out.println("O is pressed (o + SHIFT) ");
rightWin.zoomOut(0.1);
// rightWin.zoomOut();
// StdDraw.clear();
drawJulia(rightWin, JC);
} else if(StdDraw.isKeyPressed(73) && StdDraw.isKeyPressed(16)){
// for I 73 = I
System.out.println("I is pressed (i + shift)");
rightWin.zoomIn(0.1);
// rightWin.zoomIn();
drawJulia(rightWin, JC);
} else if(StdDraw.isKeyPressed(82) && StdDraw.isKeyPressed(16)){
// for R 82 = R
System.out.println("R is pressed (r + SHIFT) ");
rightWin.resetView();
drawJulia(rightWin, JC);
//----------Arrow Key Movement-----------
}else if(StdDraw.isKeyPressed(37) && StdDraw.isKeyPressed(16)){
//Left Arrow key
System.out.println("Left Arrow and Shift are pressed");
rightWin.leftShift();
drawJulia(rightWin, JC);
} else if(StdDraw.isKeyPressed(38) && StdDraw.isKeyPressed(16)){
//Up Arrow key
System.out.println("Up Arrow and Shift are pressed");
rightWin.upShift();
drawJulia(rightWin, JC);
} else if(StdDraw.isKeyPressed(39) && StdDraw.isKeyPressed(16)){
//Right Arrow key
System.out.println("Right Arrow and Shift are pressed");
rightWin.rightShift();
drawJulia(rightWin, JC);
} else if(StdDraw.isKeyPressed(40) && StdDraw.isKeyPressed(16)){
//Down Arrow key
System.out.println("Down Arrow and Shift are pressed");
rightWin.downShift();
drawJulia(rightWin, JC);
// ------------Left Window stuff -------------
} else if(StdDraw.isKeyPressed(79)){
//for O; 79 = O
leftWin.zoomOut(0.1);
// leftWin.zoomOut();
System.out.println("o is pressed ");
drawMandelbrot(leftWin);
} else if(StdDraw.isKeyPressed(73)){
// for I 73 I
System.out.println("i is pressed ");
leftWin.zoomIn(0.1);
// leftWin.zoomIn();
drawMandelbrot(leftWin);
} else if(StdDraw.isKeyPressed(82)){
// for R 82 = R
System.out.println("r is pressed ");
leftWin.resetView();
drawMandelbrot(leftWin);
} else if(StdDraw.isKeyPressed(37)){
//Arrow Key Movement
//Left Arrow key System.out.println("Left Arrow is pressed");
leftWin.leftShift();
drawMandelbrot(leftWin);
} else if(StdDraw.isKeyPressed(38)){
//Up Arrow key
System.out.println("Up Arrow is pressed");
leftWin.upShift();
drawMandelbrot(leftWin);
} else if(StdDraw.isKeyPressed(39)){
//Right Arrow key
System.out.println("Right Arrow is pressed");
leftWin.rightShift();
drawMandelbrot(leftWin);
} else if(StdDraw.isKeyPressed(40)){
//Down Arrow key
System.out.println("Down Arrow is pressed");
leftWin.downShift();
drawMandelbrot(leftWin);
}//end if/else
}//end hasKeyTyped if statement
if(julia == true){
drawJulia(rightWin, JC);
}
//------ displaying the coordinates oin the bottom of the screen
StdDraw.setPenColor(StdDraw.BLACK);
if((StdDraw.mouseX() < 400) && (StdDraw.mouseY() > 45) ){
ComplexNumber lP = new ComplexNumber(leftWin.mapA(StdDraw.mouseX()), leftWin.mapB(StdDraw.mouseY()));
StdDraw.text(200,25,lP.toStringR5());
} else if(StdDraw.mouseY() > 45){
ComplexNumber rP = new ComplexNumber(rightWin.mapA(StdDraw.mouseX()), rightWin.mapB(StdDraw.mouseY()));
StdDraw.text(600,25,rP.toStringR5());
}
StdDraw.show();
StdDraw.setPenColor(StdDraw.WHITE);
StdDraw.filledRectangle(0,0,800,45);
// StdDraw.clear();
// break;
}//infinite drawing loop
}//main
public static void drawMandelbrot(DisplayWindow win){
ComplexNumber complexZero = new ComplexNumber(0,0);
for(double i = win.getRMin(); i <= win.getRMax(); i+= win.getStepSizeX()){
for(double k = win.getIMin(); k <= win.getIMax(); k+= win.getStepSizeY()){
// ComplexNumber c = new ComplexNumber(win.mapA(i),win.mapB(k));
ComplexNumber c = new ComplexNumber(i,k);
// System.out.println(mandelRec(c, 0, complexZero));
int count = mandelRec(c, 0, complexZero);
if(count < 0){
StdDraw.setPenColor(StdDraw.BLACK);
} else{
// StdDraw.setPenColor(count, 100,100);
StdDraw.setPenColor(Color.getHSBColor((float)(count+220.0f)/360.0f,1.0f,1.0f));
// System.out.println(count);
}
StdDraw.filledCircle(win.mapX(i),win.mapY(k),1);
}
}//
StdDraw.show();
}//end drawJulia
public static void drawJulia(DisplayWindow rightWin, ComplexNumber lP){
ComplexNumber complexZero = new ComplexNumber(0,0);
for(double i = rightWin.getRMin(); i <= rightWin.getRMax(); i+= rightWin.getStepSizeX()){
for(double k = rightWin.getIMin(); k <= rightWin.getIMax(); k+= rightWin.getStepSizeY()){
ComplexNumber c = new ComplexNumber(i,k);
// System.out.println(mandelRec(c, 0, complexZero));
int count = mandelRec(lP, 0, c);
if(count < 0){
StdDraw.setPenColor(StdDraw.BLACK);
} else{
// StdDraw.setPenColor(Color.getHSBColor((float)(count+220.0f)/360.0f,1.0f,1.0f));
// StdDraw.setPenColor(Color.getHSBColor(230.0f,(float)(count/240.0f)-0.2f,(float)(count/240.0f)+0.15f));
StdDraw.setPenColor(Color.getHSBColor(205.0f/360.0f,1.0f,(float)(count/240.0f)+0.15f));
}
StdDraw.filledCircle(rightWin.mapX(i),rightWin.mapY(k),1);
}
}
StdDraw.show();
}//end drawJulia
public static int mandelRec(ComplexNumber c, int count, ComplexNumber Zn1){
// System.out.println(c.toStringR5()+" " + count+ " "+ Zn1.toStringR5());
ComplexNumber ret = Zn1.square().add(c);
if(ret.magnitude() > 2){
return count;
} else if(count > 360){
return -1;
} else{
return mandelRec(c, count+1, ret);
}
// return false;
}//end mandelRec
}//JuliaSetGenerator class