Qbasicnews.com
Alternate way to draw a circle - Printable Version

+- Qbasicnews.com (http://qbasicnews.com/newforum)
+-- Forum: Qbasic "like" compilers/interpreters (http://qbasicnews.com/newforum/forum-5.html)
+--- Forum: FB Discussion & Programming Help (http://qbasicnews.com/newforum/forum-15.html)
+--- Thread: Alternate way to draw a circle (/thread-8551.html)

Pages: 1 2


Alternate way to draw a circle - axipher - 12-13-2005

Hi, I am making a paint program as yuo may already know, but I have a problem, I have a circle drawing sub that uses user input then draws the circle, my only problem is that I have 2 views of the painting area, my problem is the circle can overlap the 2x view when it draws in the 1x view and vice versa, is there a way to draw a circle without using the CIRCLE command so I can set limits for the drawing process, I just need a basic code and I'll figure out a way to input my own variables properly. Any help is really appreciated.


Alternate way to draw a circle - yetifoot - 12-13-2005

heres the bresnham one, havent tested on FB, its an old QB one

Code:
'_|_|_|   BRESNHAM.BAS
'_|_|_|   This program demonstrates the Bresenham Algorithms
'_|_|_|   for the drawing of lines and circles, using PSET.
'_|_|_|   Adapted from BRESNHAM.C in Bob Stout's SNIPPETS.
'_|_|_|   No warrantee or guarantee is implied or given.
'_|_|_|   Released to   PUBLIC DOMAIN   by Kurt Kuzba. (4/16/96)

DECLARE SUB BLine (x%, y%, x2%, y2%, c%)
DECLARE SUB BCircle (x%, y%, r%, c%)

SCREEN 13

CONST HIGH = 200   'The Bresenham Cirlce needs to know the screen dimensions
CONST WIDE = 320

BCircle 159, 99, 65, 77
BLine 0, 0, WIDE, HIGH, 14
pause$ = INPUT$(1)

SCREEN 0, 0, 0, 0: WIDTH 80: COLOR 7, 0: CLS : END

SUB BCircle (xc%, yc%, r%, c%)
'_|_|_|   Bresenham Circle Drawing Algorithm
'_|_|_|   Adapted from BRESNHAM.C in Bob Stout's SNIPPETS.
   x% = 0: d% = 2 * (1 - r%): W% = 2 * WIDE \ HIGH
   WHILE r% >= 0
      PSET (xc% + x%, yc% + r%), c%
      PSET (xc% + x%, yc% - r%), c%
      PSET (xc% - x%, yc% + r%), c%
      PSET (xc% - x%, yc% - r%), c%
      IF (d% + r%) > 0 THEN r% = r% - 1: d% = d% - W% * r% - 1
      IF x% > d% THEN x% = x% + 1: d% = d% + 2 * x% + 1
   WEND
END SUB

SUB BLine (x%, y%, x2%, y2%, c%)
'_|_|_|   Bresenham Line Drawing Algorithm
'_|_|_|   Adapted from BRESNHAM.C in Bob Stout's SNIPPETS.
   i% = 0: steep% = 0: e% = 0
   IF (x2% - x%) > 0 THEN sx% = 1:  ELSE sx% = -1
   dx% = ABS(x2% - x%)
   IF (y2% - y%) > 0 THEN sy% = 1:  ELSE sy% = -1
   dy% = ABS(y2% - y%)
   IF (dy% > dx%) THEN
      steep% = 1
      SWAP x%, y%
      SWAP dx%, dy%
      SWAP sx%, sy%
   END IF
   e% = 2 * dy% - dx%
   FOR i% = 0 TO dx% - 1
      IF steep% = 1 THEN PSET (y%, x%), c%:  ELSE PSET (x%, y%), c%
      WHILE e% >= 0
         y% = y% + sy%: e% = e% - 2 * dx%
      WEND
      x% = x% + sx%: e% = e% + 2 * dy%
   NEXT
   PSET (x2%, y2%), c%
END SUB



Alternate way to draw a circle - NecrosIhsan - 12-13-2005

Code:
SUB DrawEllipse (x AS INTEGER, y AS INTEGER, xradius AS INTEGER, yradius AS INTEGER, colour AS INTEGER)
FOR xl = xr TO 0 STEP -1
  yl = SQR(xradius * xradius - xl * xl) * yradius / xradius: Y2 = yl
  DO
    Pset (x + xl, y + yl), colour
    Pset (x - xl, y + yl), colour
    Pset (x + xl, y - yl), colour
    Pset (x - xl, y - yl), colour
    yl = yl - 1
  LOOP WHILE yl > Y1
  Y1 = Y2
NEXT
END SUB
Adapt to your language/API of choice. I used the PSET keyword to illustrate how the pixels would be plotted; adapt this to your own method to have it work correctly. Big Grin


Alternate way to draw a circle - yetifoot - 12-13-2005

theres a whole topics worth here

http://forum.qbasicnews.com/viewtopic.php?t=8796


Alternate way to draw a circle - axipher - 12-13-2005

Ya, I posted there hoping for a response, but on this topic, yetifoot's works, execpt I am not sure how to implement limits, and I can't get NecrosIhsan's routine to work.


Alternate way to draw a circle - NecrosIhsan - 12-13-2005

Maybe I messed up when I pasted it...it came from an old QB source I have here.


Alternate way to draw a circle - axipher - 12-13-2005

Oh, well maybe you didn't make an error posting it, I am using fb for my program, so using qb is out of the question.


Alternate way to draw a circle - na_th_an - 12-13-2005

You have to call the sub, you know:

Code:
Declare Sub DrawEllipse

Screen 13

DrawEllipse 160, 100, 80, 60, 15

SUB DrawEllipse (x AS INTEGER, y AS INTEGER, xr AS INTEGER, yrAS INTEGER, colour AS INTEGER)
FOR xl = xr TO 0 STEP -1
  yl = SQR(xr * xr - xl * xl) * yr / xr: Y2 = yl
  DO
    Pset (x + xl, y + yl), colour
    Pset (x - xl, y + yl), colour
    Pset (x + xl, y - yl), colour
    Pset (x - xl, y - yl), colour
    yl = yl - 1
  LOOP WHILE yl > Y1
  Y1 = Y2
NEXT
END SUB



Alternate way to draw a circle - axipher - 12-14-2005

Well I am using fb and this is what I did:

Code:
Declare Sub DrawEllipse (x AS INTEGER, y AS INTEGER, xr AS INTEGER, yrAS INTEGER, colour AS INTEGER)

Screen 13,,,1

DrawEllipse 160, 100, 80, 60, 15
Sleep

SUB DrawEllipse (x AS INTEGER, y AS INTEGER, xr AS INTEGER, yrAS INTEGER, colour AS INTEGER)
FOR xl = xr TO 0 STEP -1
  yl = SQR(xr * xr - xl * xl) * yr / xr: Y2 = yl
  DO
    Pset (x + xl, y + yl), colour
    Pset (x - xl, y + yl), colour
    Pset (x + xl, y - yl), colour
    Pset (x - xl, y - yl), colour
    yl = yl - 1
  LOOP WHILE yl > Y1
  Y1 = Y2
NEXT
END SUB



Alternate way to draw a circle - yetifoot - 12-14-2005

just tried it in FB,
should be

...yr AS INTEGER...

NOT

...yrAS INTEGER...

but otherwise worked fine.