Board index » delphi » Drawing a circle

Drawing a circle

Does anyone out there know the formula for a circle.

I know you can work out the corresponding x and y value for any point given
the centre and radius. I used to know two methods about 15 years ago but
have since forgotten them.

Any help gratefully received.

Sean Radford

 

Re:Drawing a circle


Quote
Sean Radford wrote in message <01bcd8ee$2303d100$9763989e@bladesys>...
>Does anyone out there know the formula for a circle.

>I know you can work out the corresponding x and y value for any point given
>the centre and radius. I used to know two methods about 15 years ago but
>have since forgotten them.

>Any help gratefully received.

Uhm..
X^2 + Y^2 = R^2

Re:Drawing a circle


In article <01bcd8ee$2303d100$9763989e@bladesys>,

Quote
Sean Radford <s...@bladesys.demon.co.uk> wrote:
>Does anyone out there know the formula for a circle.

        I'm not sure why this was crossposted to these groups, but
the formula for a circle is x^2 + y^2 = r^2.  If what you really
want is to draw a circle on a pixel-map, there are more efficient
ways to determine the points.  Any basic graphics text should
have them.  Here's a procedure, lifted wholesale from the Oberon
System, implementing the Bresenham algorithm.

PROCEDURE circle(f: GraphicFrames.Frame; col: INTEGER; x0, y0, r: LONGINT);
        VAR
                x, y, u: LONGINT;
        BEGIN
                u := 1 - r;
                x := r;
                y := 0;
                WHILE y <= x DO
                        Display.DotC(f, col, SHORT(x0+x), SHORT(y0+y), 0);
                        Display.DotC(f, col, SHORT(x0+y), SHORT(y0+x), 0);
                        Display.DotC(f, col, SHORT(x0-y), SHORT(y0+x), 0);
                        Display.DotC(f, col, SHORT(x0-x), SHORT(y0+y), 0);
                        Display.DotC(f, col, SHORT(x0-x), SHORT(y0-y), 0);
                        Display.DotC(f, col, SHORT(x0-y), SHORT(y0-x), 0);
                        Display.DotC(f, col, SHORT(x0+y), SHORT(y0-x), 0);
                        Display.DotC(f, col, SHORT(x0+x), SHORT(y0-y), 0);
                        IF u < 0 THEN
                                INC(u, 2*y+3)
                        ELSE
                                INC(u, 2*(y-x)+5);
                                DEC(x)
                        END ;
                        INC(y)
                END
        END circle;

--
Lazlo's Chinese Relativity Axiom: No matter how great your
triumphs or how tragic your defeats, over a billion Chinese
couldn't care less.

Re:Drawing a circle


Quote
Sean Radford wrote:
> Does anyone out there know the formula for a circle.

> I know you can work out the corresponding x and y value for any point
> given
> the centre and radius. I used to know two methods about 15 years ago
> but
> have since forgotten them.

> Any help gratefully received.

> Sean Radford

   How about this

for d := 0 to 360 do
  x := cos(d) * radius;
  y :=  sin(d) * radius;
  plot(x,y);
end;

Of course if you want a solid circle then save the previous x and y's
and join
them with a line.

Re:Drawing a circle


In article <01bcd8ee$2303d100$9763989e@bladesys>,

Quote
Sean Radford <s...@bladesys.demon.co.uk> wrote:
>Does anyone out there know the formula for a circle.

>I know you can work out the corresponding x and y value for any point given
>the centre and radius. I used to know two methods about 15 years ago but
>have since forgotten them.

If the circle is centred on (a,b) and has radius r, then its equation is
(x-a)^2 - (y-b)^2 = r^2

                        Paul Bolchover

Re:Drawing a circle


Sean,
  The equation for a circle is:

Sqr(X1 - X0) + Sqr(Y1 - Y0) := Sqr(R)

Where X0 and Y0 are the coords for the center and R is the radius.

To solve for X1 given a value for Y1 you would use:

X1 := X0 + Sqrt(Sqr(R) - Sqr(Y1-Y0));
X1 := X0 - Sqrt(Sqr(R) - Sqr(Y1-Y0));

The reason for 2 equations is that for each Y coord there will be two
values of X.

Also you might want to solve Sqr(R) - Sqr(Y1-Y0) first as a negative value
here would indicate that the Y1 value is outside the circle and will raise
an error.

Hope this helps!
--

Rodney E Geraghty
GERA-Tech
Ottawa, Canada
gera...@ibm.net

Sean Radford <s...@bladesys.demon.co.uk> wrote in article
<01bcd8ee$2303d100$9763989e@bladesys>...

Quote
> Does anyone out there know the formula for a circle.

> I know you can work out the corresponding x and y value for any point
given
> the centre and radius. I used to know two methods about 15 years ago but
> have since forgotten them.

> Any help gratefully received.

> Sean Radford

Re:Drawing a circle


On Tue, 14 Oct 1997 22:13:38 GMT, "Sean Radford" <s...@bladesys.demon.co.uk>
wrote:

Quote
>Does anyone out there know the formula for a circle.

>I know you can work out the corresponding x and y value for any point given
>the centre and radius. I used to know two methods about 15 years ago but
>have since forgotten them.

Is Bresenham's algorithm what you want? It's a bit complicated to post, but you
could probably look it up.

Pete Barrett

Re:Drawing a circle


Quote
In article <344408A8.3FA7A...@ozonline.com.au> r...@careless.net.au writes:
:Sean Radford wrote:

:: Does anyone out there know the formula for a circle.
:for d := 0 to 360 do
:  x := cos(d) * radius;
:  y :=  sin(d) * radius;
:  plot(x,y);
:end;
:Of course if you want a solid circle then save the previous x and y's
:and join them with a line.

don't most 1anguages which offer a plot function also offer a circle function?

Eric
--
   _    "just a Texan a long way from home"
 _| ~-. Eric Barber, Nortel, London Road, Harlow, England CM17 9NA
 \, *_} AT&T/BT/etc. +44 1279 403527/429531 x 3527, ESN (Nortel) 6 742 3527
   \(DISCLAIMER: Working at, not speaking for, Nortel | Eric.Bar...@nortel.co.uk

Re:Drawing a circle


Quote
101711.1...@compuserve.com (Pete Barrett) writes:
>On Tue, 14 Oct 1997 22:13:38 GMT, "Sean Radford" <s...@bladesys.demon.co.uk>
>wrote:
>>Does anyone out there know the formula for a circle.

>>I know you can work out the corresponding x and y value for any point given
>>the centre and radius. I used to know two methods about 15 years ago but
>>have since forgotten them.

>Is Bresenham's algorithm what you want? It's a bit complicated to post, but you
>could probably look it up.

There are faster circle algorithms than Bresenhams, which are a little
bit easier to write.

But the question is, why?  You can SelectObject(hDC, hBitmap);
and then call Arc() or another curve function for to alter the bitmap.

--

-Thanh Lim
Fanatic Games Programmer

Re:Drawing a circle


Quote
Invent Engineering wrote:

> Sean Radford wrote:

> > Does anyone out there know the formula for a circle.[...]
>    How about this

> for d := 0 to 360 do
>   x := cos(d) * radius;
>   y :=  sin(d) * radius;
>   plot(x,y);
> end;

        Or maybe

   x :=  a + cos(d) * radius;
   y :=  b + sin(d) * radius;

if for some reason you don't want the center at the origin...

--
David Ullrich

sig.txt not found

Re:Drawing a circle


In article <01bcd9d0$1166b560$1962420c@efgdell>,
Earl F. Glynn <EarlGl...@worldnet.att.net> wrote:

Quote

>Is this thread about how to draw a circle the hard way?

        No, it was just cross-posted ridiculously.  I'm taking it out
of Comp.lang.oberon.
--
Lazlo's Chinese Relativity Axiom: No matter how great your
triumphs or how tragic your defeats, over a billion Chinese
couldn't care less.

Re:Drawing a circle


Totoro <tot...@netcom.com> wrote in article
<totoroEI3pM3....@netcom.com>...

Quote
> 101711.1...@compuserve.com (Pete Barrett) writes:

> >On Tue, 14 Oct 1997 22:13:38 GMT, "Sean Radford"

<s...@bladesys.demon.co.uk>

Quote
> >wrote:

> >>Does anyone out there know the formula for a circle.

> >>I know you can work out the corresponding x and y value for any point
given
> >>the centre and radius. I used to know two methods about 15 years ago
but
> >>have since forgotten them.

> >Is Bresenham's algorithm what you want? It's a bit complicated to post,
but you
> >could probably look it up.
> There are faster circle algorithms than Bresenhams, which are a little
> bit easier to write.

> But the question is, why?  You can SelectObject(hDC, hBitmap);
> and then call Arc() or another curve function for to alter the bitmap.

Is this thread about how to draw a circle the hard way?

Why not just use Canvas.Ellipse and pass it a "square":

For example,  Canvas.Ellipse(0,0, 100,100);

efg
_____________________________________
Earl F. Glynn          EarlGl...@WorldNet.att.net
MedTech Research Corporation
Lenexa, KS  66219  USA                    

Re:Drawing a circle


Quote
Earl F. Glynn wrote:
> [...]
> Is this thread about how to draw a circle the hard way?

> Why not just use Canvas.Ellipse and pass it a "square":

> For example,  Canvas.Ellipse(0,0, 100,100);

        You obviously haven't been paying attention: you're
not supposed to suggest easier ways to do something, because
that's making assumptions about why the programmer is doing
what he's doing, and you're not supposed to make assumptions
like that.

        (Sorry, spillover from another thread. Seriously,
seems like it must be that it's a non-Delphi, presumably
non-MS-Windows and also non-who-knows-what-else question.)

--
David Ullrich

sig.txt not found

Re:Drawing a circle


Quote
Uncle Mike wrote:

> In article <01bcd8ee$2303d100$9763989e@bladesys>,
> Sean Radford <s...@bladesys.demon.co.uk> wrote:
> >Does anyone out there know the formula for a circle.

>         I'm not sure why this was crossposted to these groups, but
> the formula for a circle is x^2 + y^2 = r^2.  If what you really
> want is to draw a circle on a pixel-map, there are more efficient
> ways to determine the points.  Any basic graphics text should
> have them.  Here's a procedure, lifted wholesale from the Oberon
> System, implementing the Bresenham algorithm.

> PROCEDURE circle(f: GraphicFrames.Frame; col: INTEGER; x0, y0, r: LONGINT);

[snip]

PROCEDURE DrawCircle* (x, y, r: INTEGER);
VAR u, v, r2: INTEGER;
BEGIN
  r2 := r*r;    (* r < Sqrt(MAX(INTEGER)) *)
  FOR u := -r TO r DO
    v := SHORT(ENTIER(0.5 + Math.sqrt(r2-u*u)));
    Dot(x+u,y+v);
    Dot(x+u,y-v);
  END;
END DrawCircle;

where Dot is any pixel drawer (with color previously set).

--
Douglas G. Danforth, Ph.D.      danfo...@csli.stanford.edu
Senior Research Engineer        (650) 723-2487
Center for the Study of Language and Information
Stanford University

Other Threads