| 1 | /* |
| 2 | * Mandelbrot set ASCII visualisation |
| 3 | * http://en.wikipedia.org/wiki/Mandelbrot_set |
| 4 | * http://www.root.cz/clanky/barvy-pro-shell/ |
| 5 | * Copylefted by: Harvie 2oo9 |
| 6 | |
| 7 | |
| 8 | aa |
| 9 | aa |
| 10 | aa |
| 11 | aa |
| 12 | aaaccaaa |
| 13 | aaai iaaa |
| 14 | aabaab baabaa |
| 15 | aab baa |
| 16 | aad daa |
| 17 | aab baa |
| 18 | aaaaad daaaaa |
| 19 | aaacccl lcccaaa |
| 20 | dbaaab baaabd |
| 21 | aab baa |
| 22 | aab baa |
| 23 | aaaaah haaaaa |
| 24 | ba aaaak c c kaaaa ab |
| 25 | aabc cbaa |
| 26 | aacabab babacaa |
| 27 | aae ZEN OF CODING eaa |
| 28 | aaa aaa |
| 29 | ae n cc n ea |
| 30 | aaa ab aa afa afa aa ba aaa |
| 31 | |
| 32 | */ |
| 33 | |
| 34 | package mandelbrot; |
| 35 | import java.math.*; |
| 36 | |
| 37 | public class mandelbrot { |
| 38 | public String[] chars = " .`-_\':,;^=+/\\\"|)\\<>)iv%xclrs{*}I?!][1taeo7zjLunT#JCwfy325Fp6mqSghVd4EgXPGZbYkOA&8U$@KHDBWNMR0Q".split(""); |
| 39 | public int max_iteration = 140; |
| 40 | public float zoom = 65; |
| 41 | |
| 42 | public double x_from = -2; |
| 43 | public double x_to = 2; |
| 44 | public double y_from = -2.1; |
| 45 | public double y_to = 2.1; |
| 46 | |
| 47 | public boolean color = false; |
| 48 | public boolean background = false; |
| 49 | |
| 50 | |
| 51 | public mandelbrot() { |
| 52 | max_iteration = 140; |
| 53 | zoom = 65; |
| 54 | |
| 55 | x_from = -2; |
| 56 | x_to = 2; |
| 57 | y_from = -2.1; |
| 58 | y_to = 2.1; |
| 59 | |
| 60 | color = false; |
| 61 | background = false; |
| 62 | } |
| 63 | |
| 64 | public int get_pixel_value(double x0, double y0) { |
| 65 | double x = 0; |
| 66 | double y = 0; |
| 67 | |
| 68 | int iteration = 0; |
| 69 | |
| 70 | while( x*x + y*y <= (2*2) && iteration < max_iteration ) { |
| 71 | double xtemp = x*x - y*y + x0; |
| 72 | y = 2*x*y + y0; |
| 73 | x = xtemp; |
| 74 | iteration++; |
| 75 | } |
| 76 | |
| 77 | if( iteration >= max_iteration ) { |
| 78 | return 0; |
| 79 | } else { |
| 80 | return iteration; |
| 81 | } |
| 82 | |
| 83 | } |
| 84 | |
| 85 | public String get_pixel_character(double x, double y) { |
| 86 | float i = ((float)get_pixel_value(x, y)/(float)max_iteration) * (chars.length-1); |
| 87 | chars[0]=" "; |
| 88 | return chars[Math.round(i)]; |
| 89 | } |
| 90 | |
| 91 | public String get_pixel_xterm_color(double x, double y) { |
| 92 | int i = Math.round( ((float)get_pixel_value(x, y)/max_iteration)*14 ); |
| 93 | return "\033["+((int)(i%2))+";"+(30+(int)(i/2))+"m"; |
| 94 | } |
| 95 | |
| 96 | public String get_pixel_xterm_background(double x, double y) { |
| 97 | int i = 40+Math.round( ((float)get_pixel_value(x, y)/max_iteration)*7 ); |
| 98 | return "\033["+i+"m"; |
| 99 | } |
| 100 | |
| 101 | public static void cls() { |
| 102 | System.out.print("\033[2J"); |
| 103 | } |
| 104 | |
| 105 | public static void top() { |
| 106 | System.out.print("\033[0;0H"); |
| 107 | } |
| 108 | |
| 109 | public void stat() { |
| 110 | System.out.print("ASCII Mandelbrot Set Visualisation (Harvie 2oo9) - Iterations: "+max_iteration+", zoom: "+zoom+" \n"); |
| 111 | } |
| 112 | |
| 113 | public void render() { |
| 114 | //if(color || background) max_iteration = 14; |
| 115 | |
| 116 | double x, y; |
| 117 | for(x=x_from; x<=x_to; x+=5/zoom) { for(y=y_from; y<=y_to; y+=2/zoom) { |
| 118 | if(color) System.out.print(get_pixel_xterm_color(x, y)); |
| 119 | if(background) System.out.print(get_pixel_xterm_background(x, y)); |
| 120 | |
| 121 | System.out.print(get_pixel_character(x, y)); |
| 122 | //System.out.print(get_pixel_color(x, y)+" "); |
| 123 | } System.out.println("\033[0m"); } |
| 124 | } |
| 125 | |
| 126 | } |