Bitmap/Midpoint circle algorithm: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: added/changed comments and whitespace, changed indentations, simplified some code.)
m (→‎{{header|FreeBASIC}}: minor change)
Line 844: Line 844:
end subroutine draw_circle_sc</lang>
end subroutine draw_circle_sc</lang>
=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<lang FreeBASIC>' version 06-07-2015
<lang FreeBASIC>' version 15-10-2016
' compile with: fbc -s console
' compile with: fbc -s gui
' OR compile with: fbc -s gui


' Variant with Integer-Based Arithmetic from Wikipedia page: Midpoint circle algorithm
' 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)
Sub circle_(x0 As Integer, y0 As Integer , radius As Integer, Col As Integer)


Dim As Integer x = radius
Dim As Integer x = radius
Dim As Integer y
Dim As Integer y
Dim As Integer decisionOver2 = 1 - x ' Decision criterion divided by 2 evaluated at x=r, y=0
' 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


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
End Sub


' ------=< MAIN >=------
' ------=< MAIN >=------

ScreenRes 800, 800, 32
ScreenRes 600, 600, 32
Dim As Integer w, h, depth
Dim As Integer w, h, depth
Randomize Timer
Randomize Timer
Line 882: Line 884:
ScreenInfo w, h
ScreenInfo w, h


For i As Integer = 0 To 50
For i As Integer = 1 To 10
circle_(Rnd * w, Rnd * h , Rnd * i * 4 , Int(Rnd * &hFFFFFF))
circle_(Rnd * w, Rnd * h , Rnd * 200 , Int(Rnd *&hFFFFFF))
Next
Next



'save screen to BMP file
'save screen to BMP file
Bsave "Name.BMP", 0
BSave "Name.BMP", 0



' empty keyboard buffer
' empty keyboard buffer
While InKey <> "" : Wend
While Inkey <> "" : Wend
Print : Print "hit any key to end program"
WindowTitle "hit any key to end program"
Sleep
Sleep
End</lang>
End</lang>