Skip to content

Instantly share code, notes, and snippets.

@soypat
Created June 10, 2025 14:54
Show Gist options
  • Save soypat/21aafe4df2c5ade024c0202c58a5c7d5 to your computer and use it in GitHub Desktop.
Save soypat/21aafe4df2c5ade024c0202c58a5c7d5 to your computer and use it in GitHub Desktop.
Midpoint circle algorithm using Jeska's algorithm
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--
}
}
}
@soypat
Copy link
Author

soypat commented Jun 10, 2025

A generalized version available at my next gist: https://gist.github.com/soypat/1253c460cac3a5e0c92862b879a02577

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment