Home » Community » Forums » » Line Drawing Algorithm Explained
  Intel sponsors gamedev.net search:   
[Control Panel] [Register] [Bookmarks] [Who's Online] [Active Topics] [Stats] [FAQ] [Search]

Add Forum to Favorites |  Send Topic To a Friend | View Forum FAQ | Track this topic


 Last Thread Next Thread 
 Line Drawing Algorithm Explained
Post Reply 
Heres another implementation of the algorithm that is less easy to follow, but significantly more compact:

    int dx = abs(x1 - x0),
        dy = abs(y1 - y0);

    bool steep = dy > dx;

    if(steep)
    {
        Swap(dx, dy);
        Swap(x0, z0);
        Swap(x1, z1);
    }

    int error = 0,
        de = dy,
        &x = steep ? y0 : x0,
        &y = steep ? x0 : y0,
        xstep = (x0 < x1) ? 1 : -1, 
        ystep = (y0 < y1) ? 1 : -1;

    Plot(x, y);

    while(x0 != x1)
    {
        x0 += xstep;
        error += de;
        if( (error << 1) >= dx)
        {
            y0 += ystep;
            error -= dx;
        }
        Plot(x, y);
    }


 User Rating: 1015   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

Thank you very much, I sought everywhere a simple explanation of this algorithm in french or in english and this one is the best I found. God bless.

 User Rating: 1015   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

All times are ET (US)

Post Reply
 Last Thread Next Thread 
Forum Rules:
You may not post new threads
You may post replies
You may not edit your posts
You may not use HTML in your posts
Jump To:
Administrative Options: