Skip to content

Commit

Permalink
draw: fix circle
Browse files Browse the repository at this point in the history
  • Loading branch information
frioux committed Feb 22, 2021
1 parent b84c511 commit 835222d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
27 changes: 27 additions & 0 deletions internal/tool/misc/img/draw.go
Expand Up @@ -284,6 +284,33 @@ func registerImageFunctions(L *lua.LState, img ImageSetter) (cleanup func() erro
return 0
}))

L.SetGlobal("circ", L.NewFunction(func(L *lua.LState) int {
xc := int(L.CheckNumber(1))
yc := int(L.CheckNumber(2))
r := float64(L.CheckNumber(3))

border := checkColor(L, 4)
fill := checkColor(L, 5)

for x := int(-r); x <= int(r); x++ {
for y := int(-r); y <= int(r); y++ {
if x*x+y*y <= int(r*r) {
img.Set(x+xc, y+yc, fill)
}
}
}

// improve with http://weber.itn.liu.se/~stegu/circle/circlealgorithm.pdf
for t := 0.0; t < 2*math.Pi; t += 1 / r {
xt := r*math.Cos(t) + float64(xc)
yt := r*math.Sin(t) + float64(yc)

img.Set(int(math.Round(xt)), int(math.Round(yt)), border)
}

return 0
}))

L.SetGlobal("line", L.NewFunction(func(L *lua.LState) int {
x1 := float64(L.CheckNumber(1))
y1 := float64(L.CheckNumber(2))
Expand Down
6 changes: 2 additions & 4 deletions internal/tool/misc/img/draw.md
Expand Up @@ -66,10 +66,8 @@ Draws a line from (x1, y1) to (x2, y2) in color.

## BUGS

Something is wrong with `line` in certain situations (I'm assuming infinity or
NaN or something is causing the issue.) `line` is used when drawing `circle`s,
so you can see the bug by drawing a circle and there will be weird gaps in the
top and bottom of them.
Line currently has gaps when the absolute value of the slope is high. I intend
to fix that soon.

## DEBUGGING

Expand Down

0 comments on commit 835222d

Please sign in to comment.