Skip to content

Commit f5b9a1d

Browse files
schodetdlech
authored andcommitted
pbio/image: Improve circle and disc shapes.
Previous code was too strict on the circle radius, resulting in pointy circles. Do not touch rounded rectangles, they looks better with small radii. Refs: pybricks/support#2154
1 parent cbbba74 commit f5b9a1d

File tree

2 files changed

+25
-21
lines changed

2 files changed

+25
-21
lines changed

lib/pbio/src/image/image.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,7 @@ void pbio_image_draw_circle(pbio_image_t *image, int x, int y, int r,
693693

694694
// Draw.
695695
int dx = 0, dy = r;
696-
int r2 = r * r;
696+
int err = 5 - r * 4;
697697
while (dx <= dy) {
698698
pbio_image_draw_pixel(image, x + dx, y + dy, value);
699699
pbio_image_draw_pixel(image, x - dx, y + dy, value);
@@ -703,10 +703,12 @@ void pbio_image_draw_circle(pbio_image_t *image, int x, int y, int r,
703703
pbio_image_draw_pixel(image, x - dy, y + dx, value);
704704
pbio_image_draw_pixel(image, x + dy, y - dx, value);
705705
pbio_image_draw_pixel(image, x - dy, y - dx, value);
706-
dx++;
707-
if (dx * dx + dy * dy > r2) {
706+
if (err > 0) {
708707
dy--;
708+
err -= dy * 8;
709709
}
710+
dx++;
711+
err += 4 + dx * 8;
710712
}
711713
}
712714

@@ -729,7 +731,7 @@ void pbio_image_fill_circle(pbio_image_t *image, int x, int y, int r,
729731

730732
// Draw.
731733
int dx = 0, dy = r;
732-
int r2 = r * r;
734+
int err = 5 - r * 4;
733735
while (dx <= dy) {
734736
// Optimization opportunity: when dy is not moving, the same pixels
735737
// are drawn again and again.
@@ -739,10 +741,12 @@ void pbio_image_fill_circle(pbio_image_t *image, int x, int y, int r,
739741
pbio_image_draw_hline(image, x - dy, y + dx, 1 + 2 * dy, value);
740742
pbio_image_draw_hline(image, x - dy, y - dx, 1 + 2 * dy, value);
741743
}
742-
dx++;
743-
if (dx * dx + dy * dy > r2) {
744+
if (err > 0) {
744745
dy--;
746+
err -= dy * 8;
745747
}
748+
dx++;
749+
err += 4 + dx * 8;
746750
}
747751
}
748752

lib/pbio/test/src/test_image.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -747,23 +747,23 @@ static void test_image_draw_circle(void *env) {
747747
test_image_prepare_small_image(&small);
748748
pbio_image_draw_circle(&small, 3, 3, 3, '*');
749749
tt_want_small_image(
750-
"...*................"
751-
".**.**.............."
750+
"..***..............."
752751
".*...*.............."
753752
"*.....*............."
753+
"*.....*............."
754+
"*.....*............."
754755
".*...*.............."
755-
".**.**.............."
756-
"...*................");
756+
"..***...............");
757757

758758
test_image_prepare_small_image(&small);
759759
pbio_image_draw_circle(&small, 6, 6, 6, '*');
760760
tt_want_small_image(
761-
"......*............."
762-
"...***.***.........."
761+
"....*****..........."
762+
"...*.....*.........."
763763
"..*.......*........."
764764
".*.........*........"
765-
".*.........*........"
766-
".*.........*........"
765+
"*...........*......."
766+
"*...........*......."
767767
"*...........*.......");
768768

769769
test_image_prepare_images(&outer, &inner, &full);
@@ -786,23 +786,23 @@ static void test_image_fill_circle(void *env) {
786786
test_image_prepare_small_image(&small);
787787
pbio_image_fill_circle(&small, 3, 3, 3, '*');
788788
tt_want_small_image(
789-
"...*................"
790-
".*****.............."
789+
"..***..............."
791790
".*****.............."
792791
"*******............."
792+
"*******............."
793+
"*******............."
793794
".*****.............."
794-
".*****.............."
795-
"...*................");
795+
"..***...............");
796796

797797
test_image_prepare_small_image(&small);
798798
pbio_image_fill_circle(&small, 6, 6, 6, '*');
799799
tt_want_small_image(
800-
"......*............."
800+
"....*****..........."
801801
"...*******.........."
802802
"..*********........."
803803
".***********........"
804-
".***********........"
805-
".***********........"
804+
"*************......."
805+
"*************......."
806806
"*************.......");
807807

808808
test_image_prepare_images(&outer, &inner, &full);

0 commit comments

Comments
 (0)