You are not logged in or registered. Please login or register to use the full functionality of this board...
SIGN IN Join Our Community For FREE


Bresenham Like Circle Algo
05-27-2017, 12:26 PM (This post was last modified: 05-27-2017 12:29 PM by Andy_A.)
Post: #1
 (Print Post)
Bresenham Like Circle Algo
Here's a nice and fast circle algo if you ever need to roll your own circle() function.

The PDF with the explanation and source code for your perusal is here:
http://web.engr.oregonstate.edu/~sllu/bcircle.pdf

Here's my BlitzPlus implementation. (8ms for 100 circles of 590 pixel diameter if I remember correctly)
Code Snippet: [Select]
;Source:
;    http://homepage.smc.edu/kennedy_john/papers.htm
;    http://homepage.smc.edu/kennedy_john/bcircle.pdf

;    A Fast Bresenham Type Algorithm For Drawing Circles
;    by
;    John Kennedy

;    Blitzplus/Blitz 3D port by Andy_A


AppTitle "Bresenham Circle"
Graphics 800,600,32,2
SetBuffer BackBuffer()

numIterations% = 100

;Color 255, 224, 32

LockBuffer GraphicsBuffer()

st = MilliSecs()
For i = 1 To numIterations
    PlotCircle(400, 300, 290, $FFE020, 1)
Next
et1# = MilliSecs()-st
UnlockBuffer


st = MilliSecs()            ;NOTE: Blitz Filled OVAL will *NOT* draw on Locked Buffer
For i = 1 To numIterations
    Oval 110, 10, 580, 580, 1
Next
et2# = MilliSecs()-st



Text 5,    5,"Avg/Circle: "+(et1/Float(numIterations))+"ms"
Text 5,20,"  Avg/Oval: "+(et2/Float(numIterations))+"ms"

Flip
WaitMouse()
End

Function PlotCircle(CX%, CY%, R%, colr%, fill% = 0)
    Local X%, Y%
    Local XChange%, YChange%
    Local RadiusError%
    Color (colr And $FF0000) Shr 16, (colr And $FF00) Shr 8, colr And $FF
    X = R
    Y = 0
    XChange = 1 - (R Shl 1)
    YChange = 1
    RadiusError = 0

    While  X >= Y

        If fill <> 0 Then
            Line(CX-X, CY+Y, CX+X, CY+Y)        ;used calc'd values to draw
            Line(CX-X, CY-Y, CX+X, CY-Y)        ;scan lines from points
            Line(CX-Y, CY+X, CX+Y, CY+X)        ;in opposite octants
            Line(CX-Y, CY-X, CX+Y, CY-X)
        Else
            WritePixelFast(CX+X, CY+Y, colr)    ; {point in octant 1}
            WritePixelFast(CX-X, CY+Y, colr)    ; {point in octant 4}
            WritePixelFast(CX-X, CY-Y, colr)    ; {point in octant 5}
            WritePixelFast(CX+X, CY-Y, colr)    ; {point in octant 8}
            WritePixelFast(CX+Y, CY+X, colr)    ; {point in octant 2}
            WritePixelFast(CX-Y, CY+X, colr)    ; {point in octant 3}
            WritePixelFast(CX-Y, CY-X, colr)    ; {point in octant 6}
            WritePixelFast(CX+Y, CY-X, colr)    ; {point in octant 7}
        End If

        Y = Y + 1
        RadiusError = RadiusError + YChange
        YChange = YChange + 2

        If (RadiusError Shl 1) + XChange > 0 Then
            X = X - 1
            RadiusError = RadiusError + XChange
            XChange = XChange + 2
        End If
    Wend
End Function
Find all posts by this user
Like Post
05-27-2017, 02:31 PM (This post was last modified: 05-27-2017 04:17 PM by bplus.)
Post: #2
 (Print Post)
RE: Bresenham Like Circle Algo
Hi Andy,

SQR() was not used, very interesting! Got to try it, thanks!

UPDATE: Confirmed in SmallBASIC, best time yet for algo!

B += _
Find all posts by this user
Like Post
05-29-2017, 04:29 AM
Post: #3
 (Print Post)
RE: Bresenham Like Circle Algo
This is pretty cool... I'm running Blitz3D, on my Linux Mint box, using Wine... As a result, my times are relatively poor, compared to running on native Windows. Circle = 31.13ms and Oval = 0.3ms. But, still cool, none the less.

Just gotta love those 8 direction algo's. Sweet.

Good Job.

J

May your journey be free of incident.

Live long and prosper.
Find all posts by this user
Like Post



Forum Jump:


User(s) browsing this thread: 1 Guest(s)




QB64 Member Project - STxAxTIC 3D World
QB64 Member Project - Overboard
QB64 Member Project - Rubix's Magic
QB64 Member Project - Kings Vallery version two
QB64 Member Project - Score 4
QB64 Member Project - 9 Board
QB64 Member Project - Algeria Weather
QB64 Member Project - Pivet version one
QB64 Member Project - Line Thickness
QB64 Member Project - Color Triangles
QB64 Member Project - Basic Dithering
QB64 Member Project - Foursight
QB64 Member Project - Martin Fractals version one
QB64 Member Project - Quarto
QB64 Member Project - Red Scrolling LED Sign
QB64 Member Project - OpenGL Triangles
QB64 Member Project - Dakapo
QB64 Member Project - Color Rotating Text
QB64 Member Project - MAPTRIANGLE
QB64 Member Project - Martin Fractals version two
QB64 Member Project - Spinning Color Wheel
QB64 Member Project - Bowditch curve
QB64 Member Project - Splatter
QB64 Member Project - Amazon
QB64 Member Project - Input
QB64 Member Project - Kobolts Monopoly
QB64 Member Project - ARB Checkers
QB64 Member Project - Rotating Background
QB64 Member Project - Full Color LED Sign
QB64 Member Project - Othello
QB64 Member Project - Domain
QB64 Member Project - Touche
QB64 Member Project - Kings Valley verion one
QB64 Member Project - Sabotage
QB64 Member Project - Blokus
QB64 Member Project - Swirl
QB64 Member Project - Spiro Roses
QB64 Member Project - Dreamy Clock
QB64 Member Project - Isolation
QB64 Member Project - Martin Fractals version three
QB64 Member Project - Exit
QB64 Member Project - Connect Four
QB64 Member Project - Pivot version two
QB64 Member Project - Qubic
QB64 Member Project - Martin Fractals version four
QB64 Member Project - Inside Moves
QB64 Member Project - RGB Color Wheel
QB64 Member Project - Kings Court
QB64 Member Project - Point Blank