在纸js中细分路径

subdividing paths in paper js

正在研究一种简单的方法来细分 paper js 中的现有路径(或复合路径)。有没有人有任何建议,或者知道我可能在哪里出错?我的想法是,我可以采用一条路径和要进行的细分数量( subdivide(path,level) ),然后循环遍历该路径,在中点创建新点,然后使用这些点,插入或剪切进入原来的路径。

function subdivide(path,level){
  var numPoints = path.index;
  var t_seg = [];

  for(var i = 0; i < level; i++){
     for(var j = 0; j < path.index*2; j+=2){
        var p_1 = j;
        var p_2 = j+1;

        var t_p = new Point((p_1.x+p_2.x)/2,(p_1.y+p_2.y)/2);
        t_seg.push(t_p);
     }
  }
  var t_path = new Path(t_seg);

  return t_path;
}

对此有何建议?有一个简化方法,但我真的只想细分我现有的路径...

您可以使用 Path.flatten():

// Create a circle shaped path at { x: 80, y: 50 }
// with a radius of 35:
var path = new Path.Circle({
    center: new Size(80, 50),
    radius: 35
});

// Select the path, so we can inspect its segments:
path.selected = true;

// Create a copy of the path and move it 150 points to the right:
var copy = path.clone();
copy.position.x += 150;

// Convert its curves to points, with a max distance of 20:
copy.flatten(20);

(如果我没理解错的话)