Skip to content

Commit 76f6f99

Browse files
author
Benjamin Stigsen
authored
gg: add draw_ring() (#12817)
1 parent b1a9bf2 commit 76f6f99

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

vlib/gg/gg.v

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,53 @@ pub fn (ctx &Context) draw_line_with_config(x f32, y f32, x2 f32, y2 f32, config
529529
sgl.pop_matrix()
530530
}
531531

532+
pub fn (ctx &Context) draw_ring(x f32, y f32, inner_r f32, outer_r f32, start_angle f32, end_angle f32, segments int, color gx.Color) {
533+
if start_angle == end_angle || outer_r <= 0.0 {
534+
return
535+
}
536+
537+
mut r1 := inner_r
538+
mut r2 := outer_r
539+
mut a1 := start_angle
540+
mut a2 := end_angle
541+
542+
// TODO: Maybe this does not make since inner_r and outer_r is actually integers?
543+
if outer_r < inner_r {
544+
r1, r2 = r2, r1
545+
546+
if r2 <= 0.0 {
547+
r2 = 0.1
548+
}
549+
}
550+
551+
if a2 < a1 {
552+
a1, a2 = a2, a1
553+
}
554+
555+
if r1 <= 0.0 {
556+
ctx.draw_arc(x, y, int(r2), a1, a2, segments, color)
557+
return
558+
}
559+
560+
mut step_length := (a2 - a1) / f32(segments)
561+
mut angle := a1
562+
563+
sgl.begin_quads()
564+
sgl.c4b(color.r, color.g, color.b, color.a)
565+
for _ in 0 .. segments {
566+
sgl.v2f(x + f32(math.sin(angle)) * r1, y + f32(math.cos(angle) * r1))
567+
sgl.v2f(x + f32(math.sin(angle)) * r2, y + f32(math.cos(angle) * r2))
568+
569+
sgl.v2f(x + f32(math.sin(angle + step_length)) * r2, y + f32(math.cos(angle +
570+
step_length) * r2))
571+
sgl.v2f(x + f32(math.sin(angle + step_length)) * r1, y + f32(math.cos(angle +
572+
step_length) * r1))
573+
574+
angle += step_length
575+
}
576+
sgl.end()
577+
}
578+
532579
pub fn (ctx &Context) draw_rounded_rect(x f32, y f32, w f32, h f32, radius f32, color gx.Color) {
533580
sgl.c4b(color.r, color.g, color.b, color.a)
534581
sgl.begin_triangle_strip()

0 commit comments

Comments
 (0)