Created
June 10, 2025 14:54
-
-
Save soypat/21aafe4df2c5ade024c0202c58a5c7d5 to your computer and use it in GitHub Desktop.
Midpoint circle algorithm using Jeska's algorithm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jeska | |
import ( | |
"image" | |
"image/color" | |
) | |
func CircleOutline(img *image.RGBA, r int, c color.RGBA) { | |
t1 := r / 16 // Nicer looking circle when initialized. | |
x := r | |
y := 0 | |
for x >= y { | |
img.Set(x, y, c) | |
img.Set(x, -y, c) | |
img.Set(-x, y, c) | |
img.Set(-x, -y, c) | |
img.Set(y, x, c) | |
img.Set(y, -x, c) | |
img.Set(-y, x, c) | |
img.Set(-y, -x, c) | |
y++ | |
t1 += y | |
t2 := t1 - x | |
if t2 >= 0 { | |
t1 = t2 | |
x-- | |
} | |
} | |
} | |
func CircleFill(img *image.RGBA, r int, c color.RGBA) { | |
t1 := r / 16 // Nicer looking circle when initialized. | |
x := r | |
y := 0 | |
for x >= y { | |
for ix := -x; ix <= x; ix++ { | |
img.Set(ix, y, c) | |
img.Set(ix, -y, c) | |
img.Set(y, ix, c) | |
img.Set(-y, ix, c) | |
} | |
y++ | |
t1 += y | |
t2 := t1 - x | |
if t2 >= 0 { | |
t1 = t2 | |
x-- | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A generalized version available at my next gist: https://gist.github.com/soypat/1253c460cac3a5e0c92862b879a02577