Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
48
Task/Julia-set/C-sharp/julia-set-1.cs
Normal file
48
Task/Julia-set/C-sharp/julia-set-1.cs
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
using System.Drawing;
|
||||
// Note: You have to add the System.Drawing assembly
|
||||
// (right-click "references," Add Reference, Assemblies, Framework,
|
||||
// System.Drawing, OK)
|
||||
using System.Linq;
|
||||
|
||||
namespace RosettaJuliaSet
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
const int w = 800;
|
||||
const int h = 600;
|
||||
const int zoom = 1;
|
||||
const int maxiter = 255;
|
||||
const int moveX = 0;
|
||||
const int moveY = 0;
|
||||
const double cX = -0.7;
|
||||
const double cY = 0.27015;
|
||||
double zx, zy, tmp;
|
||||
int i;
|
||||
|
||||
var colors = (from c in Enumerable.Range(0, 256)
|
||||
select Color.FromArgb((c >> 5) * 36, (c >> 3 & 7) * 36, (c & 3) * 85)).ToArray();
|
||||
|
||||
var bitmap = new Bitmap(w, h);
|
||||
for (int x = 0; x < w; x++)
|
||||
{
|
||||
for (int y = 0; y < h; y++)
|
||||
{
|
||||
zx = 1.5 * (x - w / 2) / (0.5 * zoom * w) + moveX;
|
||||
zy = 1.0 * (y - h / 2) / (0.5 * zoom * h) + moveY;
|
||||
i = maxiter;
|
||||
while (zx * zx + zy * zy < 4 && i > 1)
|
||||
{
|
||||
tmp = zx * zx - zy * zy + cX;
|
||||
zy = 2.0 * zx * zy + cY;
|
||||
zx = tmp;
|
||||
i -= 1;
|
||||
}
|
||||
bitmap.SetPixel(x, y, colors[i]);
|
||||
}
|
||||
}
|
||||
bitmap.Save("julia-set.png");
|
||||
}
|
||||
}
|
||||
}
|
||||
50
Task/Julia-set/C-sharp/julia-set-2.cs
Normal file
50
Task/Julia-set/C-sharp/julia-set-2.cs
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
public struct CalculatedPoint
|
||||
{
|
||||
public int x;
|
||||
public int y;
|
||||
public int i;
|
||||
}
|
||||
|
||||
static void MultiThreaded()
|
||||
{
|
||||
const int w = 800;
|
||||
const int h = 600;
|
||||
const int zoom = 1;
|
||||
const int maxiter = 255;
|
||||
const int moveX = 0;
|
||||
const int moveY = 0;
|
||||
const double cX = -0.7;
|
||||
const double cY = 0.27015;
|
||||
|
||||
// Precalculate a pallette of 256 colors
|
||||
var colors = (from c in Enumerable.Range(0, 256)
|
||||
select Color.FromArgb((c >> 5) * 36, (c >> 3 & 7) * 36, (c & 3) * 85)).ToArray();
|
||||
|
||||
// The "AsParallel" below invokes PLINQ, making evaluation parallel using as many cores as
|
||||
// are available.
|
||||
var calculatedPoints = Enumerable.Range(0, w * h).AsParallel().Select(xy =>
|
||||
{
|
||||
double zx, zy, tmp;
|
||||
int x, y;
|
||||
int i = maxiter;
|
||||
y = xy / w;
|
||||
x = xy % w;
|
||||
zx = 1.5 * (x - w / 2) / (0.5 * zoom * w) + moveX;
|
||||
zy = 1.0 * (y - h / 2) / (0.5 * zoom * h) + moveY;
|
||||
while (zx * zx + zy * zy < 4 && i > 1)
|
||||
{
|
||||
tmp = zx * zx - zy * zy + cX;
|
||||
zy = 2.0 * zx * zy + cY;
|
||||
zx = tmp;
|
||||
i -= 1;
|
||||
}
|
||||
return new CalculatedPoint { x = x, y = y, i = i };
|
||||
});
|
||||
|
||||
// Bitmap is not multi-threaded, so main thread needs to read in the results as they
|
||||
// come in and plot the pixels.
|
||||
var bitmap = new Bitmap(w, h);
|
||||
foreach (CalculatedPoint cp in calculatedPoints)
|
||||
bitmap.SetPixel(cp.x, cp.y, colors[cp.i]);
|
||||
bitmap.Save("julia-set-multi.png");
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue