Board index » delphi » Drawing Circles

Drawing Circles

Hi,

Does anyone have source to draw a circle (fairly quickly).  
I would like something that calls a putpixel function taking
x : word, y : word and c : byte so that I can use this
function in any video mode and just change the code for
putpixel (for now atleast I will be using it in VESA
640x480x256 is DOS real mode).

Any help is greatly appreciated.

Ben Stephenson
aa...@info.london.on.ca

 

Re:Drawing Circles


Quote
Ben Stephenson wrote:

> Hi,

> Does anyone have source to draw a circle (fairly quickly).
> I would like something that calls a putpixel function taking
> x : word, y : word and c : byte so that I can use this
> function in any video mode and just change the code for
> putpixel (for now atleast I will be using it in VESA
> 640x480x256 is DOS real mode).

> Any help is greatly appreciated.

> Ben Stephenson
> aa...@info.london.on.ca

This should help you.

{$x+}

uses crt, graph;

var
    a, b, r, k, i, j : longint;
    x,y : real;
    c : integer;

procedure screen13;

begin
     asm;
         mov ax, 0013h
         int 10h
     end;
end;

procedure putpixel(x,y,col:word);

begin
     mem [$a000:(y*320)+x]:=col;
end;

begin
     clrscr;
     write ('Rayon : ');
     readln(r);
     write ('Position x : ');
     readln(a);
     write ('Position y : ');
     readln(b);
     write ('Couleur : ');
     readln(c);
     screen13;
     for j := 0 to 199 do
     begin
          putpixel (160,j,17);
     end;
     for j := 0 to 319 do
     begin
          putpixel (j,100,17);
     end;
     for j := (-r*100) to (r*100) do
     begin
          x := j;
          y := sqrt(sqr(r) - sqr(x/100));
          k := round(y);
          i := round(x/100);
          putpixel (i+160+a,k+100-b,c);
     end;
     for j := (r*100) downto (-r*100) do
     begin
          x := j;
          y := sqrt(sqr(r) - sqr(x/100));
          k := round(y);
          i := round(x/100);
          putpixel (i+160+a,-k+100-b,c);
     end;
     readkey;
     textmode (co80);
end.

                                -=Fungus=-
                It grows everywhere...

Re:Drawing Circles


Quote
Ben Stephenson wrote:

> Hi,

> Does anyone have source to draw a circle (fairly quickly).
> I would like something that calls a putpixel function taking
> x : word, y : word and c : byte so that I can use this
> function in any video mode and just change the code for
> putpixel (for now atleast I will be using it in VESA
> 640x480x256 is DOS real mode).

Someone else posted an algorithm that uses floating point math to draw a
circle.  Okay, but when you're looking for speed, you shoot yourself in
the foot (especially when using "real", which isn't even a true floating
point type).

Here's an integer-based circle procedure that uses a modified Bresenham
circle algorithm.  Faster than floating point, and a bit more accurate
than the "classic" Bresenham algorithm:

procedure circle (xcent,ycent,rad:integer;colr:byte);

var
  x, y, d : integer;
  dE, dSE : integer;

begin
  x := 0;
  y := rad;

  d := 1 - rad;
  dE := 3;
  dSE := -(rad shl 1) + 5;
  plot(xcent-x, ycent-y, colr);
  plot(xcent-x, ycent+y, colr);
  plot(xcent+y, ycent-x, colr);
  plot(xcent-y, ycent+x, colr);

  while y>x do
    begin
      if d<0 then
        begin
          inc (d,dE);
          inc (dE,2);
          inc (dSE,2);
          inc (x);
        end
      else
        begin
          inc (d,dSE);
          inc (dE,2);
          inc (dSE,4);
          inc (x);
          dec (y);
        end;
    plot(xcent+x, ycent-y, colr);
    plot(xcent-x, ycent+y, colr);
    plot(xcent-x, ycent-y, colr);
    plot(xcent+x, ycent+y, colr);
    plot(xcent+y, ycent-x, colr);
    plot(xcent-y, ycent+x, colr);
    plot(xcent-y, ycent-x, colr);
    plot(xcent+y, ycent+x, colr);
  end;
end;

Provide your own plot() procedure, and you're in business.

Quote
> Any help is greatly appreciated.

> Ben Stephenson
> aa...@info.london.on.ca

--
Scott Earnest        | We now return you to our regularly |
set...@ix.netcom.com | scheduled chaos and mayhem. . . .  |

Other Threads