0.基本概念
https://blog.csdn.net/sinat_35676815/article/details/120884682
1.参考代码
#include <iostream>
#include <vector>
#include <cmath>
// 定义点坐标结构体
struct Point {
double x;
double y;
};
// 计算组合数C(n, i)
int binomial(int n, int i) {
int res = 1;
for (int j = 1; j <= i; j++) {
res *= (n - j + 1);
res /= j;
}
return res;
}
// 计算贝塞尔曲线上的点坐标
Point bezier(std::vector<Point>& controlPoints, double t) {
Point res = {0, 0};
int n = controlPoints.size() - 1;
for (int i = 0; i <= n; i++) {
int b = binomial(n, i);
double u = pow(1 - t, n - i) * pow(t, i);
res.x += b * u * controlPoints[i].x;
res.y += b * u * controlPoints[i].y;
}
return res;
}
int main() {
std::vector<Point> controlPoints = {{0, 0}, {2, 3}, {4, 2}, {6, 3}, {8, 0}};
const int resolution = 100;
for (int i = 0; i <= resolution; i++) {
double t = i / static_cast<double>(resolution);
Point p = bezier(controlPoints, t);
std::cout << p.x << ", " << p.y << std::endl;
}
return 0;
}