Skip to content

Commit 07a34eb

Browse files
author
PSvils
committed
Working implementations for Julia and Mandelbrot sets. Supports zooming and moving, but currently slow generation.
1 parent 1678d5b commit 07a34eb

File tree

5 files changed

+199
-0
lines changed

5 files changed

+199
-0
lines changed

progen/geom/Vector2D.hx

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package progen.geom;
2+
3+
/**
4+
* ...
5+
* @author P Svilans
6+
*/
7+
class Vector2D
8+
{
9+
10+
public var x:Float;
11+
public var y:Float;
12+
13+
public function new(x:Float = 0.0, y:Float = 0.0):Void
14+
{
15+
this.x = x;
16+
this.y = y;
17+
}
18+
19+
}

progen/julia/ComplexData.hx

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package progen.julia;
2+
3+
/**
4+
* ...
5+
* @author P Svilans
6+
*/
7+
8+
class ComplexData
9+
{
10+
11+
public var real:Float;
12+
public var imaginary:Float;
13+
14+
public var valid:Bool;
15+
public var iterations:Int;
16+
17+
public function new(real:Float = 0.0, imaginary:Float = 0.0):Void
18+
{
19+
this.real = real;
20+
this.imaginary = imaginary;
21+
22+
valid = true;
23+
iterations = 0;
24+
}
25+
26+
}

progen/julia/ComplexSet.hx

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package progen.julia;
2+
import progen.geom.Vector2D;
3+
4+
/**
5+
* ...
6+
* @author P Svilans
7+
*/
8+
class ComplexSet
9+
{
10+
11+
public var position:Vector2D;
12+
public var zoom:Vector2D;
13+
14+
public var width:UInt;
15+
public var height:UInt;
16+
17+
public var data:Array<ComplexData>;
18+
19+
public function new():Void
20+
{
21+
position = new Vector2D();
22+
zoom = new Vector2D(1.0, 1.0);
23+
}
24+
25+
public function initialize(width:UInt, height:UInt):Void
26+
{
27+
this.width = width;
28+
this.height = height;
29+
30+
data = createBuffer(width * height);
31+
}
32+
33+
private inline function createBuffer(length:UInt):Array<ComplexData>
34+
{
35+
var buffer = new Array<ComplexData>();
36+
for (i in 0...length) buffer.push(new ComplexData());
37+
38+
return buffer;
39+
}
40+
41+
public function generate(iterations:UInt = 1):Void
42+
{
43+
for (x in 0...width)
44+
for (y in 0...height)
45+
{
46+
var current = data[y * width + x];
47+
48+
if (current.valid)
49+
{
50+
for (i in 0...iterations)
51+
{
52+
process(current);
53+
54+
current.iterations++;
55+
56+
//if the point is outside the circle with radius 2: stop
57+
if ((current.real * current.real + current.imaginary * current.imaginary) > 4)
58+
{
59+
current.valid = false;
60+
break;
61+
}
62+
}
63+
}
64+
}
65+
}
66+
67+
private function process(current:ComplexData):Void
68+
{
69+
70+
}
71+
72+
}

progen/julia/JuliaSet.hx

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package progen.julia;
2+
import progen.geom.Vector2D;
3+
import progen.julia.ComplexData;
4+
5+
/**
6+
* ...
7+
* @author P Svilans
8+
*/
9+
class JuliaSet extends ComplexSet
10+
{
11+
12+
public var constant_real:Float;
13+
public var constant_imaginary:Float;
14+
15+
public function new(constant_real:Float = -0.7, constant_imaginary:Float = 0.27015):Void
16+
{
17+
this.constant_real = constant_real;
18+
this.constant_imaginary = constant_imaginary;
19+
20+
super();
21+
}
22+
23+
override public function initialize(width:UInt, height:UInt):Void
24+
{
25+
super.initialize(width, height);
26+
27+
for (y in 0...height)
28+
for (x in 0...width)
29+
{
30+
var current = data[y * width + x];
31+
32+
//calculate the initial real and imaginary part of z, based on the pixel location and zoom and position values
33+
current.real = 1.5 * (x - width / 2) / (0.5 * zoom.x * width) + position.x;
34+
current.imaginary = (y - height / 2) / (0.5 * zoom.y * height) + position.y;
35+
}
36+
}
37+
38+
override function process(current:ComplexData):Void
39+
{
40+
//remember value of previous iteration
41+
var old_real:Float = current.real;
42+
var old_imaginary:Float = current.imaginary;
43+
44+
//the actual iteration, the real and imaginary part are calculated
45+
current.real = old_real * old_real - old_imaginary * old_imaginary + constant_real;
46+
current.imaginary = 2 * old_real * old_imaginary + constant_imaginary;
47+
}
48+
49+
}

progen/julia/MandelbrotSet.hx

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package progen.julia;
2+
import progen.geom.Vector2D;
3+
import progen.julia.ComplexData;
4+
5+
/**
6+
* ...
7+
* @author P Svilans
8+
*/
9+
10+
class MandelbrotSet extends ComplexSet
11+
{
12+
13+
public function new():Void
14+
{
15+
super();
16+
}
17+
18+
override function process(current:ComplexData):Void
19+
{
20+
//calculate the initial real and imaginary part of z, based on the pixel location and zoom and position values
21+
var pr = 1.5 * (x - width / 2) / (0.5 * zoom.x * width) + position.x;
22+
var pi = (y - height / 2) / (0.5 * zoom.y * width) + position.y;
23+
24+
//remember value of previous iteration
25+
var old_real:Float = current.real;
26+
var old_imaginary:Float = current.imaginary;
27+
28+
//the actual iteration, the real and imaginary part are calculated
29+
current.real = old_real * old_real - old_imaginary * old_imaginary + pr;
30+
current.imaginary = 2 * old_real * old_imaginary + pi;
31+
}
32+
33+
}

0 commit comments

Comments
 (0)