I’m glad you got it working!

]]>Thanks for your Samples, yes your sample saves multiple images – [ONE] per record! I want to save [MANY] per record as i stated ( 5-6 images). It’s no problem as i’ve managed to accomplish my needs for the app i’m designing for my daughter utilizing the sample code and link i mentioned. Once i read it and fully understood it- it became clear and made sense, once i implemented the code into my app with a couple of adjustments it works and performs as i needed.

thanks for your samples and your books as they helped me accomplish what i needed.

Thanks

]]>You can download the example program to see the details that are not covered in the post.

You might also want to look at the earlier post Find a linear least squares fit for a set of points in C#. It uses the same approach but with a line so it’s a bit easier to understand.

If that doesn’t help, let me know and I’ll take a look at your code.

]]>I have attached the code below:

I believe that the issue is with the LoadArray array function. Your web site wasn’t very clear on what the textboxes actually contained in them. I was hoping to feed a list of classes (each class contains an X and Y datapair) into the function and use that.

I was hoping you could take a look and let me know what I am missing.

Here is the code.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

namespace MeziLuMathExtensions

{

public class clsPolynomialRegression : CommonMathCode

{

#region Data Structures …

public List Coefficients

{

internal set;

get;

}

#endregion

#region Calculators …

///

/// Find the least squares linear fit for an NthDegree polynomial.

///

///

///

///

public Tuple<List, bool> Calculate(List DataPairs, int NthDegree)

{ //http://csharphelper.com/blog/2014/10/find-a-polynomial-least-squares-fit-for-a-set-of-points-in-c/

try

{

#region Allocate space for (degree + 1) equations with (degree + 2) terms each (including the constant term) …

double[,] coeffs = new double[NthDegree + 1, NthDegree + 2];

#endregion

#region Calculate the coefficients for the equations …

for (int j = 0; j <= NthDegree; j++)

{

#region Calculate the coefficients for the jth equation …

#region Calculate the constant term for this equation …

coeffs[j, NthDegree + 1] = 0;

foreach(DataPair Pair in DataPairs)

{

coeffs[j, NthDegree + 1] -= Math.Pow(Pair.X, j) * Pair.Y;

}

#endregion

#region Calculate the other coefficients …

for (int a_sub = 0; a_sub <= NthDegree; a_sub++)

{

#region Calculate the dth coefficient.

coeffs[j, a_sub] = 0;

foreach(DataPair Pair in DataPairs)

{

coeffs[j, a_sub] -= Math.Pow(Pair.X, a_sub + j);

}

#endregion

}

#endregion

#endregion

}

#endregion

#region Solve the equations and return …

return GaussianElimination(DataPairs, coeffs);

#endregion

}

catch(Exception ex)

{

errHandler(ex);

return new Tuple<List, bool> (new List(), false);

}

}

#endregion

#region Helper Functions …

///

/// Loads the augmented array for Gaussian Elimination.

/// Tuple Definitions: Item1 = The Array; Item2 = num_rows; Item3 = num_cols; Item4 = Success.

/// Column num_cols holds the result values.

/// Column num_cols + 1 will hold the variables’ final values after backsolving.

///

///

///

///

private Tuple LoadArray(List DataPairs, double[,] coeffs)

{

try

{

int num_rows = coeffs.GetUpperBound(0) + 1;

int num_cols = coeffs.GetUpperBound(1) + 1;

#region Obsolete Code (Original Rod Stevens code.) …

//TextBox txtValues = new TextBox();

//TextBox txtCoefficients = new TextBox();

// Build the augmented matrix.

//string[] value_rows = txtValues.Text.Split(

// new string[] {“\r\n”},

// StringSplitOptions.RemoveEmptyEntries);

//string[] coef_rows = txtCoefficients.Text.Split(

// new string[] { “\r\n” },

// StringSplitOptions.RemoveEmptyEntries);

//string[] one_row = coef_rows[0].Split(

// new string[] { ” ” },

// StringSplitOptions.RemoveEmptyEntries);

//num_rows = coef_rows.GetUpperBound(0) + 1;

//num_cols = one_row.GetUpperBound(0) + 1;

//num_rows = coeffs.GetLength(0);

//num_cols = coeffs.GetLength(1);

#endregion

double[,] arr = new double[num_rows, num_cols + 2];

for (int r = 0; r < num_rows; r++)

{ //Original Rod Stevens Code:

//one_row = coef_rows[r].Split(

// new char[] {' '},

// StringSplitOptions.RemoveEmptyEntries);

for (int c = 0; c < num_cols; c++)

{

arr[r, c] = coeffs[r, c];//double.Parse(one_row[c]);

}

arr[r, num_cols] = DataPairs[r].X;// double.Parse(value_rows[r]);

}

return new Tuple (arr, num_rows, num_cols, true);

}

catch(Exception ex)

{

errHandler(ex);

return new Tuple (null, 0, 0, false);

}

}

///

/// Solve the system of equations using Gaussian Elimination.

///

///

///

///

private Tuple<List, bool> GaussianElimination(List DataPairs, double[,] coeffs)

{ //http://csharphelper.com/blog/2014/10/solve-a-system-of-equations-with-gaussian-elimination-in-c/

try

{

#region Setup …

Coefficients = new List();

const double tiny = 0.00000000001;

#endregion

#region Build the augmented matrix …

// The values num_rows and num_cols are the number of rows

// and columns in the matrix, not the augmented matrix.

//int num_rows, num_cols;

// double[,] arr = LoadArray(DataPairs, coeffs, out num_rows, out num_cols);

Tuple Results = LoadArray(DataPairs, coeffs);

if(!Results.Item4)

{

return new Tuple<List, bool>(new List(), false);

}

double[,] arr = Results.Item1;

int num_rows = Results.Item2;

int num_cols = Results.Item3;

#endregion

// This is probably not used any more:

double[,] orig_arr = (double[,]) arr.Clone();

#region Start solving …

for (int r = 0; r < num_rows – 1; r++)

{

#region

// Zero out all entries in column r after this row.

// See if this row has a non-zero entry in column r.

if (Math.Abs(arr[r, r]) < tiny)

{

#region Too close to zero. Try to swap with a later row …

for (int r2 = r + 1; r2 tiny)

{

#region This row will work. Swap them …

for (int c = 0; c tiny)

{

#region Zero out this column in later rows …

for (int r2 = r + 1; r2 < num_rows; r2++)

{

#region

double factor = -arr[r2, r] / arr[r, r];

for (int c = r; c <= num_cols; c++)

{

arr[r2, c] = arr[r2, c] + factor * arr[r, c];

}

#endregion

}

#endregion

}

#endregion

#endregion

}

#endregion

#region See if there is a solution and process …

if (arr[num_rows – 1, num_cols – 1] == 0)

{

#region There is no solution …

// See if all of the entries in this row are 0.

bool all_zeros = true;

for (int c = 0; c = 0; r–)

{

#region

double tmp = arr[r, num_cols];

for (int r2 = r + 1; r2 < num_rows; r2++)

{

tmp -= arr[r, r2] * arr[r2, num_cols + 1];

}

arr[r, num_cols + 1] = tmp / arr[r, r];

#endregion

}

#region Transfer the Coefficients to the List …

for (int r = 0; r < num_rows; r++)

{

Coefficients.Add(arr[r, num_cols + 1]);

//Original Rod Stevens Code:

//txt += "\r\nx" + r.ToString() + " = " +

// arr[r, num_cols + 1].ToString();

}

#endregion

#region Verify (Disabled) …

//string txt = "Check:" + Environment.NewLine;

//for(int r = 0; r < num_rows; r++)

//{

// double tmp = 0;

// for(int c = 0; c < num_cols; c++)

// {

// tmp += orig_arr[r, c] * arr[c, num_cols + 1];

// }

// txt += "\r\n" + tmp.ToString();

//}

//txt = txt.Substring("\r\n".Length + 1);

//MessageBox.Show(txt);

#endregion

#endregion

}

#endregion

return new Tuple<List, bool> (Coefficients, true);

}

catch(Exception ex)

{

errHandler(ex);

return new Tuple<List, bool> (new List(), false);

}

}

#endregion

#region Error Analysis …

public double ErrorSquared(List points)

{

double total = 0;

foreach (DataPair pt in points)

{

double dy = pt.Y – PredictY(pt.X);

total += dy * dy;

}

return total;

}

#endregion

#region Predictors …

public double PredictY(double XValue)

{

try

{

double RetVal = 0.0;

int CurrentPower = 0;

foreach(double dbl in Coefficients)

{

RetVal += dbl * Math.Pow(XValue, CurrentPower);

CurrentPower += 1;

}

return RetVal;

}

catch (Exception ex)

{

errHandler(ex);

return InvalidReturn;

}

}

#endregion

} // End of Class.

} // End of Namespace.

Second, if the dots form a square with sides parallel to the X and Y axes, then you could use them to define a `Rectangle` object and use the `Graphics` object’s `DrawRectangle`` method. If you know that the dots form this arrangement, then you can find the minimum X and Y coordinates to get the ``Rectangle` object's upper left corner. Then find the maximum X and Y coordinates and subtract to get the width and height.

Probably the most general approach would be to put the points in an array and then use the `Graphics` object's `DrawPolygon` method. That will work with rotated squares and will make the lines connect nicely, unlike the first approach where the corners may not fit together perfectly depending on the line's properties. (I.e. thickness and the pen's `LineJoin` property.)

As for how to figure out which dots make squares, see these posts:

]]>If that’s not what you need, I don’t understand what you have in mind. Let me know and I’m sure I can come up with an example.

]]>ALL other illegal locations will be correctly validated as out of bounds because they will not be found in the dictionary.

I just spent 3 hours on this because I’m a bad developer and maybe this will help other bad developers ðŸ˜‰

Quick solution : If row = 0 And ClickHeight < TileHeight / 2 And col Mod 2 = 1 Then OutOfBounds = True ]]>

var xml_formatted = XDocument.Parse (xml_not_formatted).ToString (); ]]>