Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
MandelbrotTopographyFractal.cs
1using System;
2using System.Numerics;
3using System.Text;
4using SkiaSharp;
10
12{
26 {
32 : base(new ScriptNode[] { z, f, dr, Palette, DimX, DimY },
33 new ArgumentType[] { ArgumentType.Scalar, ArgumentType.Scalar, ArgumentType.Scalar,
35 {
36 }
37
43 : base(new ScriptNode[] { z, f, dr, Palette, DimX },
44 new ArgumentType[] { ArgumentType.Scalar, ArgumentType.Scalar, ArgumentType.Scalar,
46 {
47 }
48
53 : base(new ScriptNode[] { z, f, dr, Palette },
54 new ArgumentType[] { ArgumentType.Scalar, ArgumentType.Scalar, ArgumentType.Scalar,
56 {
57 }
58
63 : base(new ScriptNode[] { z, f, dr },
64 new ArgumentType[] { ArgumentType.Scalar, ArgumentType.Scalar, ArgumentType.Scalar }, Start, Length, Expression)
65 {
66 }
67
71 public override string[] DefaultArgumentNames
72 {
73 get
74 {
75 return new string[] { "z", "f", "dr", "Palette", "DimX", "DimY" };
76 }
77 }
78
83 {
84 string ColorExpression = null;
85 SKColor[] Palette;
86 double rc, ic;
87 double dr;
88 int dimx, dimy;
89 int i, c;
90 object Obj;
91 ScriptNode fDef = null;
92 c = Arguments.Length;
93 i = 0;
94
95 Obj = Arguments[i++].AssociatedObjectValue;
96 if (Obj is Complex z)
97 {
98 rc = z.Real;
99 ic = z.Imaginary;
100 }
101 else
102 {
103 rc = Expression.ToDouble(Obj);
104 ic = Expression.ToDouble(Arguments[i++].AssociatedObjectValue);
105 }
106
107 if (i >= c)
108 throw new ScriptRuntimeException("Insufficient parameters in call to MandelbrotTopographyFractal().", this);
109
110 Obj = Arguments[i].AssociatedObjectValue;
111 if (Obj is ILambdaExpression f)
112 {
113 fDef = this.Arguments[i++];
114
115 if (f.NrArguments != 2)
116 throw new ScriptRuntimeException("Lambda expression in calls to MandelbrotTopographyFractal() must be of two variables (z,c).", this);
117 }
118 else
119 {
120 f = null;
121 fDef = null;
122
123 if (Obj is null)
124 i++;
125 }
126
127 dr = Expression.ToDouble(Arguments[i++].AssociatedObjectValue);
128
129 if (i < c && !(this.Arguments[i] is null) && Arguments[i] is ObjectVector)
130 {
131 ColorExpression = this.Arguments[i].SubExpression;
133 }
134 else
135 {
136 Palette = ColorModels.RandomLinearAnalogousHSL.CreatePalette(1024, 16, out int Seed, this, Variables);
137 ColorExpression = "RandomLinearAnalogousHSL(1024,16," + Seed.ToString() + ")";
138
139 if (i < c && this.Arguments[i] is null)
140 i++;
141 }
142
143 if (i < c)
144 dimx = (int)Expression.ToDouble(Arguments[i++].AssociatedObjectValue);
145 else
146 dimx = 320;
147
148 if (i < c)
149 dimy = (int)Expression.ToDouble(Arguments[i++].AssociatedObjectValue);
150 else
151 dimy = 200;
152
153 if (i < c)
154 {
155 throw new ScriptRuntimeException("Parameter mismatch in call to MandelbrotTopographyFractal(r,c,dr[,Palette][,dimx[,dimy]]).",
156 this);
157 }
158
159 if (dimx <= 0 || dimx > 5000 || dimy <= 0 || dimy > 5000)
160 throw new FractalImageSizeScriptException(this);
161
162 if (!(f is null))
163 {
164 return CalcMandelbrot(rc, ic, dr, f, Variables, Palette, dimx, dimy, this, this.FractalZoomScript,
165 new object[] { Palette, dimx, dimy, ColorExpression, fDef });
166 }
167 else
168 {
169 return CalcMandelbrot(Variables, rc, ic, dr, Palette, dimx, dimy, this, this.FractalZoomScript,
170 new object[] { Palette, dimx, dimy, ColorExpression, fDef });
171 }
172 }
173
174 private string FractalZoomScript(double r, double i, double Size, object State)
175 {
176 object[] Parameters = (object[])State;
177 int DimX = (int)Parameters[1];
178 int DimY = (int)Parameters[2];
179 string ColorExpression = (string)Parameters[3];
180 ScriptNode f = (ScriptNode)Parameters[4];
181
182 StringBuilder sb = new StringBuilder();
183
184 sb.Append("MandelbrotTopographyFractal((");
185 sb.Append(Expression.ToString(r));
186 sb.Append(',');
187 sb.Append(Expression.ToString(i));
188 sb.Append("),");
189
190 if (!(f is null))
191 sb.Append(f.SubExpression);
192
193 sb.Append(',');
194 sb.Append(Expression.ToString(Size / 4));
195
196 if (!string.IsNullOrEmpty(ColorExpression))
197 {
198 sb.Append(',');
199 sb.Append(ColorExpression);
200 }
201
202 sb.Append(',');
203 sb.Append(DimX.ToString());
204 sb.Append(',');
205 sb.Append(DimY.ToString());
206 sb.Append(')');
207
208 return sb.ToString();
209 }
210
214 public static FractalGraph CalcMandelbrot(Variables Variables, double rCenter, double iCenter, double rDelta,
215 SKColor[] Palette, int Width, int Height, ScriptNode Node, FractalZoomScript FractalZoomScript, object State)
216 {
217 double r0, i0, r1, i1;
218 double dr, di;
219 double r, i;
220 double zr, zi, zrt, zr2, zi2;
221 double aspect;
222 int x, y;
223 int n, N;
224
225 N = Palette.Length;
226
227 int Size = Width * Height;
228 int[] ColorIndex = new int[Size];
229 int Index = 0;
230
231 rDelta *= 0.5;
232 r0 = rCenter - rDelta;
233 r1 = rCenter + rDelta;
234
235 aspect = ((double)Width) / Height;
236
237 i0 = iCenter - rDelta / aspect;
238 i1 = iCenter + rDelta / aspect;
239
240 dr = (r1 - r0) / Width;
241 di = (i1 - i0) / Height;
242
243 for (y = 0, i = i0; y < Height; y++, i += di)
244 {
245 for (x = 0, r = r0; x < Width; x++, r += dr)
246 {
247 zr = r;
248 zi = i;
249
250 n = 0;
251 zr2 = zr * zr;
252 zi2 = zi * zi;
253
254 while (zr2 + zi2 < 9 && n < N)
255 {
256 n++;
257 zrt = zr2 - zi2 + r;
258 zi = 2 * zr * zi + i;
259 zr = zrt;
260
261 zr2 = zr * zr;
262 zi2 = zi * zi;
263 }
264
265 if (n >= N)
266 ColorIndex[Index++] = N;
267 else
268 ColorIndex[Index++] = n;
269 }
270 }
271
272 ColorIndex = FractalGraph.FindBoundaries(ColorIndex, Width, Height);
273
274 return new FractalGraph(Variables, FractalGraph.ToPixels(ColorIndex, Width, Height, Palette),
275 r0, i0, r1, i1, rDelta * 2, true, Node, FractalZoomScript, State);
276 }
277
281 public static FractalGraph CalcMandelbrot(double rCenter, double iCenter, double rDelta,
282 ILambdaExpression f, Variables Variables, SKColor[] Palette, int Width, int Height,
283 ScriptNode Node, FractalZoomScript FractalZoomScript, object State)
284 {
285 double r0, i0, r1, i1;
286 double dr, di;
287 double r, i, Mod;
288 double aspect;
289 int x, x2, y;
290 int n, N;
291
292 N = Palette.Length;
293
294 int Size = Width * Height;
295 int[] ColorIndex = new int[Size];
296
297 Complex z;
298 IElement[] P = new IElement[2];
299 int j, c;
300 IElement Obj;
301
302 rDelta *= 0.5;
303 r0 = rCenter - rDelta;
304 r1 = rCenter + rDelta;
305
306 aspect = ((double)Width) / Height;
307
308 i0 = iCenter - rDelta / aspect;
309 i1 = iCenter + rDelta / aspect;
310
311 dr = (r1 - r0) / Width;
312 di = (i1 - i0) / Height;
313
314 for (y = 0, i = i0; y < Height; y++, i += di)
315 {
316 Complex[] Row = new Complex[Width];
317 Complex[] Row0 = new Complex[Width];
318 int[] Offset = new int[Width];
319
320 c = Width;
321 for (x = 0, x2 = y * Width, r = r0; x < Width; x++, r += dr, x2++)
322 {
323 Row[x] = Row0[x] = new Complex(r, i);
324 Offset[x] = x2;
325 }
326
327 Variables v = new Variables();
328 Variables.CopyTo(v);
329
330 n = 0;
331 while (n < N && c > 0)
332 {
333 n++;
334 P[0] = new ComplexVector(Row);
335 P[1] = Expression.Encapsulate(Row0);
336 Obj = f.Evaluate(P, v);
337 Row = Obj.AssociatedObjectValue as Complex[];
338
339 if (Row is null)
340 throw new LambdaTypeScriptException(Obj.GetType(), Node);
341 else if (Row.Length != c)
342 throw new LambdaLengthScriptException(Row.Length, c, Node);
343
344 for (x = x2 = 0; x < c; x++)
345 {
346 z = Row[x];
347 j = Offset[x];
348
349 Mod = z.Magnitude;
350
351 if (Mod < 3)
352 {
353 if (x != x2)
354 {
355 Row[x2] = z;
356 Row0[x2] = Row0[x];
357 Offset[x2] = j;
358 }
359
360 x2++;
361 }
362 else
363 {
364 if (n >= N)
365 ColorIndex[j++] = N;
366 else
367 ColorIndex[j++] = n;
368 }
369 }
370
371 if (x2 < x)
372 {
373 Array.Resize(ref Row, x2);
374 Array.Resize(ref Row0, x2);
375 Array.Resize(ref Offset, x2);
376 c = x2;
377 }
378 }
379
380 if (c > 0)
381 {
382 for (x = 0; x < c; x++)
383 {
384 j = Offset[x];
385 ColorIndex[j] = N;
386 }
387 }
388
389 }
390
391 ColorIndex = FractalGraph.FindBoundaries(ColorIndex, Width, Height);
392
393 return new FractalGraph(Variables, FractalGraph.ToPixels(ColorIndex, Width, Height, Palette),
394 r0, i0, r1, i1, rDelta * 2, true, Node, FractalZoomScript, State);
395 }
396
400 public override string FunctionName => nameof(MandelbrotTopographyFractal);
401
402 }
403}
Class managing a script expression.
Definition: Expression.cs:41
static IElement Encapsulate(object Value)
Encapsulates an object.
Definition: Expression.cs:5241
static double ToDouble(object Object)
Converts an object to a double value.
Definition: Expression.cs:5110
static string ToString(double Value)
Converts a value to a string, that can be parsed as part of an expression.
Definition: Expression.cs:4760
static FractalGraph CalcMandelbrot(double rCenter, double iCenter, double rDelta, ILambdaExpression f, Variables Variables, SKColor[] Palette, int Width, int Height, ScriptNode Node, FractalZoomScript FractalZoomScript, object State)
TODO
override IElement Evaluate(IElement[] Arguments, Variables Variables)
TODO
MandelbrotTopographyFractal(ScriptNode z, ScriptNode f, ScriptNode dr, ScriptNode Palette, int Start, int Length, Expression Expression)
TODO
MandelbrotTopographyFractal(ScriptNode z, ScriptNode f, ScriptNode dr, ScriptNode Palette, ScriptNode DimX, int Start, int Length, Expression Expression)
TODO
MandelbrotTopographyFractal(ScriptNode z, ScriptNode f, ScriptNode dr, int Start, int Length, Expression Expression)
TODO
static FractalGraph CalcMandelbrot(Variables Variables, double rCenter, double iCenter, double rDelta, SKColor[] Palette, int Width, int Height, ScriptNode Node, FractalZoomScript FractalZoomScript, object State)
TODO
MandelbrotTopographyFractal(ScriptNode z, ScriptNode f, ScriptNode dr, ScriptNode Palette, ScriptNode DimX, ScriptNode DimY, int Start, int Length, Expression Expression)
TODO
Exception thrown if an image with invalid size is requested.
Exception thrown if a lambda expression with invalid type is encountered.
Exception thrown if a lambda expression with invalid type is encountered.
Defines a clickable fractal graph in the complex plane.
Definition: FractalGraph.cs:23
static PixelInformation ToPixels(double[] ColorIndex, int Width, int Height, SKColor[] Palette)
TODO
static SKColor[] ToPalette(ObjectVector Vector)
TODO
static double[] FindBoundaries(double[] ColorIndex, int Width, int Height)
TODO
Base class for multivariate funcions.
ScriptNode[] Arguments
Function arguments.
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
int Length
Length of expression covered by node.
Definition: ScriptNode.cs:101
string SubExpression
Sub-expression defining the node.
Definition: ScriptNode.cs:183
Expression Expression
Expression of which the node is a part.
Definition: ScriptNode.cs:177
int Start
Start position in script expression.
Definition: ScriptNode.cs:92
Collection of variables.
Definition: Variables.cs:25
void CopyTo(Variables Variables)
Copies available variables to another variable collection.
Definition: Variables.cs:334
Basic interface for all types of elements.
Definition: IElement.cs:21
Base interface for lambda expressions.
IElement Evaluate(IElement[] Arguments, Variables Variables)
Evaluates the lambda expression.
delegate string FractalZoomScript(double r, double i, double Size, object State)
Generates new script when zoomed.
ArgumentType
Type of parameter used in a function definition or a lambda definition.
Definition: IFunction.cs:9