View difference between Paste ID: 6hrmXSGQ and bzPHfaqe
SHOW: | | - or go back to the newest paste.
1
import java.awt.*;
2
import java.awt.image.*;
3
import javax.imageio.*;
4
import java.io.*;
5
6
/*
7
  Autore     : Marco 'RootkitNeo'
8
  Descrizione: Classe che si occupa della creazione del frattale
9
  Dipendenza : http://pastebin.com/jfTDFE8W
10
*/
11
12
13
class Fractal {
14
  // Variabili
15
  // -----------------------------------------------------------------------------------------------------------
16
  private Image img;                                     // Frattale
17
  private double X0,X1, Y0, Y1, modulo, x, y;            // [X0,Y0][X0,Y1] punti luce; modulo: valore temporaneo abs(); x,y valori temporanei pixel immagine
18
  private int cicli,profondita, width, height;           // profondita: dettaglio immagine
19
  private Complex c1, c2;                                // Numeri complessi
20
  // -----------------------------------------------------------------------------------------------------------
21
  
22
  
23
  Fractal(int width, int height, double X0, double X1, double Y0, double Y1, int profondita) {
24
    this.width = width;
25
    this.height= height;
26
    this.X0 = X0;
27
    this.X1 = X1;
28
    this.Y0 = Y0;
29
    this.Y1 = Y1;
30
    this.profondita = profondita;
31
    
32
  }
33
  
34
  void generaFrattale() {
35
    int[] pixels = new int[width*height];
36
    int k = 0;
37
    img = null;
38
    
39
    for(int i=0; i<height; i++) {
40
      y = Y1 + (((double)i * (Y0-Y1)) / (double)height);
41
      
42
      for(int j = 0; j<width; j++) {
43
        x = X1 + (((double)j * (X0-X1)) / (double)width);
44
        
45
        c1 = new Complex(x,y);
46
        c2 = c1;
47
        
48
        
49
        for(cicli = 0; cicli < profondita; cicli++) {
50
          c1 = c1.somma(c1.prodotto(c1,c1),c2);
51
          modulo = c1.abs();
52
          
53
          if(modulo > 4.0) break;
54
        }
55
        
56
        if(modulo<=4.0) {
57
          pixels[k++] = (255 << 24) | 0;
58
        } else {
59
          int r, g, b;
60
          
61
          r =  (cicli>>2);
62
          g =  (cicli>>1);
63
          b =  10+(cicli<<2);
64
          
65
          r = (r>128) ? 128 : r;
66
          g = (g>128) ? 128 : g;
67
          b = (b>255) ? 255 : b;
68
          
69
          pixels[k++] = (255 << 24) | (r << 16) | (g << 8) | b;
70
          
71
        }
72
      }
73
    } 
74
75
    try {
76
      Toolkit tk = Toolkit.getDefaultToolkit();
77
      img = tk.createImage(new MemoryImageSource(width, height,pixels,0,width));
78
    } catch(Exception e) {
79
      e.printStackTrace();
80
    }
81
  }
82
  
83
  
84
  void salvaImmagine(String name) {
85
    if(img == null) return;
86
    BufferedImage b = new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);
87
    Graphics g = b.getGraphics();
88
89
    try {
90
      g.drawImage(img.getScaledInstance(width,height,Image.SCALE_SMOOTH),0,0,null);
91
    
92
      ImageIO.write(b,"PNG",new File(name+".png"));
93
    } catch(Exception e) {e.printStackTrace();}
94
  }
95
  
96
  
97
  Image getImage() {
98
    return img;
99
  }
100
}