答案1
似乎还没有这样的波浪线模块,尽管定义一个是可能的(并且不太难)。
一个简单的实现:
path zigzag(path g, real step=2, real distance=2)
{
real len = arclength(g);
int state = 0;
path zig;
for (real u = 0; u < len; u += step) {
real t = arctime(g, u);
pair p = point(g, t);
pair norm = unit(rotate(90) * dir(g, t));
if (state == 1)
p = p + distance * norm;
else if (state == 3)
p = p - distance * norm;
zig = zig -- p;
state = (state + 1) % 4;
}
zig = zig -- point(g, length(g));
return zig;
}
// test
path g = (0,0) -- (2cm,0) -- (4cm,1cm) -- (0,3cm) -- cycle;
draw(zigzag(g));
答案2
Leo Liu 的上述回答让我想到了以下实现(我想要更平滑的波浪线):
guide squiggly(path g, real stepsize, real slope=45)
{
real len = arclength(g);
real step = len / round(len / stepsize);
guide squig;
for (real u = 0; u < len; u += step){
real a = arctime(g, u);
real b = arctime(g, u + step / 2);
pair p = point(g, a);
pair q = point(g, b);
pair np = unit( rotate(slope) * dir(g,a));
pair nq = unit( rotate(0 - slope) * dir(g,b));
squig = squig .. p{np} .. q{nq};
}
squig = squig .. point(g, length(g)){unit(rotate(slope)*dir(g,length(g)))};
return squig;
}
step
有趣的是, from的定义stepsize
使得曲线的端点不会扭曲,并且当应用到封闭(平滑)曲线时,对象实际上会平滑地闭合。
对于分段平滑曲线,最好squiggly
分别应用于每个平滑分量。