Skip to content

Instantly share code, notes, and snippets.

@beppe9000
Created June 22, 2015 00:38
Show Gist options
  • Save beppe9000/e4a29542a76b8ee3b47f to your computer and use it in GitHub Desktop.
Save beppe9000/e4a29542a76b8ee3b47f to your computer and use it in GitHub Desktop.
Midpoint Circle Algorithm
Function MakeCircleApprox(cx As Integer, cy As Integer, radius As Integer) As List(Of Tuple(Of Integer, Integer))
Dim ret_coords As New List(Of Tuple(Of Integer, Integer))
Dim x, y, decisionOver2 As Integer
x = radius
y = 0
decisionOver2 = 1 - x ' Decision criterion divided by 2 evaluated at x=r, y=0
While (x >= y)
ret_coords.Add(Tuple.Create(x + cx, y + cy))
ret_coords.Add(Tuple.Create(y + cx, x + cy))
ret_coords.Add(Tuple.Create(-x + cx, y + cy))
ret_coords.Add(Tuple.Create(-y + cx, x + cy))
ret_coords.Add(Tuple.Create(-x + cx, -y + cy))
ret_coords.Add(Tuple.Create(-y + cx, -x + cy))
ret_coords.Add(Tuple.Create(x + cx, -y + cy))
ret_coords.Add(Tuple.Create(y + cx, -x + cy))
y += 1
If (decisionOver2 <= 0) Then
decisionOver2 += 2 * y + 1 'Change in decision criterion for y -> y+1
Else
x -= 1
decisionOver2 += 2 * (y - x) + 1 ' Change for y -> y+1, x -> x-1
End If
End While
Return ret_coords
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment