Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Thick line c#

Discussion in 'Scripting' started by LearningNot, Jan 30, 2017.

  1. LearningNot

    LearningNot

    Joined:
    Sep 30, 2015
    Posts:
    106
    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:
    Code (CSharp):
    1. void plotLineWidth(int x0, int y0, int x1, int y1, float wd)
    2.     { // plot an anti-aliased line of width wd
    3.         int dx = Mathf.Abs(x1 - x0), sx = x0 < x1 ? 1 : -1;
    4.         int dy = Mathf.Abs(y1 - y0), sy = y0 < y1 ? 1 : -1;
    5.         int err = dx - dy, e2, x2, y2; // error value e_xy
    6.         float ed = dx + dy == 0 ? 1 : Mathf.Sqrt((float)dx * dx + (float)dy * dy);
    7.  
    8.         for (wd = (wd + 1) / 2; ;) { // pixel loop
    9.             SetPixelMyImg(x0, y0);
    10.             e2 = err; x2 = x0;
    11.  
    12.             if (2 * e2 >= -dx) { // x step
    13.                 for (e2 += dy, y2 = y0; e2 < ed * wd && (y1 != y2 || dx > dy); e2 += dx) {
    14.                     y2 += sy;
    15.                     SetPixelMyImg(x0, y2);
    16.                 }
    17.  
    18.                 if (x0 == x1) break;
    19.                 e2 = err; err -= dy; x0 += sx;
    20.             }
    21.             if (2 * e2 <= dy) { // y step
    22.                 for (e2 = dx - e2; e2 < ed * wd && (x1 != x2 || dx < dy); e2 += dy) {
    23.                     x2 += sx;
    24.                     SetPixelMyImg(x2, y0);
    25.                 }
    26.  
    27.                 if (y0 == y1) break;
    28.                 err += dx; y0 += sy;
    29.             }
    30.         }
    31.     }
    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 :)
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,146
    Are you drawing lines using line renderer?
     
  3. LearningNot

    LearningNot

    Joined:
    Sep 30, 2015
    Posts:
    106
    No i am editing texture pixels , and am using Brensenham algorithm , at least am trying
     
  4. VengeanceDesign

    VengeanceDesign

    Joined:
    Sep 7, 2014
    Posts:
    84
    As far as I remember Bresenham's algorithm changes how it iterates depending on whether the deltaY>deltaX. Did you account for that?
     
  5. LearningNot

    LearningNot

    Joined:
    Sep 30, 2015
    Posts:
    106
    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 :)
     
  6. VengeanceDesign

    VengeanceDesign

    Joined:
    Sep 7, 2014
    Posts:
    84
    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:

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

    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​
     
  7. LearningNot

    LearningNot

    Joined:
    Sep 30, 2015
    Posts:
    106
    ty but i ended up using a some other wey :)
     
  8. VengeanceDesign

    VengeanceDesign

    Joined:
    Sep 7, 2014
    Posts:
    84
    Alright cool. I'm glad you sorted it.
     
  9. JPhilipp

    JPhilipp

    Joined:
    Oct 7, 2014
    Posts:
    56
    > but i ended up using a some other wey

    Which way was it? Thanks.
     
    StuwuStudio likes this.