// // Mandel.java // // Visualizza l'insieme di Mandelbrot nel rettangolo (x1,y1)x(x2,y2) // del piano complesso. // // @(#) 970420 (marco@isinet.it) // import java.applet.*; import java.awt.*; public class Mandel extends Applet { double x1 = -0.796; double y1 = 0.156; double x2 = -0.785; double y2 = 0.166; double new_x1 = x1; double new_y1 = y1; double new_x2 = x2; double new_y2 = y2; double zre0 = 0.0; double zim0 = 0.0; int MAX_CICLI = 60; int MAX_COLOR = 100; int MAX_X ; int MAX_Y ; boolean tracciato[][] = new boolean[1000][1000]; int quadro_x1, quadro_y1; // // init() // // non fa nulla // public void init() { } // // punto(x,y) // // calcola se il punto (x,y) del piano complesso appartiene // o meno all'insieme di Mandelbrot. // public int punto(double re, double im) { double temp, zim, zre; int c, k; zim = zre0; zre = zim0; k = 0; while (((zre*zre + zim*zim) <= 4.0) && (k <= MAX_CICLI)) { temp = ((zre*zre) - (zim*zim)) + re; zim = (2.0*(zim*zre)) + im; zre = temp; k++; } return k; } // // paint(g) // // esegue il ciclo principale e visualizza graficamente // l'insieme di Mandelbrot. // public void paint(Graphics G) { G.setColor(Color.black); // riempie l'area grafica di bianco Rectangle r = this.bounds(); G.fillRect(0,0,r.width,r.height); calcola(); } // // calcola() // // esegue il ciclo principale e visualizza graficamente // l'insieme di Mandelbrot. // public void calcola() { Graphics G = this.getGraphics(); int i, j, k, ris, colore, maxcolor; double dx, dy, cim, cre; Rectangle r = this.bounds(); showStatus ("("+x1+", "+y1+") x ("+x2+", "+y2+")"); MAX_X = r.width; MAX_Y = r.height; dx = (x2-x1)/(double)MAX_X; // passo sull'asse x dy = (y2-y1)/(double)MAX_Y; // passo sull'asse y //G.setColor(Color.white); // riempie l'area grafica di bianco //G.fillRect(0,0,100,100); for (j=0; j<=MAX_Y; j++) { for (i=0; i<=MAX_X; i++) { tracciato[i][j] = false; } } for (k=32; k>=2; k=(int)(k/2)) { cim = y2+dy*k; // valore iniziale parte immaginaria di y for (j=0; j<=MAX_Y; j=j+k) { // questo ciclo fa variare la coordinata y cim -= (dy*k); // varia la parte immaginaria di c cre = x1-(dx*k); // inizializza parte reale di c for (i=0; i<=MAX_X; i=i+k) { // questo ciclo fa variare la coordinata x cre += (dx*k); // incrementa la parte reale di c if (!tracciato[i][j]) { ris = punto(cre, cim); // calcola il valore dell'insieme di // Mandelbrot nel punto (cre,cim) //G.setColor(Color.yellow); //G.drawRect(i, j, 1, 1); if (ris < MAX_CICLI) { // il punto non appartiene all'insieme G.setColor(Color.getHSBColor((float)ris/(float)MAX_CICLI, (float)0.9, (float)ris/(float)MAX_CICLI)); } else { // il punto appartiene all'insieme G.setColor(Color.black); } G.fillRect(i, j, k, k); // traccia il punto tracciato[i][j] = true; } } } } } public boolean mouseDown(Event e, int x, int y) { new_x1 = (float)x1 + ((float)x * (float)(Math.abs(x2 - x1))) / (float)MAX_X; new_y1 = (float)y2 - ((float)y * (float)(Math.abs(y2 - y1))) / (float)MAX_Y; Graphics G = getGraphics(); G.setColor(Color.yellow); G.drawRect(x,y,1,1); quadro_x1 = x; quadro_y1 = y; return true; } public boolean mouseUp(Event e, int x, int y) { new_x2 = (float)x1 + ((float)x * (float)(Math.abs(x2 - x1))) / (float)MAX_X; new_y2 = (float)y2 - ((float)y * (float)(Math.abs(y2 - y1))) / (float)MAX_Y; Graphics G = getGraphics(); G.setColor(Color.yellow); G.drawRect(quadro_x1,quadro_y1,Math.abs(quadro_x1-x), Math.abs(y-quadro_y1)); x1 = new_x1; y1 = new_y1; x2 = new_x2; y2 = new_y2; calcola(); return true; } public int run() { calcola(); return 1; } public boolean mouseEnter(Event e, int x, int y) { float real_x = (float)x1 + ((float)x * (float)(Math.abs(x2 - x1))) / (float)MAX_X; float real_y = (float)y2 - ((float)y * (float)(Math.abs(y2 - y1))) / (float)MAX_Y; showStatus("("+real_x+", "+real_y+")"); return true; } public boolean mouseMove(Event e, int x, int y) { float real_x = (float)x1 + ((float)x * (float)(Math.abs(x2 - x1))) / (float)MAX_X; float real_y = (float)y2 - ((float)y * (float)(Math.abs(y2 - y1))) / (float)MAX_Y; showStatus("("+real_x+", "+real_y+")"); return true; } public boolean mouseDrag(Event e, int x, int y) { float real_x = (float)x1 + ((float)x * (float)(Math.abs(x2 - x1))) / (float)MAX_X; float real_y = (float)y1 + ((float)y * (float)(Math.abs(y2 - y1))) / (float)MAX_Y; showStatus("Area: ("+real_x+", "+real_y+")"); return true; } public boolean mouseExit(Event e, int x, int y) { showStatus ("("+x1+", "+y1+") x ("+x2+", "+y2+")"); return true; } }