Advertisement
Guest User

178H

a guest
Sep 7th, 2014
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.60 KB | None | 0 0
  1.     import java.awt.image.BufferedImage;
  2.     import java.io.File;
  3.     import java.io.IOException;
  4.     import java.util.regex.Pattern;
  5.     import javax.imageio.ImageIO;
  6.  
  7.     public class Fractal {
  8.    
  9.     public static int n;
  10.     public static BufferedImage fractal;
  11.     public static Pattern p;
  12.     public static int threadNumber;
  13.     private static int returnedThreads = 0;
  14.     private static long t = System.nanoTime();
  15.    
  16.     public static void main(String[] args) throws IOException {
  17.         n = Integer.parseInt(args[0]);
  18.         p = Pattern.compile(args[1]);
  19.         threadNumber = Integer.parseInt(args[2]);
  20.         fractal = new BufferedImage(n, n, BufferedImage.TYPE_INT_RGB);
  21.         for (int i = 0; i < threadNumber; i++) {
  22.             Thread thread = new Thread(new FractalThread(n, i));
  23.             thread.start();
  24.         }
  25.     }
  26.    
  27.     public static synchronized void writeImage() throws IOException {
  28.         returnedThreads++;
  29.         if(returnedThreads >= threadNumber) {
  30.             long ti = System.nanoTime();
  31.             File f = new File("fractal.png");
  32.             ImageIO.write(fractal, "PNG", f);
  33.             System.out.println("Done in " + (System.nanoTime()-t)/1000000000F + " s");
  34.         }
  35.     }
  36.    
  37.     }
  38.  
  39.     import java.io.IOException;
  40.     import java.util.regex.Matcher;
  41.  
  42.     public class FractalThread implements Runnable {
  43.    
  44.     private int n;
  45.     private int threads = Fractal.threadNumber;
  46.     private int startingPoint;
  47.  
  48.     public FractalThread(int n, int startingPoint) {
  49.         this.n = n;
  50.         this.startingPoint = startingPoint;
  51.     }
  52.    
  53.     @Override
  54.     public void run() {
  55.         for (int i = startingPoint; i < n * n; i += threads) {
  56.             String code = getCode(i);
  57.             Matcher m = Fractal.p.matcher(code);
  58.             float percentage;
  59.             int rgb = 0;
  60.             if (m.find()) {
  61.                 String match = m.group(1);
  62.                 percentage = match.length()/(float)code.length();
  63.             } else {
  64.                 percentage = 1;
  65.                 rgb += 255 * 65536 + 255 * 256;
  66.             }
  67.             int blue = (int) (255 * percentage);
  68.             rgb += blue;
  69.             Fractal.fractal.setRGB(i % n, i / n, rgb);
  70.         }
  71.         try {
  72.             Fractal.writeImage();
  73.         } catch (IOException e) {
  74.             e.printStackTrace();
  75.         }
  76.     }
  77.    
  78.     public String getCode(int pixel) {
  79.         StringBuilder code = new StringBuilder();
  80.         int currentN = n;
  81.         int x = pixel % currentN;
  82.         int y = pixel / currentN;
  83.         while (currentN > 1) {
  84.             if (y >= currentN / 2) {
  85.                 if (x >= currentN / 2) {
  86.                     code.append("4");
  87.                     x -= currentN / 2;
  88.                     y -= currentN / 2;
  89.                 } else {
  90.                     code.append("3");
  91.                     y -= currentN / 2;
  92.                 }
  93.             } else {
  94.                 if (x >= currentN / 2) {
  95.                     code.append("1");
  96.                     x -= currentN / 2;
  97.                 } else {
  98.                     code.append("2");
  99.                 }
  100.             }
  101.             currentN /= 2;
  102.         }
  103.         return code.toString();
  104.     }
  105.  
  106.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement