Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

edppaint.cpp - horz. lines not drawn #347

Open
JustDaveIII opened this issue Jun 27, 2024 · 0 comments
Open

edppaint.cpp - horz. lines not drawn #347

JustDaveIII opened this issue Jun 27, 2024 · 0 comments

Comments

@JustDaveIII
Copy link

void Paint::DrawLine(int x0, int y0, int x1, int y1, int colored)

Horizontal lines (and I suspect vertical ones too) are not drawn. The Bresenham algorithm is not fully / correctly implemented for the Arduino files that use edppaint.cpp

Never mind that I can test before calling to see if the line is Horz. or Vert. and then call the appropriate routine as this one should handle all edge cases. Below is my code correction. It also alters the duplicate "2 * err" code to execute only once.

`void Paint::DrawLine(int x0, int y0, int x1, int y1, int colored) {

/* Corrected  Bresenham algorithm */
int dx = x1 - x0 >= 0 ? x1 - x0 : x0 - x1;
int sx = x0 < x1 ? 1 : -1;
int dy = y1 - y0 <= 0 ? y1 - y0 : y0 - y1;
int sy = y0 < y1 ? 1 : -1;
int err = dx + dy;

int e2;

while(true)

{
if ((x0 == x1) && (y0 == y1)) break;

    DrawPixel(x0, y0 , colored);
    e2 = 2 * err;
    
    if (e2 >= dy) {
       if (x0 == x1) break;     
        err += dy;
        x0 += sx;
    }
    if (e2 <= dx) {
       if (y0 == y1) break;
        err += dx; 
        y0 += sy;
    }
}

}
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant