Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
NovaTopographyFractal.cs
1using System;
2using System.Numerics;
3using System.Text;
4using SkiaSharp;
10
12{
32 {
37 ScriptNode Palette, ScriptNode DimX, ScriptNode DimY, int Start, int Length, Expression Expression)
38 : base(new ScriptNode[] { r, i, dr, R, p, Palette, DimX, DimY },
39 new ArgumentType[] { ArgumentType.Scalar, ArgumentType.Scalar, ArgumentType.Scalar,
40 ArgumentType.Scalar, ArgumentType.Scalar, ArgumentType.Vector, ArgumentType.Scalar,
42 {
43 }
44
49 ScriptNode Palette, ScriptNode DimX, int Start, int Length, Expression Expression)
50 : base(new ScriptNode[] { r, i, dr, R, p, Palette, DimX },
51 new ArgumentType[] { ArgumentType.Scalar, ArgumentType.Scalar, ArgumentType.Scalar,
52 ArgumentType.Scalar, ArgumentType.Scalar, ArgumentType.Vector, ArgumentType.Scalar },
54 {
55 }
56
61 ScriptNode Palette, int Start, int Length, Expression Expression)
62 : base(new ScriptNode[] { r, i, dr, R, p, Palette },
63 new ArgumentType[] { ArgumentType.Scalar, ArgumentType.Scalar, ArgumentType.Scalar,
64 ArgumentType.Scalar, ArgumentType.Scalar, ArgumentType.Vector },
66 {
67 }
68
74 : base(new ScriptNode[] { r, i, dr, R, p },
75 new ArgumentType[] { ArgumentType.Scalar, ArgumentType.Scalar, ArgumentType.Scalar,
76 ArgumentType.Scalar, ArgumentType.Scalar },
78 {
79 }
80
84 public override string[] DefaultArgumentNames
85 {
86 get
87 {
88 return new string[] { "r", "i", "dr", "R", "p", "Palette", "DimX", "DimY" };
89 }
90 }
91
96 {
97 string ColorExpression = null;
98 SKColor[] Palette;
99 object Obj;
100 double rc, ic;
101 double dr;
102 double Rr, Ri, pr, pi;
103 int dimx, dimy;
104 int i, c;
105
106 rc = Expression.ToDouble(Arguments[0].AssociatedObjectValue);
107 ic = Expression.ToDouble(Arguments[1].AssociatedObjectValue);
108 dr = Expression.ToDouble(Arguments[2].AssociatedObjectValue);
109
110 if ((Obj = Arguments[3].AssociatedObjectValue) is Complex)
111 {
112 Complex z = (Complex)Obj;
113 Rr = z.Real;
114 Ri = z.Imaginary;
115 }
116 else
117 {
118 Rr = Expression.ToDouble(Arguments[3].AssociatedObjectValue);
119 Ri = 0;
120 }
121
122 if ((Obj = Arguments[4].AssociatedObjectValue) is Complex)
123 {
124 Complex z = (Complex)Obj;
125 pr = z.Real;
126 pi = z.Imaginary;
127 }
128 else
129 {
130 pr = Expression.ToDouble(Arguments[4].AssociatedObjectValue);
131 pi = 0;
132 }
133
134 c = Arguments.Length;
135 i = 5;
136
137 if (i < c && !(this.Arguments[i] is null) && Arguments[i] is ObjectVector)
138 {
139 ColorExpression = this.Arguments[i].SubExpression;
141 }
142 else
143 {
144 Palette = ColorModels.RandomLinearAnalogousHSL.CreatePalette(128, 4, out int Seed, this, Variables);
145 ColorExpression = "RandomLinearAnalogousHSL(128,4," + Seed.ToString() + ")";
146
147 if (i < c && this.Arguments[i] is null)
148 i++;
149 }
150
151 if (i < c)
152 dimx = (int)Expression.ToDouble(Arguments[i++].AssociatedObjectValue);
153 else
154 dimx = 320;
155
156 if (i < c)
157 dimy = (int)Expression.ToDouble(Arguments[i++].AssociatedObjectValue);
158 else
159 dimy = 200;
160
161 if (i < c)
162 {
163 throw new ScriptRuntimeException("Parameter mismatch in call to NovaTopographyFractal(r,c,dr,Coefficients[,Palette][,dimx[,dimy]]).",
164 this);
165 }
166
167 if (dimx <= 0 || dimx > 5000 || dimy <= 0 || dimy > 5000)
168 throw new FractalImageSizeScriptException(this);
169
170 return CalcNova(Variables, rc, ic, dr, Rr, Ri, pr, pi, Palette, dimx, dimy, this, this.FractalZoomScript,
171 new object[] { Palette, dimx, dimy, Rr, Ri, pr, pi, ColorExpression });
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 double Rr = (double)Parameters[3];
180 double Ri = (double)Parameters[4];
181 double pr = (double)Parameters[5];
182 double pi = (double)Parameters[6];
183 string ColorExpression = (string)Parameters[7];
184
185 StringBuilder sb = new StringBuilder();
186
187 sb.Append("NovaTopographyFractal(");
188 sb.Append(Expression.ToString(r));
189 sb.Append(',');
190 sb.Append(Expression.ToString(i));
191 sb.Append(',');
192 sb.Append(Expression.ToString(Size / 4));
193 sb.Append(',');
194 sb.Append(Expression.ToString(Rr));
195
196 if (Ri != 0)
197 {
198 sb.Append('+');
199 sb.Append(Expression.ToString(Ri));
200 sb.Append("*i");
201 }
202
203 sb.Append(',');
204 sb.Append(Expression.ToString(pr));
205
206 if (pi != 0)
207 {
208 sb.Append('+');
209 sb.Append(Expression.ToString(pi));
210 sb.Append("*i");
211 }
212
213 if (!string.IsNullOrEmpty(ColorExpression))
214 {
215 sb.Append(',');
216 sb.Append(ColorExpression);
217 }
218
219 sb.Append(',');
220 sb.Append(DimX.ToString());
221 sb.Append(',');
222 sb.Append(DimY.ToString());
223 sb.Append(')');
224
225 return sb.ToString();
226 }
227
231 public static FractalGraph CalcNova(Variables Variables, double rCenter, double iCenter, double rDelta, double Rr, double Ri,
232 double pr, double pi, SKColor[] Palette, int Width, int Height, ScriptNode Node, FractalZoomScript FractalZoomScript, object State)
233 {
234 double r0, i0, r1, i1;
235 double dr, di;
236 double r, i;
237 double zr, zi, zr2, zi2, zr3, zi3, zr4, zi4;
238 double aspect;
239 double Temp;
240 int x, y;
241 int n, N;
242 int index;
243 double lnz;
244 double argz;
245 double amp;
246 double phi;
247
248 N = Palette.Length;
249
250 rDelta *= 0.5;
251 r0 = rCenter - rDelta;
252 r1 = rCenter + rDelta;
253
254 aspect = ((double)Width) / Height;
255
256 i0 = iCenter - rDelta / aspect;
257 i1 = iCenter + rDelta / aspect;
258
259 dr = (r1 - r0) / Width;
260 di = (i1 - i0) / Height;
261
262 int Size = Width * Height;
263 int[] ColorIndex = new int[Size];
264 double Conv = 1e-10;
265 double Div = 1e10;
266
267 for (y = 0, i = i0, index = 0; y < Height; y++, i += di)
268 {
269 for (x = 0, r = r0; x < Width; x++, r += dr)
270 {
271 zr = r;
272 zi = i;
273
274 n = 0;
275 do
276 {
277 // f: z->z^p-1 = exp(p*ln(z))-1
278 // exp(a+ib)=exp(a)*(cos(b)+i*sin(b))
279 // ln(z)=ln|z|+i*arg(z)
280 // exp(p*ln(z))-1 =
281 // = exp((pr+i*pi)*(ln|z|+i*arg(z)))-1 =
282 // = exp(pr*ln|z|-pi*arg(z)+i*(pi*ln|z|+pr*arg(z)))-1 =
283 // = exp(pr*ln|z|-pi*arg(z))*(cos(pi*ln|z|+pr*arg(z))+i*sin(pi*ln|z|+pr*arg(z)))-1
284
285 lnz = Math.Log(Math.Sqrt(zr * zr + zi * zi));
286 argz = Math.Atan2(zi, zr);
287 amp = Math.Exp(pr * lnz - pi * argz);
288 phi = pi * lnz + pr * argz;
289
290 zr2 = amp * Math.Cos(phi) - 1;
291 zi2 = amp * Math.Sin(phi);
292
293 // f': z->p*z^(p-1) = p*exp((p-1)*ln(z)) =
294 // = (pr+i*pi)*exp((pr-1+i*pi)*(ln|z|+i*arg(z))) =
295 // = (pr+i*pi)*exp((pr-1)*ln|z|-pi*arg(z)+i*(pi*ln|z|+(pr-1)*arg(z))) =
296 // = (pr+i*pi)*exp((pr-1)*ln|z|-pi*arg(z))(sin(pi*ln|z|+(pr-1)*arg(z))+i*cos(pi*ln|z|+(pr-1)*arg(z))) =
297
298 amp = Math.Exp((pr - 1) * lnz - pi * argz);
299 phi = pi * lnz + (pr - 1) * argz;
300
301 zr3 = amp * Math.Cos(phi);
302 zi3 = amp * Math.Sin(phi);
303
304 Temp = pr * zr3 - pi * zi3;
305 zi3 = pr * zi3 + pi * zr3;
306 zr3 = Temp;
307
308 // f/f':
309
310 Temp = 1.0 / (zr3 * zr3 + zi3 * zi3);
311 zr4 = (zr2 * zr3 + zi2 * zi3) * Temp;
312 zi4 = (zi2 * zr3 - zr2 * zi3) * Temp;
313
314 Temp = Rr * zr4 - Ri * zi4;
315 zi4 = Ri * zr4 + Rr * zi4;
316 zr4 = Temp;
317
318 zr -= zr4;
319 zi -= zi4;
320
321 Temp = Math.Sqrt(zr4 * zr4 + zi4 * zi4);
322 }
323 while ((Temp > Conv) && (Temp < Div) && (n++ < N));
324
325 if (Temp < Conv && n < N)
326 ColorIndex[index++] = n;
327 else
328 ColorIndex[index++] = N;
329 }
330 }
331
332 ColorIndex = FractalGraph.FindBoundaries(ColorIndex, Width, Height);
333
334 return new FractalGraph(Variables, FractalGraph.ToPixels(ColorIndex, Width, Height, Palette),
335 r0, i0, r1, i1, rDelta * 2, true, Node, FractalZoomScript, State);
336 }
337
341 public override string FunctionName => nameof(NovaTopographyFractal);
342 }
343}
Class managing a script expression.
Definition: Expression.cs:41
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
NovaTopographyFractal(ScriptNode r, ScriptNode i, ScriptNode dr, ScriptNode R, ScriptNode p, int Start, int Length, Expression Expression)
TODO
NovaTopographyFractal(ScriptNode r, ScriptNode i, ScriptNode dr, ScriptNode R, ScriptNode p, ScriptNode Palette, ScriptNode DimX, int Start, int Length, Expression Expression)
TODO
NovaTopographyFractal(ScriptNode r, ScriptNode i, ScriptNode dr, ScriptNode R, ScriptNode p, ScriptNode Palette, int Start, int Length, Expression Expression)
TODO
static FractalGraph CalcNova(Variables Variables, double rCenter, double iCenter, double rDelta, double Rr, double Ri, double pr, double pi, SKColor[] Palette, int Width, int Height, ScriptNode Node, FractalZoomScript FractalZoomScript, object State)
TODO
NovaTopographyFractal(ScriptNode r, ScriptNode i, ScriptNode dr, ScriptNode R, ScriptNode p, ScriptNode Palette, ScriptNode DimX, ScriptNode DimY, int Start, int Length, Expression Expression)
TODO
override IElement Evaluate(IElement[] Arguments, Variables Variables)
TODO
Exception thrown if an image with invalid size is requested.
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
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
Basic interface for all types of elements.
Definition: IElement.cs:21
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