Skip to content

Commit 835222d

Browse files
committed
draw: fix circle
1 parent b84c511 commit 835222d

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

internal/tool/misc/img/draw.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,33 @@ func registerImageFunctions(L *lua.LState, img ImageSetter) (cleanup func() erro
284284
return 0
285285
}))
286286

287+
L.SetGlobal("circ", L.NewFunction(func(L *lua.LState) int {
288+
xc := int(L.CheckNumber(1))
289+
yc := int(L.CheckNumber(2))
290+
r := float64(L.CheckNumber(3))
291+
292+
border := checkColor(L, 4)
293+
fill := checkColor(L, 5)
294+
295+
for x := int(-r); x <= int(r); x++ {
296+
for y := int(-r); y <= int(r); y++ {
297+
if x*x+y*y <= int(r*r) {
298+
img.Set(x+xc, y+yc, fill)
299+
}
300+
}
301+
}
302+
303+
// improve with http://weber.itn.liu.se/~stegu/circle/circlealgorithm.pdf
304+
for t := 0.0; t < 2*math.Pi; t += 1 / r {
305+
xt := r*math.Cos(t) + float64(xc)
306+
yt := r*math.Sin(t) + float64(yc)
307+
308+
img.Set(int(math.Round(xt)), int(math.Round(yt)), border)
309+
}
310+
311+
return 0
312+
}))
313+
287314
L.SetGlobal("line", L.NewFunction(func(L *lua.LState) int {
288315
x1 := float64(L.CheckNumber(1))
289316
y1 := float64(L.CheckNumber(2))

internal/tool/misc/img/draw.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,8 @@ Draws a line from (x1, y1) to (x2, y2) in color.
6666

6767
## BUGS
6868

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

7472
## DEBUGGING
7573

0 commit comments

Comments
 (0)