How to resolve the algorithm Bitmap/Midpoint circle algorithm step by step in the FreeBASIC programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Bitmap/Midpoint circle algorithm step by step in the FreeBASIC programming language
Table of Contents
Problem Statement
Using the data storage type defined on this page for raster images, write an implementation of the midpoint circle algorithm (also known as Bresenham's circle algorithm). (definition on Wikipedia).
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Bitmap/Midpoint circle algorithm step by step in the FreeBASIC programming language
Source code in the freebasic programming language
' version 15-10-2016
' compile with: fbc -s gui
' Variant with Integer-Based Arithmetic from Wikipedia page:
' Midpoint circle algorithm
Sub circle_(x0 As Integer, y0 As Integer , radius As Integer, Col As Integer)
Dim As Integer x = radius
Dim As Integer y
' Decision criterion divided by 2 evaluated at x=r, y=0
Dim As Integer decisionOver2 = 1 - x
While(x >= y)
PSet(x0 + x, y0 + y), col
PSet(x0 - x, y0 + y), col
PSet(x0 + x, y0 - y), col
PSet(x0 - x, y0 - y), col
PSet(x0 + y, y0 + x), col
PSet(x0 - y, y0 + x), col
PSet(x0 + y, y0 - x), col
PSet(x0 - y, y0 - x), col
y = y +1
If decisionOver2 <= 0 Then
decisionOver2 += y * 2 +1 ' Change in decision criterion for y -> y +1
Else
x = x -1
decisionOver2 += (y - x) * 2 +1 ' Change for y -> y +1, x -> x -1
End If
Wend
End Sub
' ------=< MAIN >=------
ScreenRes 600, 600, 32
Dim As Integer w, h, depth
Randomize Timer
ScreenInfo w, h
For i As Integer = 1 To 10
circle_(Rnd * w, Rnd * h , Rnd * 200 , Int(Rnd *&hFFFFFF))
Next
'save screen to BMP file
BSave "Name.BMP", 0
' empty keyboard buffer
While Inkey <> "" : Wend
WindowTitle "hit any key to end program"
Sleep
End
You may also check:How to resolve the algorithm Monads/List monad step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Convex hull step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Convert decimal number to rational step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Faulhaber's triangle step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Repeat step by step in the FreeBASIC programming language