Solving equation to find center point of circle from 3 points - c#

I'm looking for a high precision solution to find the center point of a circle from 3 data points on a canvas (x,y). I found this example in the attached screenshot above, now I'm using the Math.NET package to solve the equation and I'm comparing the results against this online tool: https://planetcalc.com/8116/.
However, when I calculate the radius its completely off and often a negative number???
using MathNet.Numerics.LinearAlgebra.Double.Solvers;
using MathNet.Numerics.LinearAlgebra.Double;
using System;
namespace ConsoleAppTestBed
{
class Program
{
static void Main(string[] args)
{
var dataPoints = new double[,]
{
{ 5, 80 },
{ 20, 100 },
{ 40, 140 }
};
var fitter = new CircleFitter();
var result = fitter.Fit(dataPoints);
var x = -result[0];
var y = -result[1];
var c = result[2];
Console.WriteLine("Center Point:");
Console.WriteLine(x);
Console.WriteLine(y);
Console.WriteLine(c);
//// (x^2 + y^2 - c^2)
var radius = Math.Pow(x, 2) + Math.Pow(y, 2) - Math.Pow(c, 2);
//// sqrt((x^2 + y^2 - c^2))
radius = Math.Sqrt(radius);
Console.WriteLine("Radius:");
Console.WriteLine(radius);
Console.ReadLine();
}
public class CircleFitter
{
public double[] Fit(double[,] v)
{
var xy1 = new double[] { v[0,0], v[0,1] };
var xy2= new double[] { v[1, 0], v[1, 1] };
var xy3 = new double[] { v[2, 0], v[2, 1] };
// Create Left Side Matrix of Equation
var a = CreateLeftSide_(xy1);
var b = CreateLeftSide_(xy2);
var c = CreateLeftSide_(xy3);
var matrixA = DenseMatrix.OfArray(new[,]
{
{ a[0], a[1], a[2] },
{ b[0], b[1], b[2] },
{ c[0], c[1], c[2] }
});
// Create Right Side Vector of Equation
var d = CreateRightSide_(xy1);
var e = CreateRightSide_(xy2);
var f = CreateRightSide_(xy3);
double[] vector = { d, e, f };
var vectorB = Vector<double>.Build.Dense(vector);
// Solve Equation
var r = matrixA.Solve(vectorB);
var result = r.ToArray();
return result;
}
//2x, 2y, 1
public double[] CreateLeftSide_(double[] d)
{
return new double[] { (2 * d[0]), (2 * d[1]) , 1};
}
// -(x^2 + y^2)
public double CreateRightSide_(double[] d)
{
return -(Math.Pow(d[0], 2) + Math.Pow(d[1], 2));
}
}
}
}
Any ideas?
Thanks in advance.

The solution to your problem is here: The NumberDecimalDigits property
Code:
using System;
using System.Globalization;
namespace ConsoleApp1
{
class Program
{
static void Main()
{
double x1 = 1, y1 = 1;
double x2 = 2, y2 = 4;
double x3 = 5, y3 = -3;
findCircle(x1, y1, x2, y2, x3, y3);
Console.ReadKey();
}
static void findCircle(double x1, double y1,
double x2, double y2,
double x3, double y3)
{
NumberFormatInfo setPrecision = new NumberFormatInfo();
setPrecision.NumberDecimalDigits = 3; // 3 digits after the double point
double x12 = x1 - x2;
double x13 = x1 - x3;
double y12 = y1 - y2;
double y13 = y1 - y3;
double y31 = y3 - y1;
double y21 = y2 - y1;
double x31 = x3 - x1;
double x21 = x2 - x1;
double sx13 = (double)(Math.Pow(x1, 2) -
Math.Pow(x3, 2));
double sy13 = (double)(Math.Pow(y1, 2) -
Math.Pow(y3, 2));
double sx21 = (double)(Math.Pow(x2, 2) -
Math.Pow(x1, 2));
double sy21 = (double)(Math.Pow(y2, 2) -
Math.Pow(y1, 2));
double f = ((sx13) * (x12)
+ (sy13) * (x12)
+ (sx21) * (x13)
+ (sy21) * (x13))
/ (2 * ((y31) * (x12) - (y21) * (x13)));
double g = ((sx13) * (y12)
+ (sy13) * (y12)
+ (sx21) * (y13)
+ (sy21) * (y13))
/ (2 * ((x31) * (y12) - (x21) * (y13)));
double c = -(double)Math.Pow(x1, 2) - (double)Math.Pow(y1, 2) -
2 * g * x1 - 2 * f * y1;
double h = -g;
double k = -f;
double sqr_of_r = h * h + k * k - c;
// r is the radius
double r = Math.Round(Math.Sqrt(sqr_of_r), 5);
Console.WriteLine("Center of a circle: x = " + h.ToString("N", setPrecision) +
", y = " + k.ToString("N", setPrecision));
Console.WriteLine("Radius: " + r.ToString("N", setPrecision));
}
}
}

I have just converted William Li's answer to Swift 5 for
those who like to cmd+C and cmd+V like me :)
func calculateCircle(){
let x1:Float = 0
let y1:Float = 0
let x2:Float = 0.5
let y2:Float = 0.5
let x3:Float = 1
let y3:Float = 0
let x12 = x1 - x2
let x13 = x1 - x3
let y12 = y1 - y2
let y13 = y1 - y3
let y31 = y3 - y1
let y21 = y2 - y1
let x31 = x3 - x1
let x21 = x2 - x1
let sx13 = pow(x1, 2) - pow(x3, 2)
let sy13 = pow(y1, 2) - pow(y3, 2)
let sx21 = pow(x2, 2) - pow(x1, 2)
let sy21 = pow(y2, 2) - pow(y1, 2)
let f = ((sx13) * (x12)
+ (sy13) * (x12)
+ (sx21) * (x13)
+ (sy21) * (x13))
/ (2 * ((y31) * (x12) - (y21) * (x13)))
let g = ((sx13) * (y12)
+ (sy13) * (y12)
+ (sx21) * (y13)
+ (sy21) * (y13))
/ (2 * ((x31) * (y12) - (x21) * (y13)))
let c = -pow(x1, 2) - pow(y1, 2) - 2 * g * x1 - 2 * f * y1
let h = -g
let k = -f
let r = sqrt(h * h + k * k - c)
print("center x = \(h)")
print("center y = \(k)")
print("r = \(r)")
}

Updated Answer
The equation for radius is incorrect; it should be (not c squared):
which is why you get incorrect values for the radius.
The original answer was incorrect, but it is still 'interesting'.
(Incorrect) Original Answer
The cause of the problematic calculation is not solely due to the precision of the numbers, but more because problem is ill-conditioned. If you look at the three points and where they are located on the circle, you'll see that they are bunched on a small segment of the circumference. When the points are so close to each other, it is a tough ask to find the circle's centre and radius accurately.
So the intermediate calculations, which will have small rounding errors, result in hugely exaggerated errors.
You can see the ill-conditioned nature of the problem by adding the ConditionNumber() method.
// Solve Equation
Console.WriteLine(matrixA.ConditionNumber()); // <<< Returns 5800 -> Big!
var r = matrixA.Solve(vectorB); // Existing code
A large result indicates an ill-conditioned problem. In this case a value of 5800 is returned, which is large. You might get better results using Gaussian Elimination with partial pivoting, but it still does not address the fact that the basic problem is ill-conditioned, which is why you get wildly incorrect answers.

Related

How to minimize a non-linear function with constraints in c#?

