13. Arrow

This draws an arrowed line. One can specify if the start and end points have an arrow.

<<funcdefs>>=
void btprnt_draw_arrow(btprnt_region *r,
                       int start_x, int start_y,
                       int end_x, int end_y,
                       int arrow_start,
                       int arrow_end,
                       int draw_line,
                       int c);

Arrowheads are equalateral triangles, angled relative to the slope of the line.

<<funcs>>=
void btprnt_draw_arrow(btprnt_region *r,
                       int start_x, int start_y,
                       int end_x, int end_y,
                       int arrow_start,
                       int arrow_end,
                       int draw_line,
                       int c)
{
    int sz;
    float angle;
    float off;
    int ptx[2];
    int pty[2];
    int dx;
    int dy;
    int dir;

    if (start_x > end_x) {
        dir = -1;
    } else {
        dir = 1;
    }

    sz = 9;

    dx = end_x - start_x;
    dy = end_y - start_y;

    off = asin(dx / sqrt(dx*dx + dy*dy));

    if (off < 0) {
        dir *= -1;
    }

    angle = 30;
    /* convert to radians */
    angle *= M_PI / 180.0;

    if (draw_line) {
        btprnt_draw_line(r,
                        start_x, start_y,
                        end_x, end_y, c);
    }

    if (arrow_start) {
        ptx[0] = start_x + dir * sz * sin(angle + off);
        pty[0] = start_y + dir * sz * cos(angle + off);

        ptx[1] = start_x + dir * sz * sin(off - angle);
        pty[1] = start_y + dir * sz * cos(off - angle);

        btprnt_draw_triangle(r,
                            start_x, start_y,
                            ptx[0], pty[0],
                            ptx[1], pty[1],
                            c);
    }

    if (arrow_end) {
        ptx[0] = end_x + -dir * sz * sin(angle + off);
        pty[0] = end_y + -dir * sz * cos(angle + off);

        ptx[1] = end_x + -dir * sz * sin(off - angle);
        pty[1] = end_y + -dir * sz * cos(off - angle);

        btprnt_draw_triangle(r,
                            end_x, end_y,
                            ptx[0], pty[0],
                            ptx[1], pty[1],
                            c);

    }
}



prev | home | next