Simple Mandelbrot renderer runs out of memory - c#

Ive been trying to make an simple mandelbrot renderer just to get myself into c# and forms, but when i render the image the program runs out of memory sometimes.
The memory builds up to 2GB and then crashes.
But sometimes it builds up i a jigsaw pattern like this and dont crash:
http://puu.sh/l2ri9/2fcd47e6d7.png
==============Code to render================
Renderer.CreateGraphics().Clear(Color.White);
double minR = System.Convert.ToDouble(MinR.Value);
double maxR = System.Convert.ToDouble(MaxR.Value);
double minI = System.Convert.ToDouble(MaxI.Value);
double maxI = System.Convert.ToDouble(MinI.Value);
int maxN = System.Convert.ToInt32(Iterations.Value);
SolidBrush MandelColor = new SolidBrush(Color.Red);
for (int y = 0; y < Renderer.Height; y++)
{
for (int x = 0; x < Renderer.Width; x++)
{
double cr = fitInRRange(x, Renderer.Width, minR, maxR);
double ci = fitInIRange(y, Renderer.Height, minI, maxI);
int n = findMandelbrot(cr, ci, maxN);
double t = ((n + 0.0) / (maxN + 0.0));
MandelColor.Color = Color.FromArgb(System.Convert.ToInt32(9 * (1 - t) * t * t * t * 255), System.Convert.ToInt32(15 * (1 - t) * (1 - t) * t * t * 255), System.Convert.ToInt32(8.5 * (1 - t) * (1 - t) * (1 - t) * t * 255));
Renderer.CreateGraphics().FillRectangle(MandelColor, x, y, 1, 1);
}
}
===Link To Github page===
https://github.com/JimAlexBerger/MandelbrotProject

Why not move Renderer.CreateGraphics() outside of the loop? The memory leak is probably caused by redundantly calling Renderer.CreateGraphics() without calling IDisposable.Dispose() on the Graphics object.
double minR = System.Convert.ToDouble(MinR.Value);
double maxR = System.Convert.ToDouble(MaxR.Value);
double minI = System.Convert.ToDouble(MaxI.Value);
double maxI = System.Convert.ToDouble(MinI.Value);
int maxN = System.Convert.ToInt32(Iterations.Value);
SolidBrush MandelColor = new SolidBrush(Color.Red);
using(var graphics = Renderer.CreateGraphics())
{
graphics.Clear(Color.White);
for (int y = 0; y < Renderer.Height; y++)
{
for (int x = 0; x < Renderer.Width; x++)
{
double cr = fitInRRange(x, Renderer.Width, minR, maxR);
double ci = fitInIRange(y, Renderer.Height, minI, maxI);
int n = findMandelbrot(cr, ci, maxN);
double t = ((n + 0.0) / (maxN + 0.0));
MandelColor.Color = Color.FromArgb(System.Convert.ToInt32(9 * (1 - t) * t * t * t * 255), System.Convert.ToInt32(15 * (1 - t) * (1 - t) * t * t * 255), System.Convert.ToInt32(8.5 * (1 - t) * (1 - t) * (1 - t) * t * 255));
gfx.FillRectangle(MandelColor, x, y, 1, 1);
}
}
}

Related

Make circle in C# with only pen tool, without using ellipse method

The code i have made is hard coded and i want it to convert it into circle any snippet i can add or something
The code is in C sharp, The output is like the rectangle which i have to convert it into a circle
private void pictureBox1_Click(object sender, EventArgs e)
{
int length = 100;
int flag = 0;
int flag2 = 0;
int flag3 = 0;
Pen p = new Pen(Color.Red, 4);
Graphics g = pictureBox1.CreateGraphics();
Brush redBrush = new SolidBrush(Color.Red);
for (int i = 0; i < 20; i++)
{
if(i==0 || i<10)
{
g.DrawLine(p, 622 - 10 * i, 229+10*i, 623 - 10 * i, 229+10*i);
}
if(i==10)
{
flag = 1;
}
if(flag==1)
{
g.DrawLine(p, 622 - 10 * i, 419 - 10 * i, 623 - 10 * i, 419-10*i);
flag2 = 1;
}
if(flag2 == 1)
{
g.DrawLine(p, 622 - 10 * i, 29+10*i, 623 - 10 * i, 29+10*i);
flag3 = 1;
}
if (flag3 == 1)
{
g.DrawLine(p, 432 + 10 * i, 29+10*i, 433 + 10 * i, 29 + 10 *i);
}
}
There is a built-in function for this. Use g.DrawEllipse() instead.
You can do this
void DrawCircle(Graphics g, Pen p, Point centre, double radius=20, int sides = 360)
{
var angle = 2 * Math.PI / sides;
for (int i = 0; i < sides; i++)
{
Point from = new Point((int)(radius * Math.Sin(i * angle) + centre.X), (int)(radius * Math.Cos(i * angle) + centre.Y));
Point to = new Point((int)(radius * Math.Sin((i+1) * angle) + centre.X), (int)(radius * Math.Cos((i+1) * angle) + centre.Y));
g.DrawLine(p, from, to);
}
}
and to use
DrawCircle(g, p, new Point(100, 100), 50, 8); // 8 sides, an octagon
Increase the number of sides to make it more accurate.
Alternatively,
g.DrawEllipse(p, (float)(centre.X-radius), (float)(centre.Y-radius), (float)radius*2, (float)radius*2);

Problem with different outcome with DCT transformation on image while converting C++ to C#

I tried to write APP that would make DCT-2 transformation on image fragment and then transform back with inverse DCT-2. I have found code in c++/opencv that I tried to convert to C#, but somehow, I have different outcome at some point. Here is the code i tried to convert:
for (unsigned v = 0; v < BLOCK_SIZE; ++v)
{
for (unsigned u = 0; u < BLOCK_SIZE; ++u)
{
const double cu = (u == 0) ? 1.0 / sqrt(2) : 1.0;
const double cv = (v == 0) ? 1.0 / sqrt(2) : 1.0;
double dctCoeff = 0;
for (unsigned y = 0; y < BLOCK_SIZE; ++y)
{
for (unsigned x = 0; x < BLOCK_SIZE; ++x)
{
double uCosFactor = cos((double)(2 * x + 1) * M_PI * (double)u / (2 * (double) BLOCK_SIZE));
double vCosFactor = cos((double)(2 * y + 1) * M_PI * (double)v / (2 * (double) BLOCK_SIZE));
double pixel = (double)(lenaNoseGrey.at<unsigned char>(cv::Point(x,y)));
dctCoeff += pixel * uCosFactor * vCosFactor;
}
}
dctCoeff *= (2 / (double) BLOCK_SIZE) * cu * cv;
lenaNoseDct.at<double>(cv::Point(u,v)) = dctCoeff;
}
}
And here is mine:
for (int v = 0; v < BLOCK_SIZE; ++v)
{
for (int u = 0; u < BLOCK_SIZE; ++u)
{
double cu = (u == 0) ? 1.0 / Math.Sqrt(2) : 1.0;
double cv = (v == 0) ? 1.0 / Math.Sqrt(2) : 1.0;
double dctCoeff = 0;
double dctCoeffAlpha = 0;
for (int y1 = 0; y1 < BLOCK_SIZE; ++y1)
{
for (int x1 = 0; x1 < BLOCK_SIZE; ++x1)
{
double uCosFactor = Math.Cos((2 * x1 + 1) * Math.PI * u / (2 * (double)BLOCK_SIZE));
double vCosFactor = Math.Cos((2 * y1 + 1) * Math.PI * v / (2 * (double)BLOCK_SIZE));
double pixel = (double)bitmapaWy1.GetPixel((x1 + 284), (y1 + 313)).R;
double pixelalpha = (double)bitmapaWy1.GetPixel((x1 + 284), (y1 + 313)).A;
dctCoeff += pixel * uCosFactor * vCosFactor;
//dctCoeffAlpha += pixelalpha * uCosFactor * vCosFactor;
dctCoeffAlpha = pixelalpha;
}
}
dctCoeffAlpha *= (2 / (double)BLOCK_SIZE) * cu * cv;
dctCoeff *= (2 / (double)BLOCK_SIZE) * cu * cv;
macierz[u, v] = dctCoeff;
}
}
I have different outcome in my matrix, but when i convert matrix from the C++ code above with my inverse code, it works well.
Can you find what have I done wrong? One difference I can spot is with getpixel method, but it is performed on exact same grayscaled image and image fragment that C++ code was performed.
When i inverse my matrix to image again, I can see image, but it has lot of random pixels that are too white, or too black.
Problem is solved, the issue was too much code, actual code i posted works, problem was code I had after it, which was changing "dctcoeff" to byte and it was inside loop, so value of dct didnt reset to 0;
int BLOCK_SIZE = 16;
double[,] macierz = new double[16, 16];
for (int v = 0; v < BLOCK_SIZE; ++v)
{
for (int u = 0; u < BLOCK_SIZE; ++u)
{
double cu = (u == 0) ? 1.0 / Math.Sqrt(2) : 1.0;
double cv = (v == 0) ? 1.0 / Math.Sqrt(2) : 1.0;
double dctCoeff = 0;
for (int y1 = 0; y1 < BLOCK_SIZE; ++y1)
{
for (int x1 = 0; x1 < BLOCK_SIZE; ++x1)
{
double uCosFactor = Math.Cos((2 * x1 + 1) * Math.PI * u / (2 * (double)BLOCK_SIZE));
double vCosFactor = Math.Cos((2 * y1 + 1) * Math.PI * v / (2 * (double)BLOCK_SIZE));
double pixel = bitmapaWy1.GetPixel((x1 + 284), (y1 + 313)).R;
dctCoeff += pixel * uCosFactor * vCosFactor;
//dctCoeffAlpha += pixelalpha * uCosFactor * vCosFactor;
}
}
dctCoeff *= (2 / (double)BLOCK_SIZE) * cu * cv;
macierz[u, v] = dctCoeff;
}
}
This code works like a charm. I hope some good soul will use it, becouse i spent about 20h into making it work.
This is clearly a precision issue.
Most likely the problem is at the following line:
macierz[u, v] = dctCoeff;
I also have doubt in:
double pixel = (double)bitmapaWy1.GetPixel((x1 + 284), (y1 + 313)).R;
double pixelalpha = (double)bitmapaWy1.GetPixel((x1 + 284), (y1 + 313)).A;
dctCoeff += pixel * uCosFactor * vCosFactor;
//dctCoeffAlpha += pixelalpha * uCosFactor * vCosFactor;
dctCoeffAlpha = pixelalpha;

Convert CPU Parallel.For to GPU Gpu.For

I created a CPU Parallel.For loop in my code as follow:
Vector2[] p;
Vector2[] pnew;
Vector2[] v;
float[] m;
Parallel.For(0, N, i =>
{
double dx;
double dy;
double invr;
double invr3;
double f;
double ax;
double ay;
double eps = 0.02;
ax = 0;
ay = 0;
for (int j = 0; j < N; j++)
{
dx = p[j].X - p[i].X;
dy = p[j].Y - p[i].Y;
invr =1.0 / Math.Sqrt(dx * dx + dy * dy + eps);
invr3 = invr * invr * invr;
f = m[j] * m[i] * invr3;
ax += f * dx;
ay += f * dy;
}
pnew[i].X = (float)(p[i].X + dt * v[i].X + 0.5 * dt * dt * ax); /* save new position of particle "i" */
pnew[i].Y = (float)(p[i].Y + dt * v[i].Y + 0.5 * dt * dt * ay);
v[i].X += dt * (float)ax; /* update velocity of particle "i" */
v[i].Y += dt * (float)ay;
});
The above code works fine and run on my CPU cores.
Want I want to do it to convert this code to a GPU For loop using "Alea GPU" library. So i tried the following:
Vector2[] p;
Vector2[] pnew;
Vector2[] v;
float[] m;
[GpuManaged]
public void calculForceGPU()
{
Gpu.Default.For(0, N + 1, i =>
{
double dx;
double dy;
double invr;
double invr3;
double f;
double ax;
double ay;
double eps = 0.02;
ax = 0;
ay = 0;
for (int j = 0; j < N; j++)
{
dx = p[j].X - p[i].X;
dy = p[j].Y - p[i].Y;
invr = 1.0 / Math.Sqrt(dx * dx + dy * dy + eps);
invr3 = invr * invr * invr;
f = m[j] * m[i] * invr3;
ax += f * dx;
ay += f * dy;
}
pnew[i].X = (float)(p[i].X + dt * v[i].X + 0.5 * dt * dt * ax); /* save new position of particle "i" */
pnew[i].Y = (float)(p[i].Y + dt * v[i].Y + 0.5 * dt * dt * ay);
v[i].X += dt * (float)ax; /* update velocity of particle "i" */
v[i].Y += dt * (float)ay;
});
}
You can see that it is the exact same code as above but with Parallel.For changed for Gpu.Default.For. But when I run it I get the following error:
i32 is not struct type.
Source location stack:
-> in C:\Users\...\Simulation.cs(628,21-628,42)
-> at ....Simulation.[Void <calculForceGPU>b__36_0(Int32)]
-> at Alea.Parallel.Device.DeviceFor.[Void Kernel(Int32, Int32,
System.Action`1[System.Int32])]
-> at defining runtime32 (sm52,32bit)
Loading method as kernel:
-> Method: Alea.Parallel.Device.DeviceFor.[Void Kernel(Int32, Int32,
System.Action`1[System.Int32])]
-> InstanceOpt: <None>
-> Argument.#0: 0
-> Argument.#1: 1025
-> Argument.#2: System.Action`1[System.Int32]
Getting or loading method as kernel:
-> Method: Alea.Parallel.Device.DeviceFor.[Void Kernel(Int32, Int32,
System.Action`1[System.Int32])]
-> InstanceOpt: <None>
-> Argument.#0: 0
-> Argument.#1: 1025
-> Argument.#2: System.Action`1[System.Int32]
I am not sure how to solve this error. Any help would be appreciated.
Update on what I tried after comments by NineBerry:
So it turns out the problem might be the Vector2 type because it might use properties. So I created my own struct which use Fields like so:
struct Vector2Struct
{
public float X;
public float Y;
public Vector2Struct(float x, float y)
{
X = x;
Y = y;
}
}
Vector2Struct[] p;
Vector2Struct[] pnew;
Vector2Struct[] v;
float[] m;
The rest of the code is pretty much the same as before. But I still get the same "i32 is not struct type." error.
Same error if I ditch all struct and use arrays of float instead:
float[] m;
float[] pX;
float[] pY;
float[] pnewX;
float[] pnewY;
float[] vX;
float[] vY;
Code dump as per comment. Creating a new instance of the class should make it run. You will need to install nuget ALEA and ALEA.FODY . Also I think you need FSharp.Core to run Alea
using System;
using Alea;
using Alea.Parallel;
namespace GalaxyTest
{
public class Simulation
{
// param
object[] param;
// masses
float[] m;
float[] pX;
float[] pY;
float[] pnewX;
float[] pnewY;
float[] vX;
float[] vY;
// data
static int N;
double ratioPM;
double ratioMasse;
int Np;
int Nm;
int distribution;
int simulType;
float dt = 0.01F;
double eps = 0.02;
float Rrp = 1;
float Rrm = 10;
public Simulation() //Constructor
{
Initializer();
updatePointGPUAlea();
}
private void Initializer()
{
// Settings
N = 1024;
ratioPM = 0.25;
ratioMasse = 1;
Np = 256;
Nm = 769;
distribution = 0;
simulType = 1;
// vector Initialisation
pX = new float[N];
pY = new float[N];
pnewX = new float[N];
pnewY = new float[N];
vX = new float[N];
vY = new float[N];
m = new float[N];
// compute masses
for (int i = 0; i < Np; i++)
{
m[i] = 1;
}
for (int i = Np; i < Nm; i++)
{
m[i] = -1 * (float)ratioMasse;
}
Random r = new Random();
double R;
double teta;
double Rp;
double Rn;
float signe1, signe2, signe3, signe4;
// Init pos = random shell
for (int i = 0; i < N; i++)
{
Rp = 2.61;
Rn = 45;
teta = r.NextDouble() * 2 * Math.PI;
signe1 = Math.Sign(r.NextDouble() - 0.5);
signe2 = Math.Sign(r.NextDouble() - 0.5);
signe3 = Math.Sign(r.NextDouble() - 0.5);
signe4 = Math.Sign(r.NextDouble() - 0.5);
if (m[i] > 0)
{
pX[i] = (float)(Rp * Math.Cos(teta)) + 400 / 2;
pY[i] = (float)(Rp * Math.Sin(teta)) + 400 / 2;
vX[i] = (float)(r.NextDouble() * Rrp * signe1 + Math.Sqrt(Np) / 12 * 3 * Math.Sin(teta) * (0.4 - 1 / Math.Sqrt(10 + Rp)) * 3);
vY[i] = (float)(r.NextDouble() * Rrp * signe2 - Math.Sqrt(Np) / 12 * 3 * Math.Cos(teta) * (0.4 - 1 / Math.Sqrt(10 + Rp)) * 3);
}
else
{
pX[i] = (float)(Rn * Math.Cos(teta)) + 400 / 2;
pY[i] = (float)(Rn * Math.Sin(teta)) + 400 / 2;
vX[i] = (float)r.NextDouble() * Rrm * signe3;
vY[i] = (float)r.NextDouble() * Rrm * signe4;
}
}
}
public void updatePointGPUAlea()
{
calculForceGPU();
for (int i = 0; i < N; i++)
{
// Update de la position
pX[i] = pnewX[i];
pY[i] = pnewY[i];
}
}
[GpuManaged]
public void calculForceGPU()
{
Gpu.Default.For(0, N + 1, i =>
{
double dx;
double dy;
double invr;
double invr3;
double f;
double ax;
double ay;
ax = 0;
ay = 0;
for (int j = 0; j < N; j++)
{
dx = pX[j] - pX[i];
dy = pY[j] - pY[i];
invr = 1.0 / Math.Sqrt(dx * dx + dy * dy + eps);
invr3 = invr * invr * invr;
f = m[j] * m[i] * invr3;
ax += f * dx;
ay += f * dy;
}
pnewX[i] = (float)(pX[i] + dt * vX[i] + 0.5 * dt * dt * ax); /* save new position of particle "i" */
pnewY[i] = (float)(pY[i] + dt * vY[i] + 0.5 * dt * dt * ay);
vX[i] += dt * (float)ax; /* update velocity of particle "i" */
vY[i] += dt * (float)ay;
});
}
}
}
This is probably because you use the X and Y properties of the Vector2 struct type. Alea does not support properties according to the documentation:
Note that the current version of Alea GPU only supports fields in structs but not properties
See also Alea: "i32 is not struct type

Draw a curve of the Cycloid

Good time of day. It is necessary to draw a graph of the cycloids, the radius is specified by the user. Managed to paint only half of the period, I do not understand what it is.
Code'm applying.
My function:
return r * Math.Acos((r - y) / r) - Math.Sqrt(2 * r * y - Math.Pow(y, 2));
And my Main part:
GraphPane pane = zedGraph.GraphPane;
pane.CurveList.Clear();
PointPairList list = new PointPairList();
double r = 20;
double xmax = 50;
for (double y = 0; y < xmax; y+=0.5)
{
list.Add(CountIt(y, r), y);
}
LineItem myCurve = pane.AddCurve("Cycloid", list, Color.Red, SymbolType.None);
zedGraph.AxisChange();
zedGraph.Invalidate();
Apparently it is necessary to consider the situation when y>2r, or that should be several possible x? I do not understand how to get out of the situation.
It is simpler to use parametric equations (with t=0..2*Pi for one period):
x = r * (t - sin(t))
y = r * (1 - cos(t))
If you want to continue using Cartesian equation x(y) - change limit for y to correct value 2 * r and mirror the second part like this:
for (double y = 0; y < 2 * r; y+=0.5)
{
list.Add(CountIt(y, r), y);
}
for (double y = 2 * r; y >= 0; y-=0.5)
{
list.Add(2 * Pi * r - CountIt(y, r), y);
}
If you need to draw few periods, limiting xmax:
p = 0;
while true do
{
for (double y = 0; y < 2 * r; y+=0.5)
{ x = 2 * Pi * r * p + CountIt(y, r);
if (x > xmax)
break;
list.Add(x, y);
}
for (double y = 2 * r; y >= 0; y-=0.5)
{
x = 2 * Pi * r * (p + 1) - CountIt(y, r);
if (x > xmax)
break;
list.Add(x, y);
}
p++;
}

Draw Cosx with specific period width

how can i draw a period of Cosx with width =100 and height 50?
my recent code is very hard to control them :(
// this to find y with each x to draw Cos
for (int i = 0, j = 0; i < 50; i += 1, j++)
{
int y = (int)((Math.Cos((double)i * height/10F * Math.PI / cy) + 1.0) * (cx - 1) / widtd/10F);
poi.SetValue(new Point(i, y),j); // poi is an aray of point
}
Based on OP's last comment I assume "width" means a single period. Therefore to calculate the points of a cosine function with amplitude amp and a period of period looks like this:
int amp = 50, period = 100;
Point[] poi = new Point[period];
for (int x = 0; x < period; x++)
{
int y = (int)(amp * Math.Cos(x * 2 * Math.PI * (1.0 / period)));
poi[x] = new Point(x, y);
}
Note that this is a "1:1" calculation, i.e. one point is one pixel.
I found solution here:
string filename = #"D:\test.bmp";
int width = 200;
int height = 300;
Bitmap b = new Bitmap(width, height);
for (int i = 0; i < width; i++)
{
int y = (int)((Math.Cos((double)i * 2.0 * Math.PI / width) + 1.0) * (height - 1) / 2.0);
b.SetPixel(i, y, Color.Black);
}
b.Save(filename);

Categories