9

A somewhat similar question to this one, but in a different language for graphics:

Is there a simple way to draw a squiggly line in asymptote?

11

It seems that there has not been such a module for squiggly lines, although it is possible (and not very hard) to define one.

A simple implementation:

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));

enter image description here

  • Not quite what I was looking for exactly, but it sets me on the right track. – Willie Wong Mar 5 '11 at 11:55
5

Leo Liu's answer above led me to the following implementation (I wanted more smooth squiggly lines):

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;
}

The funny definition of step from stepsize is so that the endpoints of the curve will not be distorted, and that when applying to a closed (smooth) curve the object will actually close smoothly.

For piecewise smooth curves it is perhaps better to apply squiggly to each of the smooth components separately.

  • I think you'd better use guide rather than path to build squig here. – Leo Liu Mar 5 '11 at 12:04
  • @Leo: I'm afraid I don't quite understand. If I specify deviation from the path (the same way you did), then yes, I can probably just .. instead of -- and I see that using guide would force a smooth join. But since I actually specify the tangent angle at every node in the path, does it actually matter that I use guide versus path? – Willie Wong Mar 5 '11 at 12:24
  • 2
    Yes. In Asymptote, a path is a evaluated cubic spline, but a guide is not evaluated. You set every node of the spline, and the tangent direction, but you didn't specify the control points between nodes. Note that four points (two nodes and two control points) determine a Bezier segment. There is an example in the manual (6.2 Paths and guides) to show possilbe problem using path. As a suggestion, you should always use guide to build a curve in a loop. – Leo Liu Mar 5 '11 at 13:19
  • @Leo: I see. Thanks! I've edited the above to include your suggestion. – Willie Wong Mar 5 '11 at 14:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.