#include <iostream>
#include <Eigen/StdVector>
#include <Eigen/Geometry>
#include <stdio.h>
#include <vector>
using namespace Eigen;
using namespace std;
struct Point3D
{
float x;
float y;
float z;
Point3D(float _x, float _y, float _z)
{
x = _x;
y = _y;
z = _z;
}
Point3D()
{
Point3D(0,0,0);
}
};
// 结构体表示二维点
struct Point
{
float x;
float y;
Point()
{
x = 0;
y = 0;
}
Point(float _x, float _y)
{
x = _x;
y = _y;
}
Point(Point3D& pt)
{
Point(pt.x, pt.y);
}
};
// 二维平面内计算直线拟合参数 y = kx + b
std::pair<double, double> linear_fitting_2D_points(const std::vector<Point>& points)
{
double sum_x = 0.0;
double sum_y = 0.0;
double sum_xy = 0.0;
double sum_x_squared = 0.0;
size_t n = points.size();
// 计算各项和
for (const auto& point : points) {
sum_x += point.x;
sum_y += point.y;
sum_xy += point.x * poi
三维空间内的直线最小二乘拟合(代码经过实测)
于 2024-04-10 23:31:47 首次发布