I would like to minimize the following function
with constraints
in C#. I tried to do it with Math.Net's Newton Method, but I can't figure out how to do it. How can I minimize the function programmatically in C# for given $F_1, F_2$?
Update:
After the comment of #MinosIllyrien I tried the following, but I don't get the syntax:
_f1 = 0.3; // Global fields.
_f2 = 0.7;
var minimizer = new NewtonMinimizer(1E-4, 100, false);
var objectiveFunction = ObjectiveFunction.ScalarDerivative(FunctionToMinimize, GradientOfFunctionToMinimize);
var firstGuess = CreateVector.DenseOfArray(new[] {0.5});
var minimalWeight1 = minimizer.FindMinimum(objectiveFunction, firstGuess).MinimizingPoint;
private double GradientOfFunctionToMinimize(double w1){
return _f1 - (w1 * _f2) / Math.Sqrt(1 - Math.Pow(w1, 2));
}
private double FunctionToMinimize(double w1){
return w1 * _f1 + Math.Sqrt(1 - Math.Pow(w1, 2)) * _f2;
}
This does not work, because FindMinimum method requires IObjectiveFunction as function and not IScalarObjectiveFunction...
Update 2:
I tried a solution from Google:
var solver = Solver.CreateSolver("GLOP");
Variable w1 = solver.MakeNumVar(0.0, double.PositiveInfinity, "w1");
Variable w2 = solver.MakeNumVar(0.0, double.PositiveInfinity, "w2");
solver.Add(Math.Sqrt(w1*w1 + w2*w2) == 1);
This throws the error that *-operator cannot be used for "Variable" and "Variable". Someone any ideas?
w₁² + w₂² = 1 is basically the unit circle.
The unit circle can also be described by the following parametric equation:
(cos t, sin t)
In other words, for every pair (w₁, w₂),
there is an angle t for which w₁ = cos t and w₂ = sin t.
With that substitution, the function becomes:
y = F₁ cos t + F₂ sin t
w₁ ≥ 0, w₂ ≥ 0 restricts t to a single quadrant. This leaves you with a very simple constraint, that consists of a single variable:
0 ≤ t ≤ ½π
By the way, the function can be simplified to:
y = R cos(t - α)
where R = √(F₁² + F₂²) and α = atan2(F₂, F₁)
This is a simple sine wave. Without the constraint on t, its range would be [-R, R], making the minimum -R. But the constraint limits the domain and thereby the range:
If F₁ < 0 and F₂ < 0, then the minimum is at w₁ = -F₁ / R, w₂ = -F₂ / R, with y = -R
For 0 < F₁ ≤ F₂, a minimum is at w₁ = 1, w₂ = 0, with y = F₁
For 0 < F₂ ≤ F₁, a minimum is at w₁ = 0, w₂ = 1, with y = F₂
Notes:
if F₁ = F₂ > 0, then you have two minima.
if F₁ = F₂ = 0, then y is just flat zero everywhere.
In code:
_f1 = 0.3;
_f2 = 0.7;
if (_f1 == 0.0 && _f2 == 0.0) {
Console.WriteLine("Constant y = 0 across the entire domain");
}
else if (_f1 < 0.0 && _f2 < 0.0) {
var R = Math.sqrt(_f1 * _f1 + _f2 * _f2);
Console.WriteLine($"Minimum y = {-R} at w1 = {-_f1 / R}, w2 = {-_f2 / R}");
}
else {
if (_f1 <= _f2) {
Console.WriteLine($"Minimum y = {_f1} at w1 = 1, w2 = 0");
}
if (_f1 >= _f2) {
Console.WriteLine($"Minimum y = {_f2} at w1 = 0, w2 = 1");
}
}

Swiss projection coordinates to ellipsoidal coordinates

