Merge pull request #5347 from radarhere/edge

This commit is contained in:
Hugo van Kemenade 2021-06-28 17:17:17 +03:00 committed by GitHub
commit f3db65db09
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 6 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 108 B

View File

@ -1379,3 +1379,22 @@ def test_compute_regular_polygon_vertices_input_error_handling(
with pytest.raises(expected_error) as e: with pytest.raises(expected_error) as e:
ImageDraw._compute_regular_polygon_vertices(bounding_circle, n_sides, rotation) ImageDraw._compute_regular_polygon_vertices(bounding_circle, n_sides, rotation)
assert str(e.value) == error_message assert str(e.value) == error_message
def test_continuous_horizontal_edges_polygon():
xy = [
(2, 6),
(6, 6),
(12, 6),
(12, 12),
(8, 12),
(8, 8),
(4, 8),
(2, 8),
]
img, draw = create_base_image_draw((16, 16))
draw.polygon(xy, BLACK)
expected = os.path.join(IMAGES_PATH, "continuous_horizontal_edges_polygon.png")
assert_image_equal_tofile(
img, expected, "continuous horizontal edges polygon failed"
)

View File

@ -734,7 +734,7 @@ ImagingDrawRectangle(
int int
ImagingDrawPolygon(Imaging im, int count, int *xy, const void *ink_, int fill, int op) { ImagingDrawPolygon(Imaging im, int count, int *xy, const void *ink_, int fill, int op) {
int i, n; int i, n, x0, y0, x1, y1;
DRAW *draw; DRAW *draw;
INT32 ink; INT32 ink;
@ -753,10 +753,28 @@ ImagingDrawPolygon(Imaging im, int count, int *xy, const void *ink_, int fill, i
return -1; return -1;
} }
for (i = n = 0; i < count - 1; i++) { for (i = n = 0; i < count - 1; i++) {
add_edge(&e[n++], xy[i + i], xy[i + i + 1], xy[i + i + 2], xy[i + i + 3]); x0 = xy[i * 2];
y0 = xy[i * 2 + 1];
x1 = xy[i * 2 + 2];
y1 = xy[i * 2 + 3];
if (y0 == y1 && i != 0 && y0 == xy[i * 2 - 1]) {
// This is a horizontal line,
// that immediately follows another horizontal line
Edge *last_e = &e[n-1];
if (x1 > x0 && x0 > xy[i * 2 - 2]) {
// They are both increasing in x
last_e->xmax = x1;
continue;
} else if (x1 < x0 && x0 < xy[i * 2 - 2]) {
// They are both decreasing in x
last_e->xmin = x1;
continue;
}
}
add_edge(&e[n++], x0, y0, x1, y1);
} }
if (xy[i + i] != xy[0] || xy[i + i + 1] != xy[1]) { if (xy[i * 2] != xy[0] || xy[i * 2 + 1] != xy[1]) {
add_edge(&e[n++], xy[i + i], xy[i + i + 1], xy[0], xy[1]); add_edge(&e[n++], xy[i * 2], xy[i * 2 + 1], xy[0], xy[1]);
} }
draw->polygon(im, n, e, ink, 0); draw->polygon(im, n, e, ink, 0);
free(e); free(e);
@ -764,9 +782,9 @@ ImagingDrawPolygon(Imaging im, int count, int *xy, const void *ink_, int fill, i
} else { } else {
/* Outline */ /* Outline */
for (i = 0; i < count - 1; i++) { for (i = 0; i < count - 1; i++) {
draw->line(im, xy[i + i], xy[i + i + 1], xy[i + i + 2], xy[i + i + 3], ink); draw->line(im, xy[i * 2], xy[i * 2 + 1], xy[i * 2 + 2], xy[i * 2 + 3], ink);
} }
draw->line(im, xy[i + i], xy[i + i + 1], xy[0], xy[1], ink); draw->line(im, xy[i * 2], xy[i * 2 + 1], xy[0], xy[1], ink);
} }
return 0; return 0;