Вписывание кривой (best fit curve) - C#
Формулировка задачи:
Очень нужна программа для вписывания дуги чрез точки:
В качестве исходных данных имеется массив точек с координатами X и Y. В качестве решения должны получиться координаты X и Y центра кривой и ее радиус.
Уже второй день не могу найти пример на VB или C# ((
Решение задачи: «Вписывание кривой (best fit curve)»
textual
Листинг программы
public class Point
{
public Point (double x, double y)
{
X = x;
Y = y;
}
public double X;
public double Y;
}
public static class FitCurve
{
public static bool GetArcInfo (Point[] points, out double x, out double y, out double r)
{
x = 0;
y = 0;
r = 0;
if (points == null)
return false;
if (points.Length <= 2)
return false;
int n = 0;
for (int i = 0; i < points.Length-1; ++i)
for (int j = 0; j < points.Length-1; ++j) {
if (points [i].Y == points [i + 1].Y)
continue;
if (points [j].Y == points [j + 1].Y)
continue;
double Ai = (points [i].X - points [i + 1].X) / (points [i + 1].Y - points [i].Y);
double Aj = (points [j].X - points [j + 1].X) / (points [j + 1].Y - points [j].Y);
double Bi = (Math.Pow (points [i + 1].X, 2) - Math.Pow (points [i].X, 2) + Math.Pow (points [i + 1].Y, 2) - Math.Pow (points [i].Y, 2)) / (2 * (points [i + 1].Y - points [i].Y));
double Bj = (Math.Pow (points [j + 1].X, 2) - Math.Pow (points [j].X, 2) + Math.Pow (points [j + 1].Y, 2) - Math.Pow (points [j].Y, 2)) / (2 * (points [j + 1].Y - points [j].Y));
if (Ai == Aj)
continue;
double x0 = (Bj - Bi) / (Ai - Aj);
double y0 = (Ai * Bj - Aj * Bi) / (Ai - Aj);
x += x0;
y += y0;
r += Math.Sqrt (Math.Pow (points [i].X - x0, 2) + Math.Pow (points [i].Y - y0, 2));
r += Math.Sqrt (Math.Pow (points [j].X - x0, 2) + Math.Pow (points [j].Y - y0, 2));
++n;
}
if (n == 0)
return false;
r = r / (2 * n);
x /= n;
y /= n;
return true;
}
}