package ToyDrawLine;

sub get_toy_api_PointsOfLineCMacros {
    my($class,%args) = @_;
    my $code = <<'ENDOFCCODE';
#define POINTS_OF_LINE(x1, y1, x2, y2)\
{\
    /*\
       Just another 2D Bresenham line walker.\
\
       arguments: int x1, y1, x2, y2\
        requires:  Point(int x, y)     (a proceedure or macro)\
\
       Calls Point(x,y) for each point on the line.\
\
       From DigitalLine.c, Paul Heckbert,\
       "Graphics Gems", Academic Press, 1990\
    */\
    int d, x, y, ax, ay, sx, sy, dx, dy;\
\
    dx = x2-x1;  ax = (dx>=0?dx:-dx)<<1;  sx = (dx>=0?1:-1);\
    dy = y2-y1;  ay = (dy>=0?dy:-dy)<<1;  sy = (dy>=0?1:-1);\
\
    x = x1;\
    y = y1;\
    if (ax>ay) {                /* x dominant */\
        d = ay-(ax>>1);\
        for (;;) {\
            { Point(x, y); }\
            if (x==x2) return;\
            if (d>=0) {\
                y += sy;\
                d -= ax;\
            }\
            x += sx;\
            d += ay;\
        }\
    }\
    else {                      /* y dominant */\
        d = ax-(ay>>1);\
        for (;;) {\
            { Point(x, y); }\
            if (y==y2) return;\
            if (d>=0) {\
                x += sx;\
                d -= ay;\
            }\
            y += sy;\
            d += ax;\
        }\
    }\
}

ENDOFCCODE
    return $code;
}

1;