Results 1 to 5 of 5

Thread: SDL, sloped pixel lines

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    OK! Now I see what are you talking about. Couldn't really figure that out because me and C-like syntax don't get along pretty well

    Any way looking at your code I'm wondering a few things.
    1. When calculating dx and dy values why don't you simply use:
      Code:
      dx := ABS(x2-x1);
      dy := ABS(y2-y1);
    2. Also looking at you code it seems as if you are using a for loop to determine the width of line rectangles. Isn't that very slow?
      Wouldn't it be easier to calculate the width of horizontal lines (line segments) that will represent your diagonal line by simply dividing the dx with dy?

  2. #2
    Member
    Join Date
    Apr 2014
    Location
    Lower Saxony, Germany
    Posts
    38
    Quote Originally Posted by SilverWarior View Post
    Also looking at you code it seems as if you are using a for loop to determine the width of line rectangles. Isn't that very slow?
    Wouldn't it be easier to calculate the width of horizontal lines (line segments) that will represent your diagonal line by simply dividing the dx with dy?
    You're right ...

    I adapted a line algorithm intended for drawing single pixels. That code has to iterate over every pixel of the line. But that's unnecessary when drawing horizontal or vertical line segments instead of pixels.
    So I followed your hint and rewrote the code:

    Code:
    procedure draw_line(renderer: psdl_renderer; x1, y1, x2, y2: longint);
    var
      dx, dy, rx, ry: longint;
      error, c: longint;
      step, rest: longint;
    begin
    
      { if the whole line is horizontal or vertical than use SDL_RenderDrawLine and exit
        if not, we know that neither dx nor dy can be zero (division by zero not possible) }
      if (y1=y2) or (x1=x2) then begin
        sdl_renderdrawline(renderer, x1, y1, x2, y2);
        exit;
      end;
    
      dy := y2-y1;
      if dy>=0 then ry := 1
      else begin
        dy := -dy;
        ry := -1
      end;
      inc(dy);
    
      dx := x2-x1;
      if dx>=0 then rx := 1
      else begin
        dx := -dx;
        rx := -1
      end;
      inc(dx);
    
      { slope is moderate (dy/dx <= 1) }
      if dx>=dy then begin
    
        step := dx div dy;
        rest := dx-(dy*step);
        step := step*rx;
    
        x2 := x1;
    
        error := dy shr 1;
        for c := 1 to dy do begin
    
          dec(error, rest);
          if error<0 then begin
            inc(error, dy);
            inc(x2, rx);
          end;
    
          inc(x2, step);
          sdl_renderdrawline(renderer, x1, y1, x2-rx, y1);
          x1 := x2;
    
          inc(y1, ry);
        end;
    
      end
      { slope is steep (dy/dx > 1) }
      else begin
    
        step := dy div dx;
        rest := dy-(dx*step);
        step := step*ry;
    
        y2 := y1;
    
        error := dx shr 1;
        for c := 1 to dx do begin
    
          dec(error, rest);
          if error<0 then begin
            inc(error, dx);
            inc(y2, ry);
          end;
    
          inc(y2, step);
          sdl_renderdrawline(renderer, x1, y1, x1, y2-ry);
          y1 := y2;
    
          inc(x1, rx);
        end;
    
      end;
    end;
    Regarding your other question: I have to know the signs of the deltas (dx, dy) and store them in the rates (rx, ry) for later use. After that I make the deltas positive.

    Thanks for following my trial-and-error technique.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •