-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMovingHop_poly.java
More file actions
613 lines (486 loc) · 13.2 KB
/
MovingHop_poly.java
File metadata and controls
613 lines (486 loc) · 13.2 KB
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
import java.lang.Math;
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
/**
* Moving hopalong fractal.
* @author Terren Suydam
* 99/2/17
*/
public class MovingHop extends Applet
implements ActionListener
{
public CoordThread thread;
HopCanvas fractal;
ControlPanel controls;
public static String THREAD_FLD = "Threads";
public static String DOTS_FLD = "Dots";
public static String ZOOM_FLD = "Zoom";
public static String MORPH_FLD = "Morph";
public static String PLAY = "Play";
public static String PAUSE = "Pause";
public static String RESTART = "Restart";
public static String QUIT = "Quit";
public static String UPDATE = "Update";
public static final int initThreads = 10;
public static final int initDots = 300;
public static final double initZoom = 100;
public static final double initMorph = .01;
public static final int MAX_THREADS = 100;
public static final int MAX_DOTS = 1000;
public void init()
{
setLayout(new BorderLayout());
fractal = new HopCanvas(this);
controls = new ControlPanel(this, fractal);
add("South", controls);
add("Center", fractal);
controls.setValues(initThreads, initDots, initZoom, initMorph);
}
public void stop() {
if (thread != null)
thread.quit();
}
public void destroy() {
if (thread != null)
thread.quit();
}
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (MovingHop.PLAY.equals(command) ||
MovingHop.RESTART.equals(command) )
{
if (thread != null && thread.quit)
thread = null;
if (thread == null) {
fractal.repaint();
return;
}
} else {
if (thread == null) return;
}
if (MovingHop.PLAY.equals(command)) {
if (thread != null) {
synchronized (thread) {
thread.paused = false;
thread.notify();
}
}
} else if (MovingHop.RESTART.equals(command)) {
thread.setup();
fractal.repaint();
thread.paused = false;
synchronized (thread) {
thread.notify();
}
} else if (MovingHop.PAUSE.equals(command)) {
thread.paused = true;
thread.debug();
// advance one 'frame' if already paused
synchronized (thread) {
thread.notify();
}
} else if (MovingHop.QUIT.equals(command)) {
thread.quit();
} else if (MovingHop.UPDATE.equals(command)) {
thread.update(controls.getNumThreads(),
controls.getNumDots(),
controls.getZoom(),
controls.getMorph());
fractal.repaint();
}
}
public void hopalong() {
if (thread == null) {
thread = new CoordThread(this, fractal);
thread.setup();
thread.start();
}
}
public void nullifyThread() {
thread = null;
System.out.println("Threads killed");
}
}
class CoordThread extends Thread
{
MovingHop applet;
HopCanvas fractal;
public DrawerThread threads[] = new DrawerThread[MovingHop.MAX_THREADS];
int coords1[] = new int[MovingHop.MAX_DOTS * 2];
int coords2[] = new int[MovingHop.MAX_DOTS * 2];
public int numThreads = MovingHop.initThreads;
public int numDots = MovingHop.initDots;
public double zoom = MovingHop.initZoom;
public boolean paused = false;
public boolean updating = false;
public boolean quit = false;
public int deadThreads = 0;
// turn keeps all threads on sync;
// index refers to which thread on a given turn
long turn = 0;
int index = 0;
int hue;
int colorSteps = 100;
final int skipFirst = 5;
double a, b, c;
double ia, ib, ic;
// these three factors affect the morphing of a, b, c
double morphSize = MovingHop.initMorph;
double randomStepSize = .4;
double maxRange = 3f;
double scaleFactor;
int windowSize;
int xc, yc; // center of window;
public CoordThread(MovingHop applet, HopCanvas fractal) {
this.applet = applet;
this.fractal = fractal;
}
public void setup() {
a = maxRange*(Math.random()-0.5);
b = maxRange*(Math.random()-0.5);
c = maxRange*(Math.random()-0.5);
ia = Math.random() - .5;
ib = Math.random() - .5;
ic = Math.random() - .5;
Dimension d = fractal.getSize();
windowSize = d.width;
xc = d.width/2;
yc = d.height/2;
scaleFactor = (double)windowSize / (Math.sqrt(Math.abs(a * b * c)) * 25.0) * zoom / 100f;
}
void morphABC() {
ia += (Math.random() - .5) * randomStepSize;
ib += (Math.random() - .5) * randomStepSize;
ic += (Math.random() - .5) * randomStepSize;
normalize();
double dist = Math.sqrt(a*a + b*b + c*c);
if (dist > maxRange) {
ia = -ia;
ib = -ib;
ic = -ic;
double scale = dist/maxRange;
a /= scale;
b /= scale;
c /= scale;
}
a += ia*morphSize;
b += ib*morphSize;
c += ic*morphSize;
scaleFactor = (double)windowSize / (Math.sqrt(Math.abs(a * b * c)) * 25.0) * zoom / 100f;
}
void normalize() {
double size = Math.sqrt(ia*ia + ib*ib + ic*ic);
ia /= size;
ib /= size;
ic /= size;
}
public void debug() {
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
}
public void update(int numThreads, int numDots, double zoom, double morph) {
updating = true;
this.numThreads = numThreads;
this.numDots = numDots;
this.zoom = zoom;
this.morphSize = morph;
hue = 0;
colorSteps = numThreads * 2 + 10;
}
public void quit() {
quit = true;
System.out.println("Shutting down...");
}
void computeCoords(int[] coords) {
double x=0, y=0, xp;
int xs, ys;
for (int i=-skipFirst; i<numDots; i++) {
xp = y - (sign(x) * Math.sqrt(Math.abs(b*x - c)));
y = a - x;
x = xp;
if (i < 0) continue;
xs = (int) (x*scaleFactor) + xc;
ys = yc - (int) (y*scaleFactor);
coords[i*2] = xs;
coords[i*2+1] = ys;
}
}
public void run() {
DrawerThread current;
boolean firstRun = false;
int[] temp;
index = 0;
hue = 0;
Color color;
computeCoords(coords2);
while (true) {
firstRun = false;
if (paused && !quit) {
synchronized (this) {
try {
wait();
} catch (Exception e) {}
}
}
if (quit)
break;
morphABC();
temp = coords1;
coords1 = coords2;
coords2 = temp;
computeCoords(coords2);
hue = (hue+1)%colorSteps;
color = Color.getHSBColor((float)hue/(float)colorSteps,1f,1f);
if (threads[index] == null) {
threads[index] = new DrawerThread(this, fractal);
firstRun = true;
threads[index].turn = turn;
}
current = threads[index];
synchronized (current) {
if (!firstRun && current.turn != turn) {
if (current.turn != turn-1) {
current.turn = turn;
} else {
try {
// current thread busy, will wake this up when done
current.wait();
} catch (Exception e) {}
}
}
// current thread ready and waiting
current.setup(coords1, coords2, color);
if (firstRun)
current.start();
else
current.notify();
}
if (++index >= numThreads) {
index = 0;
turn++;
updating = false;
}
//try{
// sleep(500);
//} catch(Exception e) {}
}
// exit run loop
for (int i=0; i<MovingHop.MAX_THREADS; i++) {
if (threads[i] == null) break;
synchronized (threads[i]) {
threads[i].setup(coords1, coords2, Color.black);
threads[i].notify();
}
}
applet.nullifyThread();
}
double sign(double a) {
if (a == 0) return 0;
return (double)((a<0)?-1:1);
}
}
class DrawerThread extends Thread
{
CoordThread coord;
HopCanvas fractal;
Color color;
public long turn = 0;
//int[] oldCoords1, oldCoords2;
int[] newCoords1, newCoords2;
public DrawerThread(CoordThread coord, HopCanvas fractal) {
this.coord = coord;
this.fractal = fractal;
//oldCoords1 = new int[MovingHop.MAX_DOTS * 2];
//oldCoords2 = new int[MovingHop.MAX_DOTS * 2];
newCoords1 = new int[MovingHop.MAX_DOTS * 2];
newCoords2 = new int[MovingHop.MAX_DOTS * 2];
}
public void setup(int[] coords1, int[] coords2, Color color) {
//copyCoords(newCoords1, oldCoords1);
//copyCoords(newCoords2, oldCoords2);
copyCoords(coords1, newCoords1);
copyCoords(coords2, newCoords2);
this.color = color;
}
void copyCoords(int[] source, int[] dest) {
for (int i=0; i<coord.numDots*2; i++)
dest[i] = source[i];
}
public void run() {
int x[] = new int[3];
int y[] = new int[3];
while (true) {
Graphics g = fractal.getGraphics();
if (g != null) {
//g.setColor(Color.black);
//for (int i=0; i<coord.numDots; i++)
// g.drawLine(oldCoords1[i*2], oldCoords1[i*2+1],
// oldCoords2[i*2], oldCoords2[i*2+1]);
//synchronized(this) {
if (!(coord.updating || coord.quit)) {
g.setColor(color);
for (int i=1; i<coord.numDots; i++) {
x[0] = newCoords1[i*2-2];
x[1] = newCoords1[i*2];
x[2] = newCoords2[i*2];
y[0] = newCoords1[i*2-1];
y[1] = newCoords1[i*2+1];
y[2] = newCoords2[i*2+1];
color = Color.getHSBColor((float)Math.random(), (float)Math.random(), (float)Math.random());
g.setColor(color);
g.fillPolygon(x, y, 3);
}
//for (int i=0; i<coord.numDots; i++)
// g.drawLine(newCoords1[i*2], newCoords1[i*2+1],
// newCoords2[i*2], newCoords2[i*2+1]);
//for (int i=1; i<coord.numDots; i++)
// g.drawLine(newCoords1[i*2-2], newCoords1[i*2-1],
// newCoords1[i*2], newCoords1[i*2+1]);
}
//g.setColor(Color.black);
//for (int i=0; i<coord.numDots; i++)
// g.drawLine(newCoords1[i*2], newCoords1[i*2+1],
// newCoords2[i*2], newCoords2[i*2+1]);
//for (int i=1; i<coord.numDots; i++)
// g.drawLine(newCoords1[i*2-2], newCoords1[i*2-1],
// newCoords1[i*2], newCoords1[i*2+1]);
g = null;
//}
}
if (coord.quit) break;
synchronized(this) {
turn++;
try {
notify(); // wake up coord thread if waiting
wait();
} catch (Exception e) {}
}
}
coord.deadThreads++;
System.out.println("Drawer threads dead:" + coord.deadThreads);
synchronized (this) {
notify();
}
}
}
class HopCanvas extends Canvas {
MovingHop applet;
public HopCanvas(MovingHop applet) {
this.applet = applet;
setBackground(Color.black);
}
public void paint(Graphics g) {
super.paint(g);
applet.hopalong();
}
}
class ControlPanel extends Panel
{
MovingHop applet;
HopCanvas fractal;
TextField threadsFld;
TextField dotsFld;
TextField zoomFld;
TextField morphFld;
public ControlPanel(MovingHop applet, HopCanvas fractal)
{
this.applet = applet;
this.fractal = fractal;
setBackground(Color.black);
Panel panel = new Panel();
panel.setLayout(new BorderLayout());
Button button = new Button(MovingHop.PLAY);
button.setActionCommand(MovingHop.PLAY);
button.addActionListener(applet);
panel.add("North", button);
button = new Button(MovingHop.PAUSE);
button.setActionCommand(MovingHop.PAUSE);
button.addActionListener(applet);
panel.add("South", button);
add(panel);
panel = new Panel();
panel.setLayout(new BorderLayout());
button = new Button(MovingHop.RESTART);
button.setActionCommand(MovingHop.RESTART);
button.addActionListener(applet);
panel.add("North", button);
button = new Button(MovingHop.QUIT);
button.setActionCommand(MovingHop.QUIT);
button.addActionListener(applet);
panel.add("South", button);
add(panel);
panel = new Panel();
panel.setLayout(new BorderLayout());
panel.setBackground(Color.gray);
panel.add("North", new Label(MovingHop.THREAD_FLD + ":"));
threadsFld = new TextField(2);
panel.add("South", threadsFld);
add(panel);
panel = new Panel();
panel.setLayout(new BorderLayout());
panel.setBackground(Color.gray);
panel.add("North", new Label(MovingHop.DOTS_FLD + ":"));
dotsFld = new TextField(7);
panel.add("South", dotsFld);
add(panel);
panel = new Panel();
panel.setLayout(new BorderLayout());
panel.setBackground(Color.gray);
panel.add("North", new Label(MovingHop.ZOOM_FLD + ":"));
zoomFld = new TextField(5);
panel.add("South", zoomFld);
add(panel);
panel = new Panel();
panel.setLayout(new BorderLayout());
panel.setBackground(Color.gray);
panel.add("North", new Label(MovingHop.MORPH_FLD + ":"));
morphFld = new TextField(5);
panel.add("South", morphFld);
add(panel);
button = new Button(MovingHop.UPDATE);
button.setActionCommand(MovingHop.UPDATE);
button.addActionListener(applet);
add(button);
}
public void setValues(int threads, int dots, double zoom, double morph) {
threadsFld.setText("" + threads);
dotsFld.setText("" + dots);
zoomFld.setText("" + zoom);
morphFld.setText("" + morph);
repaint();
}
public int getNumThreads() {
String t = threadsFld.getText();
try {
return Integer.valueOf(t).intValue();
} catch (NumberFormatException e) {
return -1;
}
}
public int getNumDots() {
String t = dotsFld.getText();
try {
return Integer.valueOf(t).intValue();
} catch (NumberFormatException e) {
return -1;
}
}
public double getZoom() {
String t = zoomFld.getText();
try {
return Double.valueOf(t).doubleValue();
} catch (NumberFormatException e) {
return -1;
}
}
public double getMorph() {
String t = morphFld.getText();
try {
return Double.valueOf(t).doubleValue();
} catch (NumberFormatException e) {
return -1;
}
}
}