Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
HalleyBuilderFractal.cs
1using System.Numerics;
2using System.Text;
3using SkiaSharp;
9
11{
20 {
25 ScriptNode Palette, ScriptNode DimX, ScriptNode DimY, int Start, int Length, Expression Expression)
26 : base(new ScriptNode[] { z, dr, R, Coefficients, Palette, DimX, DimY },
27 new ArgumentType[] { ArgumentType.Scalar, ArgumentType.Scalar, ArgumentType.Scalar,
28 ArgumentType.Normal, ArgumentType.Vector, ArgumentType.Scalar, ArgumentType.Scalar}, Start, Length, Expression)
29 {
30 }
31
36 ScriptNode Palette, ScriptNode DimX, int Start, int Length, Expression Expression)
37 : base(new ScriptNode[] { z, dr, R, Coefficients, Palette, DimX },
38 new ArgumentType[] { ArgumentType.Scalar, ArgumentType.Scalar, ArgumentType.Scalar,
40 {
41 }
42
47 ScriptNode Palette, int Start, int Length, Expression Expression)
48 : base(new ScriptNode[] { z, dr, R, Coefficients, Palette },
49 new ArgumentType[] { ArgumentType.Scalar, ArgumentType.Scalar, ArgumentType.Scalar,
51 {
52 }
53
59 : base(new ScriptNode[] { z, dr, R, Coefficients },
60 new ArgumentType[] { ArgumentType.Scalar, ArgumentType.Scalar, ArgumentType.Scalar,
62 {
63 }
64
70 : base(new ScriptNode[] { z, dr, R },
71 new ArgumentType[] { ArgumentType.Scalar, ArgumentType.Scalar, ArgumentType.Scalar },
73 {
74 }
75
79 public override string[] DefaultArgumentNames
80 {
81 get
82 {
83 return new string[] { "z", "dr", "R", "Coefficients", "Palette", "DimX", "DimY" };
84 }
85 }
86
91 {
92 string ColorExpression = null;
93 SKColor[] Palette;
94 double[] Coefficients = null;
95 Complex[] CoefficientsZ = null;
96 double rc, ic;
97 double dr;
98 Complex R;
99 int dimx, dimy;
100 int c = Arguments.Length;
101 int i = 0;
102 object Obj;
103
104 Obj = Arguments[i++].AssociatedObjectValue;
105 if (Obj is Complex z)
106 {
107 rc = z.Real;
108 ic = z.Imaginary;
109 }
110 else
111 {
112 rc = Expression.ToDouble(Obj);
113 ic = Expression.ToDouble(Arguments[i++].AssociatedObjectValue);
114 }
115
116 if (i >= c)
117 throw new ScriptRuntimeException("Insufficient parameters in call to HalleyBuilderFractal().", this);
118
119 dr = Expression.ToDouble(Arguments[i++].AssociatedObjectValue);
120
121 if (i < c && ((Obj = Arguments[i].AssociatedObjectValue) is double || Obj is Complex))
122 {
123 R = Expression.ToComplex(Obj);
124 i++;
125 }
126 else
127 {
128 R = Complex.One;
129
130 if (i < c && this.Arguments[i] is null)
131 i++;
132 }
133
134 if (i < c && Arguments[i] is DoubleVector)
135 {
136 Coefficients = (double[])Arguments[i++].AssociatedObjectValue;
137
138 int j, d = Coefficients.Length;
139
140 CoefficientsZ = new Complex[d];
141 for (j = 0; j < d; j++)
142 CoefficientsZ[j] = new Complex(Coefficients[j], 0);
143 }
144 else if (i < c && Arguments[i] is ComplexVector)
145 CoefficientsZ = (Complex[])Arguments[i++].AssociatedObjectValue;
146 /*else if (i < c && Parameters[i] is RealPolynomial)
147 Coefficients = ((RealPolynomial)Arguments[i++].AssociatedObjectValue).Coefficients;
148 else if (i < c && Parameters[i] is ComplexPolynomial)
149 CoefficientsZ = ((ComplexPolynomial)Arguments[i++].AssociatedObjectValue).Coefficients;*/
150 else if (i < c && Arguments[i] is IVector)
151 {
152 IVector Vector = (IVector)Arguments[i++];
153 int j, d = Vector.Dimension;
154
155 CoefficientsZ = new Complex[d];
156 for (j = 0; j < d; j++)
157 CoefficientsZ[j] = Expression.ToComplex(Vector.GetElement(j).AssociatedObjectValue);
158 }
159 else
160 CoefficientsZ = System.Array.Empty<Complex>();
161
162 if (i < c && !(this.Arguments[i] is null) && Arguments[i] is ObjectVector)
163 {
164 ColorExpression = this.Arguments[i].SubExpression;
166 }
167 else
168 {
169 Palette = ColorModels.RandomLinearAnalogousHSL.CreatePalette(128, 4, out int Seed, this, Variables);
170 ColorExpression = "RandomLinearAnalogousHSL(128,4," + Seed.ToString() + ")";
171
172 if (i < c && this.Arguments[i] is null)
173 i++;
174 }
175
176 if (i < c)
177 dimx = (int)Expression.ToDouble(Arguments[i++].AssociatedObjectValue);
178 else
179 dimx = 320;
180
181 if (i < c)
182 dimy = (int)Expression.ToDouble(Arguments[i++].AssociatedObjectValue);
183 else
184 dimy = 200;
185
186 if (i < c)
187 {
188 throw new ScriptRuntimeException("Parameter mismatch in call to HalleyBuilderFractal(z,dr[,R][,Coefficients][,Palette][,dimx[,dimy]]).",
189 this);
190 }
191
192 if (dimx <= 0 || dimx > 5000 || dimy <= 0 || dimy > 5000)
193 throw new FractalImageSizeScriptException(this);
194
195 return HalleyFractal.CalcHalley(Variables, rc, ic, dr, R, CoefficientsZ, Palette, dimx, dimy, this, this.FractalZoomScript,
196 new object[] { Palette, dimx, dimy, R, CoefficientsZ, ColorExpression, rc, ic });
197 }
198
199 private string FractalZoomScript(double r, double i, double Size, object State)
200 {
201 object[] Parameters = (object[])State;
202 int DimX = (int)Parameters[1];
203 int DimY = (int)Parameters[2];
204 Complex R = (Complex)Parameters[3];
205 Complex[] CoefficientsZ = (Complex[])Parameters[4];
206 string ColorExpression = (string)Parameters[5];
207 double rc = (double)Parameters[6];
208 double ic = (double)Parameters[7];
209 Complex z0 = new Complex(r, i);
210 int j;
211
212 int c = CoefficientsZ.Length;
213 while (c > 1 && CoefficientsZ[c - 1].Equals(0))
214 c--;
215
216 Complex[] C2;
217
218 if (c < 1)
219 {
220 C2 = new Complex[2];
221 C2[0] = -z0;
222 C2[1] = Complex.One;
223 }
224 else
225 {
226 C2 = new Complex[c + 1];
227 C2[0] = Complex.Zero;
228 for (j = 0; j < c; j++)
229 C2[j + 1] = CoefficientsZ[j];
230
231 for (j = 0; j < c; j++)
232 C2[j] -= z0 * CoefficientsZ[j];
233 }
234
235 StringBuilder sb = new StringBuilder();
236
237 sb.Append("HalleyBuilderFractal((");
238 sb.Append(Expression.ToString(rc));
239 sb.Append(',');
240 sb.Append(Expression.ToString(ic));
241 sb.Append("),");
242 sb.Append(Expression.ToString(Size));
243 sb.Append(',');
244 sb.Append(Expression.ToString(R));
245 sb.Append(',');
246 sb.Append(Expression.ToString(C2));
247
248 if (!string.IsNullOrEmpty(ColorExpression))
249 {
250 sb.Append(',');
251 sb.Append(ColorExpression);
252 }
253
254 sb.Append(',');
255 sb.Append(DimX.ToString());
256 sb.Append(',');
257 sb.Append(DimY.ToString());
258 sb.Append(')');
259
260 return sb.ToString();
261 }
262
266 public override string FunctionName => nameof(HalleyBuilderFractal);
267 }
268}
Class managing a script expression.
Definition: Expression.cs:41
static Complex ToComplex(object Object)
Converts an object to a complex value.
Definition: Expression.cs:5228
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
Calculates a Halley Fractal Image, but when clicked, adds a root to the underying polynomial,...
HalleyBuilderFractal(ScriptNode z, ScriptNode dr, ScriptNode R, ScriptNode Coefficients, ScriptNode Palette, ScriptNode DimX, int Start, int Length, Expression Expression)
TODO
HalleyBuilderFractal(ScriptNode z, ScriptNode dr, ScriptNode R, ScriptNode Coefficients, int Start, int Length, Expression Expression)
TODO
override IElement Evaluate(IElement[] Arguments, Variables Variables)
TODO
HalleyBuilderFractal(ScriptNode z, ScriptNode dr, ScriptNode R, int Start, int Length, Expression Expression)
TODO
HalleyBuilderFractal(ScriptNode z, ScriptNode dr, ScriptNode R, ScriptNode Coefficients, ScriptNode Palette, ScriptNode DimX, ScriptNode DimY, int Start, int Length, Expression Expression)
TODO
HalleyBuilderFractal(ScriptNode z, ScriptNode dr, ScriptNode R, ScriptNode Coefficients, ScriptNode Palette, int Start, int Length, Expression Expression)
TODO
static FractalGraph CalcHalley(Variables Variables, double rCenter, double iCenter, double rDelta, Complex R, double[] Coefficients, SKColor[] Palette, int Width, int Height, ScriptNode Node, FractalZoomScript FractalZoomScript, object State)
Calculates a Halley Fractal
Exception thrown if an image with invalid size is requested.
Defines a clickable fractal graph in the complex plane.
Definition: FractalGraph.cs:23
static SKColor[] ToPalette(ObjectVector Vector)
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
object AssociatedObjectValue
Associated object value.
Definition: IElement.cs:34
Basic interface for vectors.
Definition: IVector.cs:9
int Dimension
Dimension of vector.
Definition: IVector.cs:14
IElement GetElement(int Index)
Gets an element of the vector.
ArgumentType
Type of parameter used in a function definition or a lambda definition.
Definition: IFunction.cs:9