From 938c95fa18b348e9ef3dbb6f341f6335f51d41f9 Mon Sep 17 00:00:00 2001 From: That-One-Nerd Date: Tue, 26 Nov 2024 08:30:40 -0500 Subject: [PATCH] Added fill support. Also reworked the matrix constructors to make more sense. --- Nerd_STF/Fill.cs | 5 ++ Nerd_STF/Helpers/MatrixHelper.cs | 14 ++--- Nerd_STF/ListTuple.cs | 5 ++ .../Mathematics/Algebra/IMatrixOperations.cs | 1 + Nerd_STF/Mathematics/Algebra/Matrix2x2.cs | 49 ++++++++++++--- Nerd_STF/Mathematics/Algebra/Matrix3x3.cs | 54 +++++++++++++--- Nerd_STF/Mathematics/Algebra/Matrix4x4.cs | 61 ++++++++++++++++--- Nerd_STF/Mathematics/Equations/Linear.cs | 5 ++ Nerd_STF/Mathematics/Equations/Polynomial.cs | 10 +++ Nerd_STF/Mathematics/Equations/Quadratic.cs | 6 ++ Nerd_STF/Mathematics/Float2.cs | 5 ++ Nerd_STF/Mathematics/Float3.cs | 6 ++ Nerd_STF/Mathematics/Float4.cs | 7 +++ Nerd_STF/Mathematics/Int2.cs | 5 ++ Nerd_STF/Mathematics/Int3.cs | 6 ++ Nerd_STF/Mathematics/Int4.cs | 7 +++ Nerd_STF/Mathematics/MathE.cs | 32 ++++++++++ 17 files changed, 243 insertions(+), 35 deletions(-) create mode 100644 Nerd_STF/Fill.cs diff --git a/Nerd_STF/Fill.cs b/Nerd_STF/Fill.cs new file mode 100644 index 0000000..c6658dd --- /dev/null +++ b/Nerd_STF/Fill.cs @@ -0,0 +1,5 @@ +namespace Nerd_STF +{ + public delegate T Fill(int index); + public delegate T Fill2d(int x, int y); +} diff --git a/Nerd_STF/Helpers/MatrixHelper.cs b/Nerd_STF/Helpers/MatrixHelper.cs index 114eb1d..8f4b522 100644 --- a/Nerd_STF/Helpers/MatrixHelper.cs +++ b/Nerd_STF/Helpers/MatrixHelper.cs @@ -16,12 +16,12 @@ namespace Nerd_STF.Helpers int y = 0; foreach (double v in part) { - matrix[byRows ? (x, y) : (y, x)] = v; + matrix[byRows ? (y, x) : (x, y)] = v; y++; - if (byRows ? y >= size.x : y >= size.y) break; + if (byRows ? y >= size.y : y >= size.x) break; } x++; - if (byRows ? x >= size.y : x >= size.x) break; + if (byRows ? x >= size.x : x >= size.y) break; } } public static void SetMatrixValues(TMat matrix, IEnumerable> vals, bool byRows) @@ -35,17 +35,17 @@ namespace Nerd_STF.Helpers int y = 0; foreach (double v in part) { - matrix[byRows ? (x, y) : (y, x)] = v; + matrix[byRows ? (y, x) : (x, y)] = v; y++; - if (byRows ? y >= size.x : y >= size.y) break; + if (byRows ? y >= size.y : y >= size.x) break; } x++; - if (byRows ? x >= size.y : x >= size.x) break; + if (byRows ? x >= size.x : x >= size.y) break; } } public static void SetRow(TMat matrix, int row, IEnumerable vals) - where TMat: IMatrix + where TMat : IMatrix { int col = 0; int max = matrix.Size.y; diff --git a/Nerd_STF/ListTuple.cs b/Nerd_STF/ListTuple.cs index 22d8c73..85994b4 100644 --- a/Nerd_STF/ListTuple.cs +++ b/Nerd_STF/ListTuple.cs @@ -25,6 +25,11 @@ namespace Nerd_STF { this.items = items; } + public ListTuple(Fill items, int length) + { + this.items = new T[length]; + for (int i = 0; i < length; i++) this.items[i] = items(i); + } public T this[int index] { diff --git a/Nerd_STF/Mathematics/Algebra/IMatrixOperations.cs b/Nerd_STF/Mathematics/Algebra/IMatrixOperations.cs index a818875..b89b43d 100644 --- a/Nerd_STF/Mathematics/Algebra/IMatrixOperations.cs +++ b/Nerd_STF/Mathematics/Algebra/IMatrixOperations.cs @@ -11,6 +11,7 @@ namespace Nerd_STF.Mathematics.Algebra static abstract TSelf operator *(TSelf a, double b); static abstract TSelf operator /(TSelf a, double b); + static abstract TSelf operator ^(TSelf a, TSelf b); static abstract TSelf? operator ~(TSelf m); } } diff --git a/Nerd_STF/Mathematics/Algebra/Matrix2x2.cs b/Nerd_STF/Mathematics/Algebra/Matrix2x2.cs index 8d5ac28..cf9ee3e 100644 --- a/Nerd_STF/Mathematics/Algebra/Matrix2x2.cs +++ b/Nerd_STF/Mathematics/Algebra/Matrix2x2.cs @@ -46,30 +46,58 @@ namespace Nerd_STF.Mathematics.Algebra this.r1c1 = r1c1; } /// if the array is of the form [c, r], if the array is of the form [r, c]. - public Matrix2x2(double[,] vals, bool byRows = true) + public Matrix2x2(double[,] vals, bool byRows = false) { if (byRows) // Collection of rows ([c, r]) - { - r0c0 = vals[0, 0]; r0c1 = vals[0, 1]; - r1c0 = vals[1, 0]; r1c1 = vals[1, 1]; - } - else // Collection of columns ([r, c]) { r0c0 = vals[0, 0]; r0c1 = vals[1, 0]; r1c0 = vals[0, 1]; r1c1 = vals[1, 1]; } + else // Collection of columns ([r, c]) + { + r0c0 = vals[0, 0]; r0c1 = vals[0, 1]; + r1c0 = vals[1, 0]; r1c1 = vals[1, 1]; + } } /// if the enumerable is a collection of rows (form [c, r]), if the enumerable is a collection of columns (form [r, c]). - public Matrix2x2(IEnumerable> vals, bool byRows = true) + public Matrix2x2(IEnumerable> vals, bool byRows = false) { MatrixHelper.SetMatrixValues(this, vals, byRows); } /// if the enumerable is a collection of rows (form [c, r]), if the enumerable is a collection of columns (form [r, c]). - public Matrix2x2(IEnumerable> vals, bool byRows = true) + public Matrix2x2(IEnumerable> vals, bool byRows = false) { MatrixHelper.SetMatrixValues(this, vals, byRows); } - + /// if the fill goes through columns for each row, if the fill goes through rows for each column. + public Matrix2x2(Fill fill, bool byRows = false) + { + if (byRows) + { + r0c0 = fill(0); r0c1 = fill(2); + r1c0 = fill(1); r1c1 = fill(3); + } + else + { + r0c0 = fill(0); r0c1 = fill(1); + r1c0 = fill(2); r1c1 = fill(3); + } + } + /// if the fill is a collection of rows (form [c, r]), if the fill is a collection of columns (form [r, c]). + public Matrix2x2(Fill2d fill, bool byRows = false) + { + if (byRows) + { + r0c0 = fill(0, 0); r0c1 = fill(1, 0); + r1c0 = fill(0, 1); r1c1 = fill(1, 1); + } + else + { + r0c0 = fill(0, 0); r0c1 = fill(0, 1); + r1c0 = fill(1, 0); r1c1 = fill(1, 1); + } + } + public double this[int r, int c] { get @@ -314,6 +342,9 @@ namespace Nerd_STF.Mathematics.Algebra public static Matrix2x2 operator /(Matrix2x2 a, double b) => new Matrix2x2(a.r0c0 / b, a.r0c1 / b, a.r1c0 / b, a.r1c1 / b); + public static Matrix2x2 operator ^(Matrix2x2 a, Matrix2x2 b) => + new Matrix2x2(a.r0c0 * b.r0c0, a.r0c1 * b.r0c1, + a.r1c0 * b.r1c0, a.r1c1 * b.r1c1); public static Matrix2x2 operator ~(Matrix2x2 a) => a.Inverse(); public static bool operator ==(Matrix2x2 a, Matrix2x2 b) => a.Equals(b); public static bool operator !=(Matrix2x2 a, Matrix2x2 b) => !a.Equals(b); diff --git a/Nerd_STF/Mathematics/Algebra/Matrix3x3.cs b/Nerd_STF/Mathematics/Algebra/Matrix3x3.cs index b409710..b618d2b 100644 --- a/Nerd_STF/Mathematics/Algebra/Matrix3x3.cs +++ b/Nerd_STF/Mathematics/Algebra/Matrix3x3.cs @@ -51,31 +51,63 @@ namespace Nerd_STF.Mathematics.Algebra this.r2c0 = r2c0; this.r2c1 = r2c1; this.r2c2 = r2c2; } /// if the array is of the form [c, r], if the array is of the form [r, c]. - public Matrix3x3(double[,] vals, bool byRows = true) + public Matrix3x3(double[,] vals, bool byRows = false) { if (byRows) // Collection of rows ([c, r]) - { - r0c0 = vals[0, 0]; r0c1 = vals[0, 1]; r0c2 = vals[0, 2]; - r1c0 = vals[1, 0]; r1c1 = vals[1, 1]; r1c2 = vals[1, 2]; - r2c0 = vals[2, 0]; r2c1 = vals[2, 1]; r2c2 = vals[2, 2]; - } - else // Collection of columns ([r, c]) { r0c0 = vals[0, 0]; r0c1 = vals[1, 0]; r0c2 = vals[2, 0]; r1c0 = vals[0, 1]; r1c1 = vals[1, 1]; r1c2 = vals[2, 1]; r2c0 = vals[0, 2]; r2c1 = vals[1, 2]; r2c2 = vals[2, 2]; } + else // Collection of columns ([r, c]) + { + r0c0 = vals[0, 0]; r0c1 = vals[0, 1]; r0c2 = vals[0, 2]; + r1c0 = vals[1, 0]; r1c1 = vals[1, 1]; r1c2 = vals[1, 2]; + r2c0 = vals[2, 0]; r2c1 = vals[2, 1]; r2c2 = vals[2, 2]; + } } /// if the enumerable is a collection of rows (form [c, r]), if the enumerable is a collection of columns (form [r, c]). - public Matrix3x3(IEnumerable> vals, bool byRows = true) + public Matrix3x3(IEnumerable> vals, bool byRows = false) { MatrixHelper.SetMatrixValues(this, vals, byRows); } /// if the enumerable is a collection of rows (form [c, r]), if the enumerable is a collection of columns (form [r, c]). - public Matrix3x3(IEnumerable> vals, bool byRows = true) + public Matrix3x3(IEnumerable> vals, bool byRows = false) { MatrixHelper.SetMatrixValues(this, vals, byRows); } + /// if the fill goes through columns for each row, if the fill goes through rows for each column. + public Matrix3x3(Fill fill, bool byRows = false) + { + if (byRows) + { + r0c0 = fill(0); r0c1 = fill(3); r0c2 = fill(6); + r1c0 = fill(1); r1c1 = fill(4); r1c2 = fill(7); + r2c0 = fill(2); r2c1 = fill(5); r2c2 = fill(8); + } + else + { + r0c0 = fill(0); r0c1 = fill(1); r0c2 = fill(2); + r1c0 = fill(3); r1c1 = fill(4); r1c2 = fill(5); + r2c0 = fill(6); r2c1 = fill(7); r2c2 = fill(8); + } + } + /// if the fill is a collection of rows (form [c, r]), if the fill is a collection of columns (form [r, c]). + public Matrix3x3(Fill2d fill, bool byRows = false) + { + if (byRows) + { + r0c0 = fill(0, 0); r0c1 = fill(1, 0); r0c2 = fill(2, 0); + r1c0 = fill(0, 1); r1c1 = fill(1, 1); r1c2 = fill(2, 1); + r2c0 = fill(0, 2); r2c1 = fill(1, 2); r2c2 = fill(2, 2); + } + else + { + r0c0 = fill(0, 0); r0c1 = fill(0, 1); r0c2 = fill(0, 2); + r1c0 = fill(1, 0); r1c1 = fill(1, 1); r1c2 = fill(1, 2); + r2c0 = fill(2, 0); r2c1 = fill(2, 1); r2c2 = fill(2, 2); + } + } public double this[int r, int c] { @@ -402,6 +434,10 @@ namespace Nerd_STF.Mathematics.Algebra new Matrix3x3(a.r0c0 / b, a.r0c1 / b, a.r0c2 / b, a.r1c0 / b, a.r1c1 / b, a.r1c2 / b, a.r2c0 / b, a.r2c1 / b, a.r2c2 / b); + public static Matrix3x3 operator ^(Matrix3x3 a, Matrix3x3 b) => + new Matrix3x3(a.r0c0 * b.r0c0, a.r0c1 * b.r0c1, a.r0c2 * b.r0c2, + a.r1c0 * b.r1c0, a.r1c1 * b.r1c1, a.r1c2 * b.r1c2, + a.r2c0 * b.r2c0, a.r2c1 * b.r2c1, a.r2c2 * b.r2c2); public static Matrix3x3 operator ~(Matrix3x3 a) => a.Inverse(); public static bool operator ==(Matrix3x3 a, Matrix3x3 b) => a.Equals(b); public static bool operator !=(Matrix3x3 a, Matrix3x3 b) => !a.Equals(b); diff --git a/Nerd_STF/Mathematics/Algebra/Matrix4x4.cs b/Nerd_STF/Mathematics/Algebra/Matrix4x4.cs index cfcd758..38a3f05 100644 --- a/Nerd_STF/Mathematics/Algebra/Matrix4x4.cs +++ b/Nerd_STF/Mathematics/Algebra/Matrix4x4.cs @@ -57,33 +57,69 @@ namespace Nerd_STF.Mathematics.Algebra this.r3c0 = r3c0; this.r3c1 = r3c1; this.r3c2 = r3c2; this.r3c3 = r3c3; } /// if the array is of the form [c, r], if the array is of the form [r, c]. - public Matrix4x4(double[,] vals, bool byRows = true) + public Matrix4x4(double[,] vals, bool byRows = false) { if (byRows) // Collection of rows ([c, r]) - { - r0c0 = vals[0, 0]; r0c1 = vals[0, 1]; r0c2 = vals[0, 2]; r0c3 = vals[0, 3]; - r1c0 = vals[1, 0]; r1c1 = vals[1, 1]; r1c2 = vals[1, 2]; r1c3 = vals[1, 3]; - r2c0 = vals[2, 0]; r2c1 = vals[2, 1]; r2c2 = vals[2, 2]; r2c3 = vals[2, 3]; - r3c0 = vals[3, 0]; r3c1 = vals[3, 1]; r3c2 = vals[3, 2]; r3c3 = vals[3, 3]; - } - else { r0c0 = vals[0, 0]; r0c1 = vals[1, 0]; r0c2 = vals[2, 0]; r0c3 = vals[3, 0]; r1c0 = vals[0, 1]; r1c1 = vals[1, 1]; r1c2 = vals[2, 1]; r1c3 = vals[3, 1]; r2c0 = vals[0, 2]; r2c1 = vals[1, 2]; r2c2 = vals[2, 2]; r2c3 = vals[3, 2]; r3c0 = vals[0, 3]; r3c1 = vals[1, 3]; r3c2 = vals[2, 3]; r3c3 = vals[3, 3]; } + else + { + r0c0 = vals[0, 0]; r0c1 = vals[0, 1]; r0c2 = vals[0, 2]; r0c3 = vals[0, 3]; + r1c0 = vals[1, 0]; r1c1 = vals[1, 1]; r1c2 = vals[1, 2]; r1c3 = vals[1, 3]; + r2c0 = vals[2, 0]; r2c1 = vals[2, 1]; r2c2 = vals[2, 2]; r2c3 = vals[2, 3]; + r3c0 = vals[3, 0]; r3c1 = vals[3, 1]; r3c2 = vals[3, 2]; r3c3 = vals[3, 3]; + } } /// if the enumerable is a collection of rows (form [c, r]), if the enumerable is a collection of columns (form [r, c]). - public Matrix4x4(IEnumerable> vals, bool byRows = true) + public Matrix4x4(IEnumerable> vals, bool byRows = false) { MatrixHelper.SetMatrixValues(this, vals, byRows); } /// if the enumerable is a collection of rows (form [c, r]), if the enumerable is a collection of columns (form [r, c]). - public Matrix4x4(IEnumerable> vals, bool byRows = true) + public Matrix4x4(IEnumerable> vals, bool byRows = false) { MatrixHelper.SetMatrixValues(this, vals, byRows); } + /// if the fill goes through columns for each row, if the fill goes through rows for each column. + public Matrix4x4(Fill fill, bool byRows = false) + { + if (byRows) + { + r0c0 = fill(0); r0c1 = fill(4); r0c2 = fill( 8); r0c3 = fill(12); + r1c0 = fill(1); r1c1 = fill(5); r1c2 = fill( 9); r1c3 = fill(13); + r2c0 = fill(2); r2c1 = fill(6); r2c2 = fill(10); r2c3 = fill(14); + r3c0 = fill(3); r3c1 = fill(7); r3c2 = fill(11); r3c3 = fill(15); + } + else + { + r0c0 = fill( 0); r0c1 = fill( 1); r0c2 = fill( 2); r0c3 = fill( 3); + r1c0 = fill( 4); r1c1 = fill( 5); r1c2 = fill( 6); r1c3 = fill( 7); + r2c0 = fill( 8); r2c1 = fill( 9); r2c2 = fill(10); r2c3 = fill(11); + r3c0 = fill(12); r3c1 = fill(13); r3c2 = fill(14); r3c3 = fill(15); + } + } + /// if the fill is a collection of rows (form [c, r]), if the fill is a collection of columns (form [r, c]). + public Matrix4x4(Fill2d fill, bool byRows = false) + { + if (byRows) + { + r0c0 = fill(0, 0); r0c1 = fill(1, 0); r0c2 = fill(2, 0); r0c3 = fill(3, 0); + r1c0 = fill(0, 1); r1c1 = fill(1, 1); r1c2 = fill(2, 1); r1c3 = fill(3, 1); + r2c0 = fill(0, 2); r2c1 = fill(1, 2); r2c2 = fill(2, 2); r2c3 = fill(3, 2); + r3c0 = fill(0, 3); r3c1 = fill(1, 3); r3c2 = fill(2, 3); r3c3 = fill(3, 3); + } + else + { + r0c0 = fill(0, 0); r0c1 = fill(0, 1); r0c2 = fill(0, 2); r0c3 = fill(0, 3); + r1c0 = fill(1, 0); r1c1 = fill(1, 1); r1c2 = fill(1, 2); r1c3 = fill(1, 3); + r2c0 = fill(2, 0); r2c1 = fill(2, 1); r2c2 = fill(2, 2); r2c3 = fill(2, 3); + r3c0 = fill(3, 0); r3c1 = fill(3, 1); r3c2 = fill(3, 2); r3c3 = fill(3, 3); + } + } public double this[int r, int c] { @@ -544,6 +580,11 @@ namespace Nerd_STF.Mathematics.Algebra a.r1c0 / b, a.r1c1 / b, a.r1c2 / b, a.r1c3 / b, a.r2c0 / b, a.r2c1 / b, a.r2c2 / b, a.r2c3 / b, a.r3c0 / b, a.r3c1 / b, a.r3c2 / b, a.r3c3 / b); + public static Matrix4x4 operator ^(Matrix4x4 a, Matrix4x4 b) => + new Matrix4x4(a.r0c0 * b.r0c0, a.r0c1 * b.r0c1, a.r0c2 * b.r0c2, a.r0c3 * b.r0c3, + a.r1c0 * b.r1c0, a.r1c1 * b.r1c1, a.r1c2 * b.r1c2, a.r1c3 * b.r1c3, + a.r2c0 * b.r2c0, a.r2c1 * b.r2c1, a.r2c2 * b.r2c2, a.r2c3 * b.r2c3, + a.r3c0 * b.r3c0, a.r3c1 * b.r3c1, a.r3c2 * b.r3c2, a.r3c3 * b.r3c3); public static Matrix4x4 operator ~(Matrix4x4 a) => a.Inverse(); public static bool operator ==(Matrix4x4 a, Matrix4x4 b) => a.Equals(b); public static bool operator !=(Matrix4x4 a, Matrix4x4 b) => !a.Equals(b); diff --git a/Nerd_STF/Mathematics/Equations/Linear.cs b/Nerd_STF/Mathematics/Equations/Linear.cs index 76c528a..a686f5a 100644 --- a/Nerd_STF/Mathematics/Equations/Linear.cs +++ b/Nerd_STF/Mathematics/Equations/Linear.cs @@ -21,6 +21,11 @@ namespace Nerd_STF.Mathematics.Equations M = m; B = b; } + public Linear(Fill fill) + { + B = fill(0); + M = fill(1); + } public double this[double x] => M * x + B; public double Get(double x) => M * x + B; diff --git a/Nerd_STF/Mathematics/Equations/Polynomial.cs b/Nerd_STF/Mathematics/Equations/Polynomial.cs index cc5017d..0fc2e20 100644 --- a/Nerd_STF/Mathematics/Equations/Polynomial.cs +++ b/Nerd_STF/Mathematics/Equations/Polynomial.cs @@ -30,6 +30,16 @@ namespace Nerd_STF.Mathematics.Equations if (reverse) this.terms = TrimExcessTerms(terms.Reverse().ToArray()); else this.terms = TrimExcessTerms(terms.ToArray()); } + public Polynomial(bool reverse, Fill terms, int length) + { + this.terms = new double[length]; + for (int i = 0; i < length; i++) + { + if (reverse) this.terms[i] = terms(length - 1 - i); + else this.terms[i] = terms(i); + } + this.terms = TrimExcessTerms(this.terms); + } private static double[] TrimExcessTerms(double[] terms) { int newLength = terms.Length; diff --git a/Nerd_STF/Mathematics/Equations/Quadratic.cs b/Nerd_STF/Mathematics/Equations/Quadratic.cs index c7ab443..a13f964 100644 --- a/Nerd_STF/Mathematics/Equations/Quadratic.cs +++ b/Nerd_STF/Mathematics/Equations/Quadratic.cs @@ -42,6 +42,12 @@ namespace Nerd_STF.Mathematics.Equations B = 0; C = c; } + public Quadratic(Fill fill) + { + C = fill(0); + B = fill(1); + A = fill(2); + } public double this[double x] => A * x * x + B * x + C; public double Get(double x) => A * x * x + B * x + C; diff --git a/Nerd_STF/Mathematics/Float2.cs b/Nerd_STF/Mathematics/Float2.cs index d381fc0..02d4e84 100644 --- a/Nerd_STF/Mathematics/Float2.cs +++ b/Nerd_STF/Mathematics/Float2.cs @@ -48,6 +48,11 @@ namespace Nerd_STF.Mathematics if (index >= 2) break; } } + public Float2(Fill fill) + { + x = fill(0); + y = fill(1); + } public double this[int index] { diff --git a/Nerd_STF/Mathematics/Float3.cs b/Nerd_STF/Mathematics/Float3.cs index 45037f5..78498f9 100644 --- a/Nerd_STF/Mathematics/Float3.cs +++ b/Nerd_STF/Mathematics/Float3.cs @@ -51,6 +51,12 @@ namespace Nerd_STF.Mathematics if (index >= 2) break; } } + public Float3(Fill fill) + { + x = fill(0); + y = fill(1); + z = fill(2); + } public double this[int index] { diff --git a/Nerd_STF/Mathematics/Float4.cs b/Nerd_STF/Mathematics/Float4.cs index 4ef34f5..de71d73 100644 --- a/Nerd_STF/Mathematics/Float4.cs +++ b/Nerd_STF/Mathematics/Float4.cs @@ -55,6 +55,13 @@ namespace Nerd_STF.Mathematics if (index >= 2) break; } } + public Float4(Fill fill) + { + w = fill(0); + x = fill(1); + y = fill(2); + z = fill(3); + } public double this[int index] { diff --git a/Nerd_STF/Mathematics/Int2.cs b/Nerd_STF/Mathematics/Int2.cs index e01f297..6d84344 100644 --- a/Nerd_STF/Mathematics/Int2.cs +++ b/Nerd_STF/Mathematics/Int2.cs @@ -47,6 +47,11 @@ namespace Nerd_STF.Mathematics if (index >= 2) break; } } + public Int2(Fill fill) + { + x = fill(0); + y = fill(1); + } public int this[int index] { diff --git a/Nerd_STF/Mathematics/Int3.cs b/Nerd_STF/Mathematics/Int3.cs index 517e599..a667f75 100644 --- a/Nerd_STF/Mathematics/Int3.cs +++ b/Nerd_STF/Mathematics/Int3.cs @@ -50,6 +50,12 @@ namespace Nerd_STF.Mathematics if (index >= 2) break; } } + public Int3(Fill fill) + { + x = fill(0); + y = fill(1); + z = fill(2); + } public int this[int index] { diff --git a/Nerd_STF/Mathematics/Int4.cs b/Nerd_STF/Mathematics/Int4.cs index 9ea1c87..9525e54 100644 --- a/Nerd_STF/Mathematics/Int4.cs +++ b/Nerd_STF/Mathematics/Int4.cs @@ -54,6 +54,13 @@ namespace Nerd_STF.Mathematics if (index >= 2) break; } } + public Int4(Fill fill) + { + w = fill(0); + x = fill(1); + y = fill(2); + z = fill(3); + } public int this[int index] { diff --git a/Nerd_STF/Mathematics/MathE.cs b/Nerd_STF/Mathematics/MathE.cs index e557d47..7249a21 100644 --- a/Nerd_STF/Mathematics/MathE.cs +++ b/Nerd_STF/Mathematics/MathE.cs @@ -374,6 +374,24 @@ namespace Nerd_STF.Mathematics } public static IEquation Lerp(IEquation a, IEquation b, double t, bool clamp = true) => new Equation((double x) => Lerp(a.Get(x), b.Get(x), t, clamp)); + public static Fill Lerp(Fill a, Fill b, double t, bool clamp = true) + { + if (clamp) Clamp(ref t, 0, 1); + return delegate (int index) + { + double aVal = a(index); + return aVal + t * (b(index) - aVal); + }; + } + public static Fill Lerp(Fill a, Fill b, double t, bool clamp = true) + { + if (clamp) Clamp(ref t, 0, 1); + return delegate (int index) + { + int aVal = a(index); + return (int)(aVal + t * (b(index) - aVal)); + }; + } #if CS11_OR_GREATER public static T Lerp(T a, T b, T t, bool clamp = true) where T : INumber @@ -381,6 +399,20 @@ namespace Nerd_STF.Mathematics if (clamp) Clamp(ref t, T.Zero, T.One); return a + t * (b - a); } + public static T Lerp(T a, T b, double t, bool clamp = true) + where T : IInterpolable => T.Lerp(a, b, t, clamp); + public static Fill Lerp(Fill a, Fill b, T t, bool clamp = true) + where T : INumber + { + if (clamp) Clamp(ref t, T.Zero, T.One); + return delegate (int index) + { + T aVal = a(index); + return aVal + t * (b(index) - aVal); + }; + } + public static Fill Lerp(Fill a, Fill b, double t, bool clamp = true) + where T : IInterpolable => (int index) => T.Lerp(a(index), b(index), t, clamp); #endif public static int Max(int a, int b) => a > b ? a : b;