如何用bezier曲线拟合离散点 c++代码链接
时间: 2023-02-08 18:39:52 浏览: 66
使用bezier曲线拟合离散点可以使用第三方库如 scipy 和 scikit-learn。
scipy中的interpolate.splprep可以用来拟合离散点,示例代码如下:
```
from scipy.interpolate import splprep, splev
import numpy as np
# 离散点
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]
# 拟合
tck, u = splprep([x, y], s=0)
# 根据拟合结果计算新点
x_new, y_new = splev(np.linspace(0, 1, 100), tck)
```
scikit-learn 中的 make_interp_spline可以用来拟合离散点,示例代码如下:
```
from scipy.interpolate import make_interp_spline
import numpy as np
# 离散点
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]
# 拟合
spl = make_interp_spline(x, y, k=3)
# 根据拟合结果计算新点
x_new = np.linspace(1, 5, 100)
y_new = spl(x_new)
```
最后是一个具体的代码实现链接:https://codepen.io/abhinav_tiwari/pen/oNgZMxV
相关问题
拟合贝塞尔曲线c++代码
以下是一份C++版本的拟合贝塞尔曲线代码,可以实现二次和三次bezier曲线的绘制,具体实现方法和参数可以参考代码注释:
```
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
// 定义点的结构体
struct Point {
double x;
double y;
};
// 计算组合数C(n, i)
double Combination(int n, int i) {
double res = 1;
for (int j = 1; j <= i; j++) {
res *= (n - j + 1) / (double)j;
}
return res;
}
// 计算贝塞尔曲线上某一点的坐标
Point Bezier(vector<Point>& points, double t) {
int n = points.size() - 1;
double x = 0, y = 0;
for (int i = 0; i <= n; i++) {
double factor = Combination(n, i) * pow(t, i) * pow(1 - t, n - i);
x += factor * points[i].x;
y += factor * points[i].y;
}
return { x, y };
}
// 拟合二次贝塞尔曲线
vector<Point> FitQuadraticBezier(vector<Point>& points) {
vector<Point> res;
if (points.size() < 3) {
return res;
}
for (double t = 0; t <= 1; t += 0.01) {
Point p = Bezier(points, t);
res.push_back(p);
}
return res;
}
// 拟合三次贝塞尔曲线
vector<Point> FitCubicBezier(vector<Point>& points) {
vector<Point> res;
if (points.size() < 4) {
return res;
}
for (double t = 0; t <= 1; t += 0.01) {
Point p = Bezier(points, t);
res.push_back(p);
}
return res;
}
int main() {
// 示例:二阶贝塞尔曲线points = {{10,10}, {20, 80},{40,120}}
vector<Point> points = { {10,10}, {20, 80},{40,120} };
vector<Point> res = FitQuadraticBezier(points);
for (auto p : res) {
cout << "(" << p.x << ", " << p.y << ")" << endl;
}
return 0;
}
```
bezier曲线代码实现c++
好的,关于bezier曲线的代码实现,以下是一个简单的示例:
```c++
#include <iostream>
#include <vector>
using namespace std;
// 计算组合数C(n, i)
int binomialCoeff(int n, int i) {
int res = 1;
for (int j = 1; j <= i; ++j) {
res *= n - j + 1;
res /= j;
}
return res;
}
// 计算Bezier曲线上的点
void bezierCurve(vector<pair<int, int>>& points, vector<pair<int, int>>& curvePoints) {
int n = points.size() - 1;
for (double t = 0; t <= 1; t += 0.01) {
int x = 0, y = 0;
for (int i = 0; i <= n; ++i) {
int coeff = binomialCoeff(n, i);
double factor = coeff * pow(t, i) * pow(1 - t, n - i);
x += factor * points[i].first;
y += factor * points[i].second;
}
curvePoints.push_back({x, y});
}
}
int main() {
vector<pair<int, int>> points = {{100, 100}, {200, 300}, {400, 50}, {500, 200}};
vector<pair<int, int>> curvePoints;
bezierCurve(points, curvePoints);
for (auto& p : curvePoints) {
cout << "(" << p.first << ", " << p.second << ")" << endl;
}
return 0;
}
```
这个示例中,我们定义了一个 `bezierCurve` 函数,它接受一个点集 `points` 和一个空的曲线点集 `curvePoints`,并计算出Bezier曲线上的所有点,存储在 `curvePoints` 中。其中,`binomialCoeff` 函数用于计算组合数,`pow` 函数用于计算幂次方。