Skip to content

Commit f3db65d

Browse files
authored
Merge pull request #5347 from radarhere/edge
2 parents b939ce4 + a3dbee9 commit f3db65d

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed
108 Bytes
Loading

Tests/test_imagedraw.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1379,3 +1379,22 @@ def test_compute_regular_polygon_vertices_input_error_handling(
13791379
with pytest.raises(expected_error) as e:
13801380
ImageDraw._compute_regular_polygon_vertices(bounding_circle, n_sides, rotation)
13811381
assert str(e.value) == error_message
1382+
1383+
1384+
def test_continuous_horizontal_edges_polygon():
1385+
xy = [
1386+
(2, 6),
1387+
(6, 6),
1388+
(12, 6),
1389+
(12, 12),
1390+
(8, 12),
1391+
(8, 8),
1392+
(4, 8),
1393+
(2, 8),
1394+
]
1395+
img, draw = create_base_image_draw((16, 16))
1396+
draw.polygon(xy, BLACK)
1397+
expected = os.path.join(IMAGES_PATH, "continuous_horizontal_edges_polygon.png")
1398+
assert_image_equal_tofile(
1399+
img, expected, "continuous horizontal edges polygon failed"
1400+
)

src/libImaging/Draw.c

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,7 @@ ImagingDrawRectangle(
734734

735735
int
736736
ImagingDrawPolygon(Imaging im, int count, int *xy, const void *ink_, int fill, int op) {
737-
int i, n;
737+
int i, n, x0, y0, x1, y1;
738738
DRAW *draw;
739739
INT32 ink;
740740

@@ -753,20 +753,38 @@ ImagingDrawPolygon(Imaging im, int count, int *xy, const void *ink_, int fill, i
753753
return -1;
754754
}
755755
for (i = n = 0; i < count - 1; i++) {
756-
add_edge(&e[n++], xy[i + i], xy[i + i + 1], xy[i + i + 2], xy[i + i + 3]);
756+
x0 = xy[i * 2];
757+
y0 = xy[i * 2 + 1];
758+
x1 = xy[i * 2 + 2];
759+
y1 = xy[i * 2 + 3];
760+
if (y0 == y1 && i != 0 && y0 == xy[i * 2 - 1]) {
761+
// This is a horizontal line,
762+
// that immediately follows another horizontal line
763+
Edge *last_e = &e[n-1];
764+
if (x1 > x0 && x0 > xy[i * 2 - 2]) {
765+
// They are both increasing in x
766+
last_e->xmax = x1;
767+
continue;
768+
} else if (x1 < x0 && x0 < xy[i * 2 - 2]) {
769+
// They are both decreasing in x
770+
last_e->xmin = x1;
771+
continue;
772+
}
773+
}
774+
add_edge(&e[n++], x0, y0, x1, y1);
757775
}
758-
if (xy[i + i] != xy[0] || xy[i + i + 1] != xy[1]) {
759-
add_edge(&e[n++], xy[i + i], xy[i + i + 1], xy[0], xy[1]);
776+
if (xy[i * 2] != xy[0] || xy[i * 2 + 1] != xy[1]) {
777+
add_edge(&e[n++], xy[i * 2], xy[i * 2 + 1], xy[0], xy[1]);
760778
}
761779
draw->polygon(im, n, e, ink, 0);
762780
free(e);
763781

764782
} else {
765783
/* Outline */
766784
for (i = 0; i < count - 1; i++) {
767-
draw->line(im, xy[i + i], xy[i + i + 1], xy[i + i + 2], xy[i + i + 3], ink);
785+
draw->line(im, xy[i * 2], xy[i * 2 + 1], xy[i * 2 + 2], xy[i * 2 + 3], ink);
768786
}
769-
draw->line(im, xy[i + i], xy[i + i + 1], xy[0], xy[1], ink);
787+
draw->line(im, xy[i * 2], xy[i * 2 + 1], xy[0], xy[1], ink);
770788
}
771789

772790
return 0;

0 commit comments

Comments
 (0)