Skip to content

Instantly share code, notes, and snippets.

@Gengoz
Created June 21, 2017 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gengoz/de92300b63493700313c93dbde999bd7 to your computer and use it in GitHub Desktop.
Save Gengoz/de92300b63493700313c93dbde999bd7 to your computer and use it in GitHub Desktop.
//=====================Extended Leas Squire Method =========================
// Define variables
sum_x = 0;
sum_y = 0;
sum_xy = 0;
sum_x2 = 0;
sum_y2 = 0;
// Length section
n = sections[i][1] - sections[i][0];
for(int j = 0; j < n1 ;j++)
{
// Get x,y,xy,x^2 and y^2 from all coordinates
sum_x += Coordinates[sections[i][0]+j][0];
sum_y += Coordinates[sections[i][0]+j][1];
sum_xy += Coordinates[sections[i][0]+j][0]*Coordinates[sections[i][0]+j][1];
sum_x2 += pow(Coordinates[sections[i][0]+j][0],2);
sum_y2 += pow(Coordinates[sections[i][0]+j][1],2);
}
// Devide by number of coordinates
sum_x /= n;
sum_y /= n;
sum_xy /= n;
sum_x2 /= n;
sum_y2 /= n;
A = -(sum_xy -sum_x*sum_y);
Bx = sum_x2-sum_x*sum_x;
By = sum_y2-sum_y*sum_y;
// Define best solution, over x or y axis
if(fabs(Bx)<fabs(By))
{
// Least squire method over y axis
B = By;
std::swap(A,B);
}
else
{
// Least squire method over x axis
B=Bx;
}
C = - ( A*sum_x + B*sum_y);
// Solution of extended least squire method where line if defined as
// y = b*x+a
a = (-C/B);
b = (-A/B)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment