HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

Bresenham Line Drawing Algorithm

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
bresenhamdrawinglinealgorithm

Problem

int Octet(double m, int Ax, int Ay, int Bx, int By)
{
    int octetNo = -1;
    double dx = Bx - Ax;
    double dy = By - Ay;

    //m = dy / dx;

    if(0=m && m>=-1)
    {
        if(Bx1)
    {
        if(Aym)
    {
        if(Ayabs(dy))?abs(dx):abs(dy))/2;
    int x = x1;
    int y = y1;

    for(int i=0 ; i=0)
        {
            di = NextDiIfDiIsGreaterThanOrEqualtoZero(m, di, x1, y1, x2, y2);
            y++;            
        }
        else
        {
            di = NextDiIfDiIsSmallerThanZero(m, di, x1, y1, x2, y2);            
        }       
        x++;
    }
}

Solution

Disclaimer: I know nothing of C++, but I'll help you as much as I can.

Overall, your code looks good. A+ for readability! Probably a missing spot somewhere, but no biggy. Still, your code is easy to read and follow.

On your BresLine(), you have this:

int dy = y2 - y1;
int dx = x2 - x1;
double m = (double)dy/(double)dx;
[...]
int limit = ((abs(dx)>abs(dy))?abs(dx):abs(dy))/2;


You could try to store that abs() somewhere, to shave on re-calling it.

You have the functions NextDiIfDiIsGreaterThanOrEqualtoZero() and NextDiIfDiIsSmallerThanZero(). They have giant names. And the code is almost the same. I suggest the following:

int NextDi(double m, int di, int x1, int y1, int x2, int y2)
{
    int di_plus_1 = -1;
    int dx = x2 - x1;
    int dy = y2 - y1;

    int octet = Octet(m, x1, y1, x2, y2);

    switch(octet)
    {
        case 1:
        case 5:
            di_plus_1 = di - 2 * (di > 0 ? dx - dy : dy); 
            break;
        case 2:
        case 6:
            di_plus_1 = di + 2 * (di > 0 ? dx - dy : dx); 
            break;
        case 3:
        case 7:
            di_plus_1 = di - 2 * (di > 0 ? dx - dy : dx); 
            break;
        case 4:
        case 8:
            di_plus_1 = di + 2 * (di > 0 ? dx - dy : dy); 
            break;
    }

    return di_plus_1;
}


It isn't the prettiest thing on Earth, but may work for you. To call it, you don't have to change anything! Just remove that if on BresLine. With some bitwise operations, you can cut down this code by a lot!

If you see something innacurate, please tell me. I haven't touched on the remaining functions, but I'm specially worried about the Octet function.

Code Snippets

int dy = y2 - y1;
int dx = x2 - x1;
double m = (double)dy/(double)dx;
[...]
int limit = ((abs(dx)>abs(dy))?abs(dx):abs(dy))/2;
int NextDi(double m, int di, int x1, int y1, int x2, int y2)
{
    int di_plus_1 = -1;
    int dx = x2 - x1;
    int dy = y2 - y1;

    int octet = Octet(m, x1, y1, x2, y2);

    switch(octet)
    {
        case 1:
        case 5:
            di_plus_1 = di - 2 * (di > 0 ? dx - dy : dy); 
            break;
        case 2:
        case 6:
            di_plus_1 = di + 2 * (di > 0 ? dx - dy : dx); 
            break;
        case 3:
        case 7:
            di_plus_1 = di - 2 * (di > 0 ? dx - dy : dx); 
            break;
        case 4:
        case 8:
            di_plus_1 = di + 2 * (di > 0 ? dx - dy : dy); 
            break;
    }



    return di_plus_1;
}

Context

StackExchange Code Review Q#101890, answer score: 5

Revisions (0)

No revisions yet.