forked from microsoft/dotnet-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScalarFloat.cs
127 lines (118 loc) · 4.61 KB
/
ScalarFloat.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
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
using System;
using System.Threading.Tasks;
namespace Algorithms
{
// This class contains renderers that use scalar floats
internal class ScalarFloatRenderer : FractalRenderer
{
public ScalarFloatRenderer(Action<int, int, int> dp, Func<bool> abortFunc)
: base(dp, abortFunc)
{
}
protected const float limit = 4.0f;
// Render the fractal using a Complex data type on a single thread with scalar floats
public void RenderSingleThreadedWithADT(float xmin, float xmax, float ymin, float ymax, float step)
{
int yp = 0;
for (float y = ymin; y < ymax && !Abort; y += step, yp++)
{
int xp = 0;
for (float x = xmin; x < xmax; x += step, xp++)
{
ComplexFloat num = new ComplexFloat(x, y);
ComplexFloat accum = num;
int iters = 0;
float sqabs = 0f;
do
{
accum = accum.square();
accum += num;
iters++;
sqabs = accum.sqabs();
} while (sqabs < limit && iters < max_iters);
DrawPixel(xp, yp, iters);
}
}
}
// Render the fractal with no data type abstraction on a single thread with scalar floats
public void RenderSingleThreadedNoADT(float xmin, float xmax, float ymin, float ymax, float step)
{
int yp = 0;
for (float y = ymin; y < ymax && !Abort; y += step, yp++)
{
int xp = 0;
for (float x = xmin; x < xmax; x += step, xp++)
{
float accumx = x;
float accumy = y;
int iters = 0;
float sqabs = 0f;
do
{
float naccumx = accumx * accumx - accumy * accumy;
float naccumy = 2.0f * accumx * accumy;
accumx = naccumx + x;
accumy = naccumy + y;
iters++;
sqabs = accumx * accumx + accumy * accumy;
} while (sqabs < limit && iters < max_iters);
DrawPixel(xp, yp, iters);
}
}
}
// Render the fractal using a Complex data type on a single thread with scalar floats
public void RenderMultiThreadedWithADT(float xmin, float xmax, float ymin, float ymax, float step)
{
Parallel.For(0, (int)(((ymax - ymin) / step) + .5f), (yp) =>
{
if (Abort)
return;
float y = ymin + step * yp;
int xp = 0;
for (float x = xmin; x < xmax; x += step, xp++)
{
ComplexFloat num = new ComplexFloat(x, y);
ComplexFloat accum = num;
int iters = 0;
float sqabs = 0f;
do
{
accum = accum.square();
accum += num;
iters++;
sqabs = accum.sqabs();
} while (sqabs < limit && iters < max_iters);
DrawPixel(xp, yp, iters);
}
});
}
// Render the fractal with no data type abstraction on multiple threads with scalar floats
public void RenderMultiThreadedNoADT(float xmin, float xmax, float ymin, float ymax, float step)
{
Parallel.For(0, (int)(((ymax - ymin) / step) + .5f), (yp) =>
{
if (Abort)
return;
float y = ymin + step * yp;
int xp = 0;
for (float x = xmin; x < xmax; x += step, xp++)
{
float accumx = x;
float accumy = y;
int iters = 0;
float sqabs = 0f;
do
{
float naccumx = accumx * accumx - accumy * accumy;
float naccumy = 2.0f * accumx * accumy;
accumx = naccumx + x;
accumy = naccumy + y;
iters++;
sqabs = accumx * accumx + accumy * accumy;
} while (sqabs < limit && iters < max_iters);
DrawPixel(xp, yp, iters);
}
});
}
}
}