-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathMandelbrotTileProvider.cs
87 lines (68 loc) · 2.51 KB
/
MandelbrotTileProvider.cs
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
// from http://www.coldcity.com/index.php/fractals-in-c/
using System.IO;
using System.Drawing.Imaging;
using Ptv.XServer.Controls.Map.Layers.Tiled;
namespace Mandelbrot
{
public class MandelbrotTileProvider : ITiledProvider
{
#region IMapTileServer Members
public Stream GetImageStream(int tx, int ty, int zoom)
{
const int imageWidth = 256;
const int imageHeight = 256;
const double axMin = -1.95;
const double axMax = axMin + 2.5;
const double ayMin = -1.25;
const double ayMax = ayMin + 2.5;
double part = 1.0 / (1 << zoom);
double xMin = axMin + tx * part * (axMax - axMin);
double xMax = axMin + (tx+1) * part * (axMax - axMin);
double yMin = ayMin + ty * part * (ayMax - ayMin);
double yMax = ayMin + (ty + 1) * part * (ayMax - ayMin);
const int iterations = 1000;
var hdrImage = new HDRImage(imageWidth, imageHeight);
double xInc = (xMax - xMin) / imageWidth;
double yInc = (yMax - yMin) / imageHeight;
double x = xMin; // Real part
for (int screenX = 0; screenX < imageWidth; screenX++)
{
double y = yMin; // Imaginary part
for (int screenY = 0; screenY < imageHeight; screenY++)
{
double x1 = 0, y1 = 0;
int iter = 0;
while (iter < iterations && x1 * x1 + y1 * y1 < 4)
//OH-was: while (iter < iterations && Math.Sqrt(x1 * x1 + y1 * y1) < 2)
{
iter++;
double xx = x1 * x1 - y1 * y1 + x;
y1 = 2 * x1 * y1 + y;
x1 = xx;
}
hdrImage.SetPixel(screenX, screenY, iter);
y += yInc;
}
x += xInc;
}
var bitmap = hdrImage.ToBitmap();
var memoryStream = new MemoryStream();
bitmap.Save(memoryStream, ImageFormat.Png);
memoryStream.Seek(0, SeekOrigin.Begin);
return memoryStream;
}
public string CacheId
{
get { return "MandelbrotTileServer"; }
}
public int MinZoom
{
get { return 0; }
}
public int MaxZoom
{
get { return 30; }
}
#endregion
}
}