# Board index » delphi » Formula for drawing circles

## Formula for drawing circles

Hi.

I'm looking for a formula to draw circles in the fastest time.
I don't want to use the circles in Delphi.

Yhanx

## Re:Formula for drawing circles

##### Quote
Francois Smit wrote:

> Hi.

> I'm looking for a formula to draw circles in the fastest time.

What circles ? What sizes ?

##### Quote
> I don't want to use the circles in Delphi.

Why ? They might be hardware accellerated ,and drawing circles
with accessing pixels will allways be slower.

Ok , i know of 2 possibilites :

1) draw 1/8 of a circle , calculating the points with
for x:=0 to numberofpixels

and add 7 more "setpixels" mirroring the remaining 7/8s of the
circle

2) Phytagoras : radius=sqrt(sqr(x)+sqr(y)) , solves to
begin
for x:=-max to max do
setpixel(basex+x,basey+x)
end;

3) for lots of fast circles , of same size , use one of the above to
fill a table , and use this table to draw the circles.

hope that helps ....

Andreas

## Re:Formula for drawing circles

##### Quote
"Francois Smit" <franc...@hoc.co.za> wrote in message

news:85v75b\$d1o11@bornews.borland.com...

##### Quote
> I'm looking for a formula to draw circles

You might take a look at the "Ellipse, Rotated" example in the
Mathematics/Geometry section of
http://www.efg2.com/Lab/Library/Delphi/Graphics/Algorithms.htm

--
efg

Earl F. Glynn     E-Mail:  EarlGl...@att.net
Overland Park, KS  USA

efg's Computer Lab:  http://www.efg2.com/Lab

## Re:Formula for drawing circles

Hi,

Francois Smit schrieb:

##### Quote
> I'm looking for a formula to draw circles in the fastest time.
> I don't want to use the circles in Delphi.

Why??

The Bresenham circle algorithm is the standard method to draw circles
efficiently.
(http://www.efg2.com/Lab).

Greetings, Robert
--
Robert Ro?mair
http://home.t-online.de/home/Robert.Rossmair/
Programming environment: Delphi 5 Professional, WinNT 4.0 SP6

## Re:Formula for drawing circles

Is there a trick you can try.

procedure Circle(X0, Y0, Radius : Integer);
var
X, Y : Double;
I : Integer;
begin
X:=R;
Y:=0;
for I:=1 to Round(2*Pi*R) do
begin
"PlotPixel"(Round(X0+X), Round(Y0+Y));  <- Use your prefered function
X:=X+Y/R;
Y:=Y-X/R;
end;
end;

I don't expect you use this method. It can be optimised calculating the
first (Pi/4) pixels and ploting the other simetric pixels.
The base behind the algorithm is to rotate the pixel (R, 0) around the
origin. The rotation matrix involves calculating the sin and cos functions,
but for small angles, cos=1 and sin=angle (radians). This approch generates
some errors and the circle not closes after one cicle. If you look at my
example, I used the wrong X coordinate to calculate the next Y, because I
used the new X and not the old one. This trick makes the error be reverted
closing the circle.
In old days when computers work in 8 bits without mul and div
instructions, every effort to not use sin and cos functions were welcome.

To do a more eficient algorith, use the Bresenham like Robert said or
the Michener simplification of the Bresenhan algorithm.

Arthur

##### Quote
Francois Smit wrote in message <85v75b\$d1...@bornews.borland.com>...
>Hi.

>I'm looking for a formula to draw circles in the fastest time.
>I don't want to use the circles in Delphi.

>Yhanx

## Re:Formula for drawing circles

Hi Fancois, here is a simple circle algorithm I came up with that does the 4
(I had to reproduce it from memory as I have the actual code elsewhere)  so
forgive me if it has any bugs - the original version worked well :)

Var
Distance    : Integer;
x               : Integer;
y               : Integer;
Begin
Distance := 0;    // x*x+y*y-r*r=0     at start
y := 0;
For x := radius downto 0 do
Begin
PlotPixel(xc-x,yc-y);
PlotPixel(xc-x,yc+y);
PlotPixel(xc+x,yc-y);
PlotPixel(xc+x,yc+y);
While(Distance<0)do
Begin
Inc(Distance,(y shl 1)+1);
Inc(y);
PlotPixel(xc-x,yc-y);
PlotPixel(xc-x,yc+y);
PlotPixel(xc+x,yc-y);
PlotPixel(xc+x,yc+y);
End;
Dec(Distance,(x shl 1)+1);
End;
End;
--
Paul Nicholls,
Live long and optimise.
--------------------------------
Web Page : www.southcom.com.au/~phantom
E-Mail        : phan...@southcom.com.au