I am trying to convert CH1903+ coordinates into WGS84 coordinates based on the following example provided by the swiss "Bundesamt für Statistik" using C#.
Example:
As far i can calculate all values like shown in the example. But in the end when i try to calculate the variable "S" based on the values in the example i am getting wrong results.
double E = 2679520.05;
double alpha = 1.00072913843038;
double phi = 0.820535226;
double b = 0.820535226;
double K = 0.0030667323772751
I tried both implementations:
double S = Math.Log(Math.Tan(Math.PI / 4.0 + phi / 2.0)); --> result: 0.931969600979248
or
double S = 1/alpha * (Math.Log(Math.PI/4 + b/2) - K) + E * Math.Log(Math.Tan((Math.PI/4) + (Math.Asin(E * Math.Sin(phi))/2.0))); --> result: NaN
Can somebody tell what's wrong in my implementation that i am getting this wrong results? If i understand the example correctly both calculations should return 0.933114264192610 as result for the given values.
This is my implementation for converting swiss CH1903+ coordinates -> decimal coordinates (WGS84) which works good so far
Example CH1903+ Coordinates N = 1249251.544, E = 2645320.487;
// swiss CH1903+ coordinates
double N = 1249251.544,
E = 2645320.487;
const double K = 0.0030667323772751; // Konstante der Breitenformel
const double BESSEL_GH = 6377397.155; // grosse Halbachse des Bessel-Ellipsoids
const double BESSEL_SH = 6356078.963; // kleine Halbachse des Bessel-Ellipsoids
double GRS_GH = 6.378137 * Math.Pow(10, 6); // grosse Halbachse des GRS84-Ellipsoids
double GRS_SH = 6.356752314 * Math.Pow(10, 6); ; // kleine Halbachse des GRS84-Ellipsoids
double E2_BESSEL = (Math.Pow(BESSEL_GH, 2) - Math.Pow(BESSEL_SH, 2)) / Math.Pow(BESSEL_GH, 2), // 1.numerische Exzentrizität (im Quadrat) des Bessel-Ellipsoids
E_BESSEL = Math.Sqrt(E2_BESSEL), // 1.numerische Exzentrizität des Bessel-Ellipsoids
E2_GRS = (Math.Pow(GRS_GH, 2) - Math.Pow(GRS_SH, 2)) / Math.Pow(GRS_GH, 2); // 1.numerische Exzentrizität (im Quadrat) des GRS84-Ellipsoids
const double TOLERANCE = 0.0000000000000001;
///// swiss coordinates -> ellipsoid coordinates /////
double
Y = E - 2600000.0, // swiss CH1903 coordinates
X = N - 1200000.0, // swiss CH1903 coordinates
PHI_NULL = (46 + 57 / 60.0 + 8.66 / 3600.0) * Math.PI / 180, // geogr. Breite des Nullpunkts in Bern
LAMBDA_NULL = (7 + 26 / 60.0 + 22.5 / 3600.0) * Math.PI / 180, // geogr. Länge des Nullpunkts in Bern
R = (BESSEL_GH * Math.Sqrt(1 - E2_BESSEL)) / (1 - E2_BESSEL * Math.Pow(Math.Sin(PHI_NULL), 2)), // Radius der Projektionskugel
ALPHA = Math.Sqrt(1 + E2_BESSEL / (1 - E2_BESSEL) * Math.Pow(Math.Cos(PHI_NULL), 4)), // Verhältnis Kugellänge zu Ellipsoidlänge
B_GLOBAL_NULL = Math.Asin(Math.Sin(PHI_NULL) / ALPHA), // Breite des Nullpunkts auf der Kugel
L_PSEUDO = Y / R, // Kugelkoordinaten bezüglich Pseudoäquatorsystem in Bern
B_PSEUDO = 2 * (Math.Atan(Math.Pow(Math.E, (X / R))) - (Math.PI / 4)), // Kugelkoordinaten bezüglich Pseudoäquatorsystem in Bern
L = Math.Atan(Math.Sin(L_PSEUDO) / (Math.Cos(B_GLOBAL_NULL) * Math.Cos(L_PSEUDO) - Math.Sin(B_GLOBAL_NULL) * Math.Tan(B_PSEUDO))), // Kugelkoordinaten bezüglich Nullpunkt Bern
B = Math.Asin(Math.Cos(B_GLOBAL_NULL) * Math.Sin(B_PSEUDO) + Math.Sin(B_GLOBAL_NULL) * Math.Cos(B_PSEUDO) * Math.Cos(L_PSEUDO)), // Kugelkoordinaten bezüglich Nullpunkt Bern
LAMBDA = LAMBDA_NULL + L / ALPHA,
PHI = B,
S = (Math.Log(Math.Tan(Math.PI / 4.0 + B / 2.0)) - K) / ALPHA + E_BESSEL * Math.Log(Math.Tan(Math.PI / 4.0 + Math.Asin(E_BESSEL * Math.Sin(PHI)) / 2.0));
bool cont = true;
// iterate to tolerance
while (cont)
{
double PHI_TMP = 2 * Math.Atan(Math.Pow(Math.E, S)) - Math.PI / 2;
double S_TMP = (Math.Log(Math.Tan(Math.PI / 4 + B / 2)) - K) / ALPHA + E_BESSEL * Math.Log(Math.Tan(Math.PI / 4 + Math.Asin(E_BESSEL * Math.Sin(PHI_TMP)) / 2));
if (Math.Abs(PHI - PHI_TMP) < TOLERANCE)
{
cont = false;
}
else
{
PHI = PHI_TMP;
S = S_TMP;
}
}
///// ellipsoid coordinates (CH1903) -> karth. coordinates (CH1903) /////
double RN_CH1903 = BESSEL_GH / (Math.Sqrt(1 - E2_BESSEL * Math.Pow(Math.Sin(PHI), 2))), // Normalkrümmungsradius
X_KARTH = RN_CH1903 * Math.Cos(PHI) * Math.Cos(LAMBDA),
Y_KARTH = RN_CH1903 * Math.Cos(PHI) * Math.Sin(LAMBDA),
Z_KARTH = (RN_CH1903 * (1 - E2_BESSEL)) * Math.Sin(PHI);
///// karth. coordinates (CH1903) -> karth. coordinates (ETRS89/CHTRS95) /////
double X_CH1930P = X_KARTH + 674.374,
Y_CH1930P = Y_KARTH + 15.056,
Z_CH1930P = Z_KARTH + 405.346;
///// karth. coordinates (ETRS89/CHTRS95) -> ellipsoid coordinates (ETRS89/CHTRS95) /////
double PHI_ETRS = Math.Atan(Z_CH1930P / Math.Sqrt(Math.Pow(X_CH1930P, 2) + Math.Pow(Y_CH1930P, 2))),
RN_ETRS = BESSEL_GH / (Math.Sqrt(1 - E2_GRS * Math.Pow(Math.Sin(PHI_ETRS), 2)));
cont = true;
// iterate to tolerance
while (cont)
{
double PHI_ETRS_TMP = Math.Atan((Z_CH1930P / (Math.Sqrt(Math.Pow(X_CH1930P, 2) + Math.Pow(Y_CH1930P, 2)))) / (1 - (RN_ETRS * E2_GRS) / (RN_ETRS + 0))),
RN_ETRS_TMP = BESSEL_GH / (Math.Sqrt(1 - E2_GRS * Math.Pow(Math.Sin(PHI_ETRS_TMP), 2)));
if (Math.Abs(PHI_ETRS - PHI_ETRS_TMP) < TOLERANCE)
{
cont = false;
}
else
{
PHI_ETRS = PHI_ETRS_TMP;
RN_ETRS = RN_ETRS_TMP;
}
}
double LAMBDA_ETRS = Math.Atan(Y_CH1930P / X_CH1930P);
///// ellipsoid coordinates (ETRS89/CHTRS95) -> decimal coordinates (WGS84) /////
double lat = PHI_ETRS * 180 / Math.PI;
double lon = LAMBDA_ETRS * 180 / Math.PI;

How to Draw a Gaussian Curve in c#

I have a Histogram statistics bar chart with below data.
Count, HistogramBin
0, -1615.25
0, -1056.42
0, -497.48
1, 61.25
1, 620.05
1, 1178.92
0, 1737.76
0, 2296.59
I need to form Gauss curve based on above values. Could anyone guide me how to achieve the same.
I have written a function based on Wikipedia link: https://en.wikipedia.org/wiki/Gaussian_function
Our average is : 340.67
SD: Standard deviation: 488.98001098632812
private DataTable GenerateGaussTable1(DataTable histogramDataTable,
HistogramValueItem histogramValueDBItem)
{
double amplitude = (Average + 3 * Sigma) / 2;
double mean = Average;
double sd = Sigma;
DataTable dt = new DataTable();
dt.Columns.Add("x", typeof(float));
dt.Columns.Add("Y", typeof(float));
foreach (DataRow row in histogramDataTable.Rows)// top provided data
{
double x = Convert.ToDouble(row[1]) / 2;
double var1 = 1 / sd * Math.Sqrt(2 * 3.14);
double var2 = -0.5 * Math.Pow((x - mean)/sd, 2);
double var4= Math.Exp(var2);
double var5 = var1 * var4;
// Y = Amplitude * exp(-0.5 * ((X - Mean) / SD) ^ 2)
double y = var5;
dt.Rows.Add((float)x, (float)y);
}
return dt;
}
Here is my code:
double gauss(double x, double a, double b, double c)
{
var v1 = ( x - b) / (2d * c * c);
var v2 = -v1 * v1 / 2d;
var v3 = a * Math.Exp(v2);
return v3;
}
and:
private void button_Click(object sender, EventArgs e)
{
Series s1 = chart2.Series[0];
s1.ChartType = SeriesChartType.Line;
s1.Name = "Line";
Series s2 = chart2.Series.Add("Spline");
s2.ChartType = SeriesChartType.Spline;
double avg = 1.8;
double amp = 3;
double sd = 0.53;
List<double> xes = new List<double>
{ 0, 0, 0.05, 0.1, 0.4, 0.9, 1.3, 1.6, 2, 2.4, 2.8, 3.2, 4 };
foreach (var x in xes)
{
s1.Points.AddXY(x, gauss(x, amp, avg, sd));
s2.Points.AddXY(x, gauss(x, amp, avg, sd));
}
}
The math was taken from wikipedia
I think your SD is way too large to create a bell curve; try dividing by 10-100..! - Of course your SD actually is very large and so you really won't get a meaningful bell curve for those data..
I've tried your function, but it gives wrong curves,
The gauss function is wrong, why do you use "2d"?
Here the function :
so first, v1 = (x-b). Then v2 = (x-b)² / 2 c²
And finaly v3 = a exp (v2)
double gauss(double x, double a, double b, double c)
{
var v1 = (x - b);
var v2 = (v1 * v1) / (2 * (c*c));
var v3 = a * Math.Exp(-v2);
return v3;
}
After this fix, the curves are much better.

2D Trilateration using linear least squares in C#

I am doing 2D trilateration using the MathNet for the matrices and vectors. This i my code:
public static double[] trilaterate2DLinear(double[] pA, double[] pB, double[] pC, double rA, double rB, double rC) {
//Convert doubles to vectors for processing
Vector<double> vA = Vector<double>.Build.Dense(pA);
Vector<double> vB = Vector<double>.Build.Dense(pB);
Vector<double> vC = Vector<double>.Build.Dense(pC);
//Declare elements of b vector
//bBA = 1/2 * (rA^2 - rB^2 + dBA^2)
double[] b = {0, 0};
b[0] = 0.5 * (Math.Pow(rA, 2) - Math.Pow(rB, 2) + Math.Pow(getDistance(pB, pA), 2));
b[1] = 0.5 * (Math.Pow(rA, 2) - Math.Pow(rC, 2) + Math.Pow(getDistance(pC, pA), 2));
//Convert b array to vector form
Vector<double> vb = Vector<double>.Build.Dense(b);
//Build A array
//A = {x2 -x1, y2 - y1}
// {x3 - x1, y3 - y1}
double[,] A = { { pB[0] - pA[0], pB[1] - pA[1] }, { pC[0] - pA[0], pC[1] - pA[1] } };
//Convert A to Matrix form
Matrix<double> mA = Matrix<double>.Build.DenseOfArray(A);
//Declare Transpose of A matrix;
Matrix<double> mAT = mA.Transpose();
//Declare solution vector x to 0
Vector<double> x = Vector<double>.Build.Dense(2);
//Check if A*AT is non-singular (non 0 determinant)
if (mA.Multiply(mAT).Determinant() == 0)
{
//x = ((AT * A)^-1)*AT*b
x = (((mA.Multiply(mAT)).Inverse()).Multiply(mAT)).Multiply(vb);
}
else
{
//TODO case for A*AT to be singular
x = (((mA.Multiply(mAT)).Inverse()).Multiply(mAT)).Multiply(vb);
}
//final position is x + vA
//return as double so as not
return (x.Add(vA)).ToArray();
}
//Gets the Euclidean distance between two points
private static double getDistance(double[] p1, double[] p2)
{
//d^2 = (p1[0] - p2[0])^2 + (p1[1] - p2[1]);
double distSquared = Math.Pow((p1[0] - p2[0]),2) + Math.Pow((p1[1] - p2[1]),2);
return Math.Sqrt(distSquared);
}
pA, pB & pC are the coordinates of the the Beacons and rA, rB & rC are the distances from the each beacon to the user. Is there anything obvious I am doing wrong? Maybe the order of Matrix multiplications need to change but I am not familiar enough with the Linear Least Squares to be able to track the Matrices and tell.
Solved. The if statement condition and calculations inside the if statement where wrong.
Correction:
public static double[] trilaterate2DLinear(double[] pA, double[] pB, double[] pC, double rA, double rB, double rC) {
//Convert doubles to vectors for processing
Vector<double> vA = Vector<double>.Build.Dense(pA);
Vector<double> vB = Vector<double>.Build.Dense(pB);
Vector<double> vC = Vector<double>.Build.Dense(pC);
//Declare elements of b vector
//bBA = 1/2 * (rA^2 - rB^2 + dBA^2)
double[] b = {0, 0};
b[0] = 0.5 * (Math.Pow(rA, 2) - Math.Pow(rB, 2) + Math.Pow(getDistance(pB, pA), 2));
b[1] = 0.5 * (Math.Pow(rA, 2) - Math.Pow(rC, 2) + Math.Pow(getDistance(pC, pA), 2));
//Convert b array to vector form
Vector<double> vb = Vector<double>.Build.Dense(b);
//Build A array
//A = {x2 -x1, y2 - y1}
// {x3 - x1, y3 - y1}
double[,] A = { { pB[0] - pA[0], pB[1] - pA[1] }, { pC[0] - pA[0], pC[1] - pA[1] } };
//Convert A to Matrix form
Matrix<double> mA = Matrix<double>.Build.DenseOfArray(A);
//Declare Transpose of A matrix;
Matrix<double> mAT = mA.Transpose();
//Declare solution vector x to 0
Vector<double> x = Vector<double>.Build.Dense(2);
//Check if A*AT is non-singular (non 0 determinant)
double det = mA.Multiply(mAT).Determinant();
if (mA.Multiply(mAT).Determinant() > 0.1)
{
//x = ((AT * A)^-1)*AT*b
// x = (((mA.Multiply(mAT)).Inverse()).Multiply(mAT)).Multiply(vb);
x = (mA.Transpose() * mA).Inverse() * (mA.Transpose() * vb);
}
else
{
//TODO case for A*AT to be singular
x = (((mA.Multiply(mAT)).Inverse()).Multiply(mAT)).Multiply(vb);
}
//final position is x + vA
//return as double so as not
return (x.Add(vA)).ToArray();
}
You are not calculating the B vector correctly.
It should be:
//dBA = 0.5 * (rA^2 - rB^2 - length_vA^2 + length_vB^2)
b[0] = 0.5 * (Math.Pow(rA, 2) - Math.Pow(rB, 2) - Math.Pow(getDistance(pA,{0,0}), 2) + Math.Pow(getDistance(pB,{0,0}), 2));
b[1] = 0.5 * (Math.Pow(rA, 2) - Math.Pow(rC, 2) - Math.Pow(getDistance(pA,{0,0}), 2) + Math.Pow(getDistance(pC,{0,0}), 2));

How would i work out magnitude quickly for 3 values?

How can I use a Fast Magnitude calculation for 3 values (instead of using square root)? (+/- 3% is good enough)
public void RGBToComparison(Color32[] color)
{
DateTime start = DateTime.Now;
foreach (Color32 i in color)
{
var r = PivotRgb(i.r / 255.0);
var g = PivotRgb(i.g / 255.0);
var b = PivotRgb(i.b / 255.0);
var X = r * 0.4124 + g * 0.3576 + b * 0.1805;
var Y = r * 0.2126 + g * 0.7152 + b * 0.0722;
var Z = r * 0.0193 + g * 0.1192 + b * 0.9505;
var LB = PivotXyz(X / 95.047);
var AB = PivotXyz(Y / 100);
var BB = PivotXyz(Z / 108.883);
var L = Math.Max(0, 116 * AB - 16);
var A = 500 * (LB - AB);
var B = 200 * (AB - BB);
totalDifference += Math.Sqrt((L-LT)*(L-LT) + (A-AT)*(A-AT) + (B-BT)*(B-BT));
}
totalDifference = totalDifference / color.Length;
text.text = "Amount of Pixels: " + color.Length + " Time(MilliSeconds):" + DateTime.Now.Subtract(start).TotalMilliseconds + " Score (0 to 100)" + (totalDifference).ToString();
RandomOrNot();
}
private static double PivotRgb(double n)
{
return (n > 0.04045 ? Math.Pow((n + 0.055) / 1.055, 2.4) : n / 12.92) * 100.0;
}
private static double PivotXyz(double n)
{
return n > 0.008856 ? CubicRoot(n) : (903.3 * n + 16) / 116;
}
private static double CubicRoot(double n)
{
return Math.Pow(n, 1.0 / 3.0);
}
This is the important part: totalDifference += Math.Sqrt((L-LT)*(L-LT) + (A-AT)*(A-AT) + (B-BT)*(B-BT));
I know there are FastMagnitude calculations online, but all the ones online are for two values, not three. For example, could i use the difference between the values to get a precise answer? (By implementing the difference value into the equation, and if the difference percentage-wise is big, falling back onto square root?)
Adding up the values and iterating the square root every 4 pixels is a last resort that I could do. But firstly, I want to find out if it is possible to have a good FastMagnitude calculation for 3 values.
I know I can multi-thread and parllelize it, but I want to optimize my code before I do that.
If you just want to compare the values, why not leave the square root out and work with the length squared?
Or use the taylor series of the square root of 1+x and cut off early :)

Categories