const chaikinSimplify = (positions, refineCount, weight1, weight2) => {
let newPositions = [];
for (let i = 0; i < refineCount; i++) {
// 添加起点
newPositions.push(positions[0]);
// 内循环进行一次细化操作
for (let j = 1, n = positions.length; j < n - 1; j++) {
const nexti = j + 1;
const lasti = j - 1;
if (j === 1) { // 细化后的第一个顶点
const x = weight1*positions[lasti][0] + weight2*positions[j][0];
const y = weight1*positions[lasti][1] + weight2*positions[j][1];
newPositions.push([x, y]);
}
let x = weight2*positions[lasti][0] + weight1*positions[j][0];
let y = weight2*positions[lasti][1] + weight1*positions[j][1];
newPositions.push([x, y]);
x = weight2*positions[nexti][0] + weight1*positions[j][0];
y = weight2*positions[nexti][1] + weight1*positions[j][1];
newPositions.push([x, y]);
if (j === n - 2) { // 细化后的最后一个顶点
const x = weight1*positions[nexti][0] + weight2*positions[j][0];
const y = weight1*positions[nexti][1] + weight2*positions[j][1];
newPositions.push([x, y]);
}
}
// 添加终点
newPositions.push(positions[positions.length - 1]);
positions = newPositions;
newPositions = [];
}
return positions;
}
*方法由AI协助,并不是本人完全原创