-
-
Notifications
You must be signed in to change notification settings - Fork 707
Expand file tree
/
Copy pathMandelbrot.hx
More file actions
141 lines (117 loc) · 3.07 KB
/
Copy pathMandelbrot.hx
File metadata and controls
141 lines (117 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#if (!js && !flash)
import haxe.io.Bytes;
#end
#if anon_objects
typedef RGB = { r:Int, g:Int, b:Int };
typedef Complex = { i:Float, j:Float };
#else
class RGB
{
public var r:Int;
public var g:Int;
public var b:Int;
public function new(inR:Int, inG:Int, inB:Int)
{
r = inR;
g = inG;
b = inB;
}
}
class Complex
{
public var i:Float;
public var j:Float;
public function new(inI:Float, inJ:Float)
{
i = inI;
j = inJ;
}
}
#end
class Mandelbrot
{
static inline var SIZE = 10;
static inline var MaxIterations = 1000;
static inline var MaxRad = 1<<16;
static inline var width = 35*SIZE;
static inline var height = 20*SIZE;
public static function main()
{
var t0 = now();
var palette = [];
for(i in 0...MaxIterations+1)
palette.push( createPalette(i/MaxIterations) );
var image = [];
image[ width*height-1 ] = null;
var outPixel = 0;
var scale = 0.1/SIZE;
for(y in 0...height)
{
if ( (y%10)== 0)
trace(y);
for(x in 0...width)
{
var offset = createComplex(x*scale - 2.5,y*scale - 1);
var val = createComplex(0.0,0.0);
var iteration = 0;
while( complexLength2(val)<MaxRad && iteration<MaxIterations)
{
val = complexAdd( complexSquare(val), offset );
iteration++;
}
image[outPixel++] = palette[iteration];
}
}
var time = now()-t0;
trace('Mandelbrot $width x $height : $time s');
#if (!js && !flash)
var header = 'P6 $width $height 255\n';
var buffer = Bytes.alloc(header.length + width*height*3);
var pos = header.length;
buffer.blit(0, Bytes.ofString(header), 0, pos );
for(pixel in image)
{
buffer.set(pos++, pixel.r);
buffer.set(pos++, pixel.g);
buffer.set(pos++, pixel.b);
}
sys.io.File.saveBytes("mandelbrot.ppm", buffer);
#end
}
public static function now()
{
#if cppia
return 0;
#else
return haxe.Timer.stamp();
#end
}
public static function complexLength2(val:Complex) : Float
{
return val.i*val.i + val.j*val.j;
}
public static function complexAdd(val0:Complex, val1:Complex)
{
return createComplex( val0.i + val1.i, val0.j + val1.j );
}
public static function complexSquare(val:Complex)
{
return createComplex( val.i*val.i - val.j*val.j, 2.0 * val.i * val.j );
}
#if anon_objects
inline public static function createComplex(inI:Float, inJ:Float) return { i:inI, j:inJ };
#else
inline public static function createComplex(inI:Float, inJ:Float) return new Complex(inI, inJ);
#end
public static function createPalette(inFraction:Float)
{
var r = Std.int(inFraction*255);
var g = Std.int((1-inFraction)*255);
var b = Std.int( (0.5-Math.abs(inFraction-0.5))*2*255 );
#if anon_objects
return { r:r, g:g, b:b };
#else
return new RGB(r,g,b);
#end
}
}