Skip to content

Commit a965a39

Browse files
authored
gg: optimise draw_rounded_rect_filled (#26399)
1 parent 840c267 commit a965a39

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

vlib/gg/draw.c.v

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ pub fn (ctx &Context) draw_rounded_rect_empty(x f32, y f32, w f32, h f32, radius
435435
// `w` is the width, `h` is the height .
436436
// `radius` is the radius of the corner-rounding in pixels.
437437
// `c` is the color of the filled.
438-
// it divides the rounded rectangle into 3 shapes, the top part, the midle and the bottom part.
438+
// it divides the rounded rectangle into 2 shapes, the top rounded part and the bottom rounded part which are connected at both extremes.
439439
pub fn (ctx &Context) draw_rounded_rect_filled(x f32, y f32, w f32, h f32, radius f32, c Color) {
440440
if w <= 0 || h <= 0 || radius < 0 {
441441
return
@@ -467,24 +467,32 @@ pub fn (ctx &Context) draw_rounded_rect_filled(x f32, y f32, w f32, h f32, radiu
467467
// bottom y coordinate
468468
by := sy + height - r
469469

470-
if r != 0 {
470+
if r == 0 {
471+
// No radius means juste a rectangle
472+
sgl.begin_quads()
473+
sgl.v2f(sx, ty)
474+
sgl.v2f(rx + r, ty)
475+
sgl.v2f(rx + r, by)
476+
sgl.v2f(sx, by)
477+
sgl.end()
478+
} else {
479+
// draw the top then the bottom and link them with 2 triangle
471480
mut rad := f32(0)
472481
mut dx := f32(0)
473482
mut dy := f32(0)
474483

475484
// top part
485+
// starting at -30 then multiplying it by -1 makes you ends with the angle closer to the side which is needed to link both parts
476486
sgl.begin_triangle_strip()
477-
for i in 0 .. 31 {
478-
rad = f32(math.radians(i * 3))
487+
for i in -30 .. 1 {
488+
rad = f32(math.radians(-i * 3))
479489
dx = r * math.cosf(rad)
480490
dy = r * math.sinf(rad)
481491
sgl.v2f(rx + dx, ty - dy)
482492
sgl.v2f(lx - dx, ty - dy)
483493
}
484-
sgl.end()
485494

486495
// bottom part
487-
sgl.begin_triangle_strip()
488496
for i in 0 .. 31 {
489497
rad = f32(math.radians(i * 3))
490498
dx = r * math.cosf(rad)
@@ -494,14 +502,6 @@ pub fn (ctx &Context) draw_rounded_rect_filled(x f32, y f32, w f32, h f32, radiu
494502
}
495503
sgl.end()
496504
}
497-
498-
// Draw the iner rectangle
499-
sgl.begin_quads()
500-
sgl.v2f(sx, ty)
501-
sgl.v2f(rx + r, ty)
502-
sgl.v2f(rx + r, by)
503-
sgl.v2f(sx, by)
504-
sgl.end()
505505
}
506506

507507
// draw_triangle_empty draws the outline of a triangle.

0 commit comments

Comments
 (0)