From 412c7b0368469a8c386ea45e0775abbe780a1929 Mon Sep 17 00:00:00 2001 From: Linklancien <123779978+Linklancien@users.noreply.github.com> Date: Mon, 12 Jan 2026 13:57:06 +0100 Subject: [PATCH] gg: add draw_ellipse_thick and draw_ellipse_thick_rotate (#26327) --- vlib/gg/draw.c.v | 62 ++++++++++++++++++++++++++++++++ vlib/gg/testdata/draw_elipses.vv | 7 +++- 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/vlib/gg/draw.c.v b/vlib/gg/draw.c.v index 501b2b572..adb1818c2 100644 --- a/vlib/gg/draw.c.v +++ b/vlib/gg/draw.c.v @@ -1009,6 +1009,32 @@ pub fn (ctx &Context) draw_ellipse_empty(x f32, y f32, rw f32, rh f32, c Color) sgl.end() } +// draw_ellipse_empty draws the outline of an ellipse. +// `x`,`y` defines the center of the ellipse. +// `rw` defines the *width* radius of the ellipse. +// `rh` defines the *height* radius of the ellipse. +// `th` defines the *thickness* of the ellipse. +// `c` is the color of the outline. +pub fn (ctx &Context) draw_ellipse_thick(x f32, y f32, rw f32, rh f32, th f32, c Color) { + if c.a != 255 { + sgl.load_pipeline(ctx.pipeline.alpha) + } + sgl.c4b(c.r, c.g, c.b, c.a) + + sgl.begin_quads() + for i := 0; i < 360; i += 10 { + sgl.v2f(x + math.sinf(f32(math.radians(i + 10))) * (rw - th / 2), y + + math.cosf(f32(math.radians(i + 10))) * (rh - th / 2)) + sgl.v2f(x + math.sinf(f32(math.radians(i))) * (rw - th / 2), y + + math.cosf(f32(math.radians(i))) * (rh - th / 2)) + sgl.v2f(x + math.sinf(f32(math.radians(i))) * (rw + th / 2), y + + math.cosf(f32(math.radians(i))) * (rh + th / 2)) + sgl.v2f(x + math.sinf(f32(math.radians(i + 10))) * (rw + th / 2), y + + math.cosf(f32(math.radians(i + 10))) * (rh + th / 2)) + } + sgl.end() +} + // draw_ellipse_filled draws an opaque ellipse. // `x`,`y` defines the center of the ellipse. // `rw` defines the *width* radius of the ellipse. @@ -1057,6 +1083,42 @@ pub fn (ctx &Context) draw_ellipse_empty_rotate(x f32, y f32, rw f32, rh f32, ro sgl.end() } +// draw_ellipse_empty draws the outline of an ellipse. +// `x`,`y` defines the center of the ellipse. +// `rw` defines the *width* radius of the ellipse. +// `rh` defines the *height* radius of the ellipse. +// `th` defines the *thickness* of the ellipse. +// `rota` defines the *rotation* angle of the ellipse, in radians. +// `c` is the color of the outline. +pub fn (ctx &Context) draw_ellipse_thick_rotate(x f32, y f32, rw f32, rh f32, th f32, rota f32, c Color) { + if c.a != 255 { + sgl.load_pipeline(ctx.pipeline.alpha) + } + sgl.c4b(c.r, c.g, c.b, c.a) + + cos_rot := math.cosf(rota) + sin_rot := math.sinf(rota) + sgl.begin_quads() + for i := 0; i < 360; i += 10 { + xfactor_current := math.sinf(f32(math.radians(i))) + xfactor_next := math.sinf(f32(math.radians(i + 10))) + yfactor_current := math.cosf(f32(math.radians(i))) + yfactor_next := math.cosf(f32(math.radians(i + 10))) + + sgl.v2f(x + xfactor_next * (rw - th / 2) * cos_rot - yfactor_next * (rh - th / 2) * sin_rot, + y + yfactor_next * (rh - th / 2) * cos_rot + xfactor_next * (rw - th / 2) * sin_rot) + sgl.v2f(x + xfactor_current * (rw - th / 2) * cos_rot - yfactor_current * (rh - th / 2) * sin_rot, + y + yfactor_current * (rh - th / 2) * cos_rot + + xfactor_current * (rw - th / 2) * sin_rot) + sgl.v2f(x + xfactor_current * (rw + th / 2) * cos_rot - yfactor_current * (rh + th / 2) * sin_rot, + y + yfactor_current * (rh + th / 2) * cos_rot + xfactor_current * (rw + + th / 2) * sin_rot) + sgl.v2f(x + xfactor_next * (rw + th / 2) * cos_rot - yfactor_next * (rh + th / 2) * sin_rot, + y + yfactor_next * (rh + th / 2) * cos_rot + xfactor_next * (rw + th / 2) * sin_rot) + } + sgl.end() +} + // draw_ellipse_filled draws an opaque ellipse. // `x`,`y` defines the center of the ellipse. // `rw` defines the *width* radius of the ellipse. diff --git a/vlib/gg/testdata/draw_elipses.vv b/vlib/gg/testdata/draw_elipses.vv index 9e0b59630..1ceb352b5 100644 --- a/vlib/gg/testdata/draw_elipses.vv +++ b/vlib/gg/testdata/draw_elipses.vv @@ -1,9 +1,11 @@ import gg +const thickness = f32(5) + gg.start( window_title: 'Ellipses' width: 600 - height: 200 + height: 400 frame_fn: fn (mut ctx gg.Context) { ctx.begin() drot := f32(ctx.frame) / 60 @@ -13,6 +15,9 @@ gg.start( ctx.draw_ellipse_empty_rotate(300, 100, 50, 25, -drot, gg.black) ctx.draw_ellipse_filled_rotate(500, 100, 100, 50, drot, gg.yellow) ctx.draw_ellipse_empty_rotate(500, 100, 50, 25, drot, gg.black) + ctx.draw_ellipse_thick(100, 300, 100, 50, thickness, gg.green) + ctx.draw_ellipse_thick_rotate(300, 300, 100, 50, thickness, drot, gg.purple) + ctx.draw_ellipse_thick_rotate(500, 300, 100, 50, thickness, -drot, gg.indigo) ctx.end() } ) -- 2.39.5