interface IPoint {
x: number
y: number
}
/**
* 生成符合贝塞尔曲线的点
* @param [{x,y}] points 【{x,y}】
* @param count
* @returns 点的集合
*/
export const CreateBezierPoints = (points: IPoint[], count: number) => {
const _points: IPoint[] = [];
for (let i = 0; i < count; i++) {
let point = MultiPointBezier(points, i / count);
_points.push(point);
}
return _points;
}
/**
*
* @param points 点位
* @param t
* @returns
*/
const MultiPointBezier = (points: IPoint[], t: number) => {
const len = points.length;
let x = 0,
y = 0;
// 获取公式每一项的系数
// 2阶=> 1,2,1
// 3阶=> 1,3,3,1
const coefficient = (start, end) => {
let cs = 1,
bcs = 1;
while (end > 0) {
cs *= start;
bcs *= end;
start--;
end--;
}
return cs / bcs;
};
for (let i = 0; i < len; i++) {
let point = points[i];
const c = coefficient(len - 1, i)
const b = Math.pow(1 - t, len - 1 - i) * Math.pow(t, i) * c
x += point.x * b
y += point.y * b
}
// x,y 均是通过表达式求出
return { x: x, y: y };
}
//
二阶公式
三阶公式
本文参考: