Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Mandelbrot in 512 bytes

  • Loading branch information...
commit 0faadc3f907e5a13531e626b3268f9414ca49851 1 parent 9af704a
@ricbit authored
View
168 2008-04-mandelbrot/F.j
@@ -0,0 +1,168 @@
+; Mandelbrot Fractal under 512 bytes
+; author: Ricardo Bittencourt
+; thanks to Girino Vey for his help
+
+; compile with Jasmin: http://jasmin.sourceforge.net/
+
+.class public synchronized F
+.super java/applet/Applet
+
+; --------------------------------------------------
+.method public <init>()V
+ .limit stack 33 ; chosen to increse redundancy
+ .limit locals 33 ; for zip compression
+ aload_0
+ invokenonvirtual java/applet/Applet/<init>()V
+ return
+.end method
+
+; --------------------------------------------------
+.method public paint(Ljava/awt/Graphics;)V
+ .limit stack 33
+ .limit locals 33
+
+ ; local 2 = j*256+i
+ iconst_0
+ istore_2
+outer_loop:
+
+ ; local 3 = c
+ ; c=-1;
+ iconst_m1
+ istore_3
+
+ ; local 7 = x
+ ; local 8 = y
+ ; local 9 = x2
+ ; local 6 = y2
+ ; local 0 = k
+ ; x=y=x2=y2=k=0;
+
+ fconst_0
+ dup
+ dup2
+ dup
+ fstore 7 ;x
+ fstore 8 ;y
+ fstore 9 ;x2
+ fstore 10 ;y2
+ fstore_0 ;k
+
+inner_loop:
+ ; y=x*y*2+(j+(-128))/100f;
+
+ iload_2
+ bipush 8
+ ishr
+ bipush -128
+ iadd
+ i2f
+ ldc 100.0
+ fdiv
+
+ fload 7 ;x
+ fload 8 ;y
+ fmul
+ fconst_2
+ fmul
+ fadd
+ fstore 8 ;y
+
+ ; x=x2-y2+i/100f-2;
+
+ fload 9 ;x2
+ fload 10 ;y2
+ fsub
+ iload_2 ; i
+ ldc 255
+ iand
+ i2f
+ ldc 100.0
+ fdiv
+ fadd
+ fconst_2
+ fsub
+ dup
+ fstore 7 ; x
+
+ ; x2=x*x;
+
+ dup
+ fmul
+ dup
+ fstore 9 ;x2
+
+ ; y2=y*y
+
+ fload 8 ; y
+ dup
+ fmul
+ dup
+ fstore 10 ; y2
+
+ ; if x2+y2>4
+ fadd
+ fconst_2
+ fdiv
+ fconst_2
+ fcmpl
+ ifge set_color
+
+ ; k+=0.03
+ fload_0 ;k
+ ldc 0.03
+ fadd
+ dup
+ fstore_0
+
+ ; if k>1
+ fconst_1
+ fcmpg
+ ifge draw_now
+ goto inner_loop
+
+set_color:
+
+ fload_0 ; k
+ f2d
+ ldc 0.7
+ f2d
+ invokestatic java/lang/Math/pow(DD)D
+ ldc 255
+ i2d
+ dmul
+ d2i
+ bipush 8
+ ishl
+ istore_3 ; c
+
+draw_now:
+ aload_1
+ new java/awt/Color
+ dup
+ iload_3
+ invokenonvirtual java/awt/Color/<init>(I)V
+ invokevirtual java/awt/Graphics/setColor(Ljava/awt/Color;)V
+
+ ; g.drawrect (i,j,1,1);
+ aload_1
+ iload_2
+ ldc 255
+ iand
+ iload_2
+ bipush 8
+ ishr
+ iconst_1
+ iconst_1
+ invokevirtual java/awt/Graphics/drawRect(IIII)V
+
+ iinc 2 1
+ iload_2
+ iconst_2
+ bipush 16
+ ishl
+ if_icmple outer_loop
+
+ return
+.end method
+
View
30 2008-04-mandelbrot/fractal.as
@@ -0,0 +1,30 @@
+import flash.display.BitmapData;
+
+var fractal:BitmapData=new BitmapData (256,256,false,0);
+this.createEmptyMovieClip("movie", this.getNextHighestDepth());
+movie.attachBitmap(fractal, movie.getNextHighestDepth(), "auto",true);
+movie._x=0; movie._y=0;
+
+for (var i:Number=0; i<256; i++)
+ for (var j:Number=0; j<128; j++) {
+ var color:Number=Mandelbrot((i/256)*2.5-2,j/100);
+ fractal.setPixel(i,128+j,color);
+ fractal.setPixel(i,128-j,color);
+ }
+
+stop();
+
+function Mandelbrot(xc:Number, yc:Number):Number {
+ var i:Number, x:Number, y:Number, x2:Number, y2:Number;
+
+ x=0; y=0; x2=0; y2=0; i=0;
+ while (i++<32) {
+ if (x2+y2>4)
+ return (Math.pow(i/32,0.7)*0xFF00)&0xFF00;
+ y=2*x*y+yc;
+ x=x2-y2+xc;
+ x2=x*x;
+ y2=y*y;
+ }
+ return 0xFFFFFF;
+}
View
1  2008-04-mandelbrot/fractal.html
@@ -0,0 +1 @@
+<table cellpadding=0 cellspacing=0><script language="JavaScript">function t(d){s="";for(r=20;r>=0;r-=4)s+="0123456789ABCDEF".charAt((d>>r)&15);return s;}function m(xc,yc){for(x=y=q=w=k=0;k++<32;){if(q+w>4)return(Math.pow(k/32,0.7)*0xFF00)&0xFF00;y=2*x*y+yc;x=q-w+xc;q=x*x;w=y*y;}return 0xFFFFFF;}for(var j=-128;j<128;j++){document.write("<tr>");for(var i=-2;i<0.5;i+=0.01) document.write("<td bgcolor=#"+t(m(i,j/100))+" width=2 height=2></td>");document.write("</tr>");}</script></table>
View
5 2008-04-mandelbrot/fractal.py
@@ -0,0 +1,5 @@
+from Tkinter import*
+Tk();k=256;p=PhotoImage(w=k)
+m=lambda z,i,n:(abs(z)>2or n>1)*(i,n)or m(z*z+i/k/1e2-2+(128-i%k)/1e2j,i,n+.03)
+[p.put("#%04x00"%(n**.7*k),(i/k,i%k))for(i,n)in[m(0,i,0)for i in range(k*k)]if n<1]
+Label(i=p,bg="#fff").grid();mainloop()
View
5 README
@@ -452,6 +452,11 @@ Name: 2008-03-roman
Language: C
Description: Roman numbers using preprocessor metaprogramming.
+Name: 2008-04-mandelbrot
+Language: Actionscript, Python, Javascript, Bytecode JVM
+Description: The Mandelbrot fractal implemented under 512 bytes in many
+languages.
+
Name: 2008-04-sudoku
Language: C++
Description: Solver for irregular sudokus using exact cover and dancing links.
Please sign in to comment.
Something went wrong with that request. Please try again.