Thick line c#

Am trying to make a thick line for my drawing app. issue is i found some book online http://members.chello.at/~easyfilter/Bresenham.pdf (page 81 thick line algorithm) its writen in C i think anyway i converted it to C# but have issue my lines dont have constant width duno why…

My code:

void plotLineWidth(int x0, int y0, int x1, int y1, float wd)
    { // plot an anti-aliased line of width wd
        int dx = Mathf.Abs(x1 - x0), sx = x0 < x1 ? 1 : -1;
        int dy = Mathf.Abs(y1 - y0), sy = y0 < y1 ? 1 : -1;
        int err = dx - dy, e2, x2, y2; // error value e_xy
        float ed = dx + dy == 0 ? 1 : Mathf.Sqrt((float)dx * dx + (float)dy * dy);

        for (wd = (wd + 1) / 2; ;) { // pixel loop
            SetPixelMyImg(x0, y0);
            e2 = err; x2 = x0;

            if (2 * e2 >= -dx) { // x step
                for (e2 += dy, y2 = y0; e2 < ed * wd && (y1 != y2 || dx > dy); e2 += dx) {
                    y2 += sy;
                    SetPixelMyImg(x0, y2);
                }

                if (x0 == x1) break;
                e2 = err; err -= dy; x0 += sx;
            }
            if (2 * e2 <= dy) { // y step
                for (e2 = dx - e2; e2 < ed * wd && (x1 != x2 || dx < dy); e2 += dy) {
                    x2 += sx;
                    SetPixelMyImg(x2, y0);
                }

                if (y0 == y1) break;
                err += dx; y0 += sy;
            }
        }
    }

So you can see what heppends. I draw lines (more like drawing colors cuz i am deliting black white top layer parts so bottom layer color shows) from mid to all directions…

See how its all bent. i think 90 degree lines are thin while all other are thick and deformed near ends. Any help would be nice :slight_smile:

Are you drawing lines using line renderer?

No i am editing texture pixels , and am using Brensenham algorithm , at least am trying

As far as I remember Bresenham’s algorithm changes how it iterates depending on whether the deltaY>deltaX. Did you account for that?

I kinda am not pro with math so i did not really got ins and outs of algorithm, i understand what you mean by deltaY>deltaX but dont know how to acount for it cuz like i say i dont really understand the algorithm itself totaly, if you have spear time it would be nice to help :slight_smile:

I’ve studied the algorithm for a exam but I’ve never done it in code. So I know that you sometimes change the algorithm if deltaY > deltaX but I don’t remember how you actually change it. You’ll just have to experiment. Maybe this pseudocode will help you understand what the standard algorithm does, so you can change it as you need to:

2941769--217728--upload_2017-1-31_16-11-47.png

2941769--217729--upload_2017-1-31_16-12-1.png

Try something like:

deltaY = y1-y0, deltaX = (x1-x0)
x = x0;
epsilon = deltaX - deltaY
for y FROM y0 to y1
add_point(x,y)
if (epsilon >0) THEN
x+=1;
epsilon-= deltaY
epsilon +=deltaX

ty but i ended up using a some other wey :slight_smile:

Alright cool. I’m glad you sorted it.

but i ended up using a some other wey

Which way was it? Thanks.

1 Like