Results 1 to 10 of 11

Thread: Draw a filled circle

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    PGDCE Developer Carver413's Avatar
    Join Date
    Jun 2010
    Location
    Spokane,WA,Usa
    Posts
    206
    well I suppose that works but what if you want a 6 sided circle or an ellipse or a half circle. might be better to use a more conventual routine with a fill routine.

  2. #2
    Sometimes, but fill routine makes it much slower. It is checking for pixels on every draw to find where the borders are, and could also be allocating dynamic arrays for point data, depending on algorithm. You can modify algorithms to make filled half circle etc. And if you need to draw the circle on top of a complex drawing, fill no longer works. And it's in addition that you would use sin/cos/sqrt for outline anyway

    Here's algorithm for above given function translated, filled and non-filled circle. (Drawing to TForm.canvas directly was bad idea, i know... but it showed it at least.)
    Code:
    procedure DrawPixel(x, y: longint; color: TColor);
    begin
      form1.Canvas.Pixels[x, y]:=color;
    end;
    
    procedure DrawLine(x1, x2, y: longint; color: TColor);
    var x: integer;
    begin
      for x:=x1 to x2 do
        form1.Canvas.Pixels[x, y]:=color;
    end;
    
    procedure retro_circle(xc, yc, r: longint; color: TColor);
    var x, y, d: longint;
    begin
      x:=0; y:=r;
      d:=1 - r;
      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;
        DrawPixel(xc + x, yc - y, color); // Top
        DrawPixel(xc - x, yc - y, color);
        DrawPixel(xc + y, yc - x, color); // Upper middle
        DrawPixel(xc - y, yc - x, color);
        DrawPixel(xc + y, yc + x, color); // Lower middle
        DrawPixel(xc - y, yc + x, color);
        DrawPixel(xc + x, yc + y, color); // Bottom
        DrawPixel(xc - x, yc + y, color);
        inc(x);
      end;
    end;
    
    procedure retro_fill_circle(xc, yc, r: longint; color: TColor);
    var x, y, d: longint;
    begin
      x:=0; y:=r;
      d:=1 - r;
      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;
        DrawLine(xc - x, xc + x, yc - y, color);
        DrawLine(xc - y, xc + y, yc - x, color);
        DrawLine(xc - y, xc + y, yc + x, color);
        DrawLine(xc - x, xc + x, yc + y, color);
        inc(x);
      end;
    end;
    Also as far as i see, the fill function may draw some pixels overlapped. Not perfectly optimal this way.
    Last edited by User137; 13-11-2013 at 10:49 PM. Reason: Didn't need those n variables

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
  •