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;
8
10{
19 {
24 ScriptNode Palette, ScriptNode DimX, ScriptNode DimY, int Start, int Length, Expression Expression)
25 : base(new ScriptNode[] { z, dr, R, Coefficients, Palette, DimX, DimY },
26 new ArgumentType[] { ArgumentType.Scalar, ArgumentType.Scalar, ArgumentType.Scalar,
27 ArgumentType.Normal, ArgumentType.Vector, ArgumentType.Scalar, ArgumentType.Scalar}, Start, Length, Expression)
28 {
29 }
30
35 ScriptNode Palette, ScriptNode DimX, int Start, int Length, Expression Expression)
36 : base(new ScriptNode[] { z, dr, R, Coefficients, Palette, DimX },
37 new ArgumentType[] { ArgumentType.Scalar, ArgumentType.Scalar, ArgumentType.Scalar,
39 {
40 }
41
46 ScriptNode Palette, int Start, int Length, Expression Expression)
47 : base(new ScriptNode[] { z, dr, R, Coefficients, Palette },
48 new ArgumentType[] { ArgumentType.Scalar, ArgumentType.Scalar, ArgumentType.Scalar,
50 {
51 }
52
58 : base(new ScriptNode[] { z, dr, R, Coefficients },
59 new ArgumentType[] { ArgumentType.Scalar, ArgumentType.Scalar, ArgumentType.Scalar,
61 {
62 }
63
69 : base(new ScriptNode[] { z, dr, R },
70 new ArgumentType[] { ArgumentType.Scalar, ArgumentType.Scalar, ArgumentType.Scalar },
72 {
73 }
74
78 public override string[] DefaultArgumentNames
79 {
80 get
81 {
82 return new string[] { "z", "dr", "R", "Coefficients", "Palette", "DimX", "DimY" };
83 }
84 }
85
90 {
91 string ColorExpression = null;
92 SKColor[] Palette;
93 double[] Coefficients = null;
94 Complex[] CoefficientsZ = null;
95 double rc, ic;
96 double dr;
97 Complex R;
98 int dimx, dimy;
99 int c = Arguments.Length;
100 int i = 0;
101 object Obj;
102
103 Obj = Arguments[i++].AssociatedObjectValue;
104 if (Obj is Complex z)
105 {
106 rc = z.Real;
107 ic = z.Imaginary;
108 }
109 else
110 {
111 rc = Expression.ToDouble(Obj);
112 ic = Expression.ToDouble(Arguments[i++].AssociatedObjectValue);
113 }
114
115 if (i >= c)
116 throw new ScriptRuntimeException("Insufficient parameters in call to HalleyBuilderFractal().", this);
117
118 dr = Expression.ToDouble(Arguments[i++].AssociatedObjectValue);
119
120 if (i < c && ((Obj = Arguments[i].AssociatedObjectValue) is double || Obj is Complex))
121 {
122 R = Expression.ToComplex(Obj);
123 i++;
124 }
125 else
126 {
127 R = Complex.One;
128
129 if (i < c && this.Arguments[i] is null)
130 i++;
131 }
132
133 if (i < c && Arguments[i] is DoubleVector)
134 {
135 Coefficients = (double[])Arguments[i++].AssociatedObjectValue;
136
137 int j, d = Coefficients.Length;
138
139 CoefficientsZ = new Complex[d];
140 for (j = 0; j < d; j++)
141 CoefficientsZ[j] = new Complex(Coefficients[j], 0);
142 }
143 else if (i < c && Arguments[i] is ComplexVector)
144 CoefficientsZ = (Complex[])Arguments[i++].AssociatedObjectValue;
145 /*else if (i < c && Parameters[i] is RealPolynomial)
146 Coefficients = ((RealPolynomial)Arguments[i++].AssociatedObjectValue).Coefficients;
147 else if (i < c && Parameters[i] is ComplexPolynomial)
148 CoefficientsZ = ((ComplexPolynomial)Arguments[i++].AssociatedObjectValue).Coefficients;*/
149 else if (i < c && Arguments[i] is IVector)
150 {
151 IVector Vector = (IVector)Arguments[i++];
152 int j, d = Vector.Dimension;
153
154 CoefficientsZ = new Complex[d];
155 for (j = 0; j < d; j++)
156 CoefficientsZ[j] = Expression.ToComplex(Vector.GetElement(j).AssociatedObjectValue);
157 }
158 else
159 CoefficientsZ = new Complex[0];
160
161 if (i < c && !(this.Arguments[i] is null) && Arguments[i] is ObjectVector)
162 {
163 ColorExpression = this.Arguments[i].SubExpression;
165 }
166 else
167 {
168 Palette = ColorModels.RandomLinearAnalogousHSL.CreatePalette(128, 4, out int Seed, this, Variables);
169 ColorExpression = "RandomLinearAnalogousHSL(128,4," + Seed.ToString() + ")";
170
171 if (i < c && this.Arguments[i] is null)
172 i++;
173 }
174
175 if (i < c)
176 dimx = (int)Expression.ToDouble(Arguments[i++].AssociatedObjectValue);
177 else
178 dimx = 320;
179
180 if (i < c)
181 dimy = (int)Expression.ToDouble(Arguments[i++].AssociatedObjectValue);
182 else
183 dimy = 200;
184
185 if (i < c)
186 {
187 throw new ScriptRuntimeException("Parameter mismatch in call to HalleyBuilderFractal(z,dr[,R][,Coefficients][,Palette][,dimx[,dimy]]).",
188 this);
189 }
190
191 if (dimx <= 0 || dimx > 5000 || dimy <= 0 || dimy > 5000)
192 throw new ScriptRuntimeException("Image size must be within 1x1 to 5000x5000", this);
193
194 return HalleyFractal.CalcHalley(Variables, rc, ic, dr, R, CoefficientsZ, Palette, dimx, dimy, this, this.FractalZoomScript,
195 new object[] { Palette, dimx, dimy, R, CoefficientsZ, ColorExpression, rc, ic });
196 }
197
198 private string FractalZoomScript(double r, double i, double Size, object State)
199 {
200 object[] Parameters = (object[])State;
201 int DimX = (int)Parameters[1];
202 int DimY = (int)Parameters[2];
203 Complex R = (Complex)Parameters[3];
204 Complex[] CoefficientsZ = (Complex[])Parameters[4];
205 string ColorExpression = (string)Parameters[5];
206 double rc = (double)Parameters[6];
207 double ic = (double)Parameters[7];
208 Complex z0 = new Complex(r, i);
209 int j;
210
211 int c = CoefficientsZ.Length;
212 while (c > 1 && CoefficientsZ[c - 1].Equals(0))
213 c--;
214
215 Complex[] C2;
216
217 if (c < 1)
218 {
219 C2 = new Complex[2];
220 C2[0] = -z0;
221 C2[1] = Complex.One;
222 }
223 else
224 {
225 C2 = new Complex[c + 1];
226 C2[0] = Complex.Zero;
227 for (j = 0; j < c; j++)
228 C2[j + 1] = CoefficientsZ[j];
229
230 for (j = 0; j < c; j++)
231 C2[j] -= z0 * CoefficientsZ[j];
232 }
233
234 StringBuilder sb = new StringBuilder();
235
236 sb.Append("HalleyBuilderFractal((");
237 sb.Append(Expression.ToString(rc));
238 sb.Append(',');
239 sb.Append(Expression.ToString(ic));
240 sb.Append("),");
241 sb.Append(Expression.ToString(Size));
242 sb.Append(',');
243 sb.Append(Expression.ToString(R));
244 sb.Append(',');
245 sb.Append(Expression.ToString(C2));
246
247 if (!string.IsNullOrEmpty(ColorExpression))
248 {
249 sb.Append(',');
250 sb.Append(ColorExpression);
251 }
252
253 sb.Append(',');
254 sb.Append(DimX.ToString());
255 sb.Append(',');
256 sb.Append(DimY.ToString());
257 sb.Append(')');
258
259 return sb.ToString();
260 }
261
265 public override string FunctionName => nameof(HalleyBuilderFractal);
266 }
267}
Class managing a script expression.
Definition: Expression.cs:39
static Complex ToComplex(object Object)
Converts an object to a complex value.
Definition: Expression.cs:4942
static double ToDouble(object Object)
Converts an object to a double value.
Definition: Expression.cs:4824
static string ToString(double Value)
Converts a value to a string, that can be parsed as part of an expression.
Definition: Expression.cs:4496
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
Defines a clickable fractal graph in the complex plane.
Definition: FractalGraph.cs:22
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:20
object AssociatedObjectValue
Associated object value.
Definition: IElement.cs:33
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