Skip to content

Commit 412c7b0

Browse files
authored
gg: add draw_ellipse_thick and draw_ellipse_thick_rotate (#26327)
1 parent 32632c1 commit 412c7b0

2 files changed

Lines changed: 68 additions & 1 deletion

File tree

vlib/gg/draw.c.v

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,6 +1009,32 @@ pub fn (ctx &Context) draw_ellipse_empty(x f32, y f32, rw f32, rh f32, c Color)
10091009
sgl.end()
10101010
}
10111011

1012+
// draw_ellipse_empty draws the outline of an ellipse.
1013+
// `x`,`y` defines the center of the ellipse.
1014+
// `rw` defines the *width* radius of the ellipse.
1015+
// `rh` defines the *height* radius of the ellipse.
1016+
// `th` defines the *thickness* of the ellipse.
1017+
// `c` is the color of the outline.
1018+
pub fn (ctx &Context) draw_ellipse_thick(x f32, y f32, rw f32, rh f32, th f32, c Color) {
1019+
if c.a != 255 {
1020+
sgl.load_pipeline(ctx.pipeline.alpha)
1021+
}
1022+
sgl.c4b(c.r, c.g, c.b, c.a)
1023+
1024+
sgl.begin_quads()
1025+
for i := 0; i < 360; i += 10 {
1026+
sgl.v2f(x + math.sinf(f32(math.radians(i + 10))) * (rw - th / 2), y +
1027+
math.cosf(f32(math.radians(i + 10))) * (rh - th / 2))
1028+
sgl.v2f(x + math.sinf(f32(math.radians(i))) * (rw - th / 2), y +
1029+
math.cosf(f32(math.radians(i))) * (rh - th / 2))
1030+
sgl.v2f(x + math.sinf(f32(math.radians(i))) * (rw + th / 2), y +
1031+
math.cosf(f32(math.radians(i))) * (rh + th / 2))
1032+
sgl.v2f(x + math.sinf(f32(math.radians(i + 10))) * (rw + th / 2), y +
1033+
math.cosf(f32(math.radians(i + 10))) * (rh + th / 2))
1034+
}
1035+
sgl.end()
1036+
}
1037+
10121038
// draw_ellipse_filled draws an opaque ellipse.
10131039
// `x`,`y` defines the center of the ellipse.
10141040
// `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
10571083
sgl.end()
10581084
}
10591085

1086+
// draw_ellipse_empty draws the outline of an ellipse.
1087+
// `x`,`y` defines the center of the ellipse.
1088+
// `rw` defines the *width* radius of the ellipse.
1089+
// `rh` defines the *height* radius of the ellipse.
1090+
// `th` defines the *thickness* of the ellipse.
1091+
// `rota` defines the *rotation* angle of the ellipse, in radians.
1092+
// `c` is the color of the outline.
1093+
pub fn (ctx &Context) draw_ellipse_thick_rotate(x f32, y f32, rw f32, rh f32, th f32, rota f32, c Color) {
1094+
if c.a != 255 {
1095+
sgl.load_pipeline(ctx.pipeline.alpha)
1096+
}
1097+
sgl.c4b(c.r, c.g, c.b, c.a)
1098+
1099+
cos_rot := math.cosf(rota)
1100+
sin_rot := math.sinf(rota)
1101+
sgl.begin_quads()
1102+
for i := 0; i < 360; i += 10 {
1103+
xfactor_current := math.sinf(f32(math.radians(i)))
1104+
xfactor_next := math.sinf(f32(math.radians(i + 10)))
1105+
yfactor_current := math.cosf(f32(math.radians(i)))
1106+
yfactor_next := math.cosf(f32(math.radians(i + 10)))
1107+
1108+
sgl.v2f(x + xfactor_next * (rw - th / 2) * cos_rot - yfactor_next * (rh - th / 2) * sin_rot,
1109+
y + yfactor_next * (rh - th / 2) * cos_rot + xfactor_next * (rw - th / 2) * sin_rot)
1110+
sgl.v2f(x + xfactor_current * (rw - th / 2) * cos_rot - yfactor_current * (rh - th / 2) * sin_rot,
1111+
y + yfactor_current * (rh - th / 2) * cos_rot +
1112+
xfactor_current * (rw - th / 2) * sin_rot)
1113+
sgl.v2f(x + xfactor_current * (rw + th / 2) * cos_rot - yfactor_current * (rh + th / 2) * sin_rot,
1114+
y + yfactor_current * (rh + th / 2) * cos_rot + xfactor_current * (rw +
1115+
th / 2) * sin_rot)
1116+
sgl.v2f(x + xfactor_next * (rw + th / 2) * cos_rot - yfactor_next * (rh + th / 2) * sin_rot,
1117+
y + yfactor_next * (rh + th / 2) * cos_rot + xfactor_next * (rw + th / 2) * sin_rot)
1118+
}
1119+
sgl.end()
1120+
}
1121+
10601122
// draw_ellipse_filled draws an opaque ellipse.
10611123
// `x`,`y` defines the center of the ellipse.
10621124
// `rw` defines the *width* radius of the ellipse.

vlib/gg/testdata/draw_elipses.vv

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import gg
22

3+
const thickness = f32(5)
4+
35
gg.start(
46
window_title: 'Ellipses'
57
width: 600
6-
height: 200
8+
height: 400
79
frame_fn: fn (mut ctx gg.Context) {
810
ctx.begin()
911
drot := f32(ctx.frame) / 60
@@ -13,6 +15,9 @@ gg.start(
1315
ctx.draw_ellipse_empty_rotate(300, 100, 50, 25, -drot, gg.black)
1416
ctx.draw_ellipse_filled_rotate(500, 100, 100, 50, drot, gg.yellow)
1517
ctx.draw_ellipse_empty_rotate(500, 100, 50, 25, drot, gg.black)
18+
ctx.draw_ellipse_thick(100, 300, 100, 50, thickness, gg.green)
19+
ctx.draw_ellipse_thick_rotate(300, 300, 100, 50, thickness, drot, gg.purple)
20+
ctx.draw_ellipse_thick_rotate(500, 300, 100, 50, thickness, -drot, gg.indigo)
1621
ctx.end()
1722
}
1823
)

0 commit comments

Comments
 (0)