Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.image.*;
- import javax.imageio.*;
- import java.io.*;
- /*
- Autore : Marco 'RootkitNeo'
- Descrizione: Classe che si occupa della creazione del frattale
- Dipendenza : http://pastebin.com/jfTDFE8W
- */
- class Fractal {
- // Variabili
- // -----------------------------------------------------------------------------------------------------------
- private Image img; // Frattale
- 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
- private int cicli,profondita, width, height; // profondita: dettaglio immagine
- private Complex c1, c2; // Numeri complessi
- // -----------------------------------------------------------------------------------------------------------
- Fractal(int width, int height, double X0, double X1, double Y0, double Y1, int profondita) {
- this.width = width;
- this.height= height;
- this.X0 = X0;
- this.X1 = X1;
- this.Y0 = Y0;
- this.Y1 = Y1;
- this.profondita = profondita;
- }
- void generaFrattale() {
- int[] pixels = new int[width*height];
- int k = 0;
- img = null;
- for(int i=0; i<height; i++) {
- y = Y1 + (((double)i * (Y0-Y1)) / (double)height);
- for(int j = 0; j<width; j++) {
- x = X1 + (((double)j * (X0-X1)) / (double)width);
- c1 = new Complex(x,y);
- c2 = c1;
- for(cicli = 0; cicli < profondita; cicli++) {
- c1 = c1.somma(c1.prodotto(c1,c1),c2);
- modulo = c1.abs();
- if(modulo > 4.0) break;
- }
- if(modulo<=4.0) {
- pixels[k++] = (255 << 24) | 0;
- } else {
- int r, g, b;
- r = (cicli>>2);
- g = (cicli>>1);
- b = 10+(cicli<<2);
- r = (r>128) ? 128 : r;
- g = (g>128) ? 128 : g;
- b = (b>255) ? 255 : b;
- pixels[k++] = (255 << 24) | (r << 16) | (g << 8) | b;
- }
- }
- }
- try {
- Toolkit tk = Toolkit.getDefaultToolkit();
- img = tk.createImage(new MemoryImageSource(width, height,pixels,0,width));
- } catch(Exception e) {
- e.printStackTrace();
- }
- }
- void salvaImmagine(String name) {
- if(img == null) return;
- BufferedImage b = new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);
- Graphics g = b.getGraphics();
- try {
- g.drawImage(img.getScaledInstance(width,height,Image.SCALE_SMOOTH),0,0,null);
- ImageIO.write(b,"PNG",new File(name+".png"));
- } catch(Exception e) {e.printStackTrace();}
- }
- Image getImage() {
- return img;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement