Page 2 of 2 FirstFirst 12
Results 11 to 11 of 11

Thread: Draw a filled circle

  1. #11
    Quote Originally Posted by Mirage View Post
    If you can draw a circle bound, you know all its points. And then you can draw all the needed horizontal lines very fast.
    That's exactly what i did. But i now decided to go back to it a little and optimize it. No more overlapped pixels. Also screenshot to let you see the areas it makes with different colors.
    Code:
    ... adding previous code...
    
    procedure DrawVLine(x, y1, y2: longint; color: TColor);
    var y: integer;
    begin
      for y:=y1 to y2 do
        form1.Canvas.Pixels[x, y]:=color;
    end;
    
    // Optimized fill circle
    procedure retro_fill_circle(xc, yc, r: longint; color: TColor);
    var x, y, d, y1, y2: longint;
    begin
      x:=0; y:=r;
      d:=1 - r;
      y1:=r*7 div 10; // I found this 7/10 ratio just by testing and testing a little...
      // It is the Y coordinate where the drawing sections separate
      y2:=yc+y1;
      y1:=yc-y1;
      while x < y do begin
        if d < 0 then
          d:=d + 2*x + 3
        else begin
          d:=d + 2*x - 2*y + 5;
          dec(y);
        end;
        DrawVLine(xc - x, yc - y, y1, color); // Top
        DrawVLine(xc + x, yc - y, y1, color);
        DrawLine(xc - y, xc + y, yc - x, color); // Upper middle
        DrawLine(xc - y, xc + y, yc + x, color); // Lower middle
        DrawVLine(xc - x, y2, yc + y, color); // Bottom
        DrawVLine(xc + x, y2, yc + y, color);
        inc(x);
      end;
    end;
    edit: Actually the real ratio might close into sin(45 degree) ~ 0.707. There is 2 pixel-lines overlapped still when drawing with 250 radius. We aren't talking of any significant performance difference though. But if drawing blended pixels you want absolute precision.
    Attached Images Attached Images
    Last edited by User137; 13-11-2013 at 10:47 PM.

Page 2 of 2 FirstFirst 12

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
  •