Skip to content

Commit e8a9b56

Browse files
authored
Improved connecting discontiguous corners (#8659)
1 parent 03dc994 commit e8a9b56

File tree

3 files changed

+27
-33
lines changed

3 files changed

+27
-33
lines changed
Loading

Tests/test_imagedraw.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1704,7 +1704,7 @@ def test_discontiguous_corners_polygon() -> None:
17041704
BLACK,
17051705
)
17061706
expected = os.path.join(IMAGES_PATH, "discontiguous_corners_polygon.png")
1707-
assert_image_similar_tofile(img, expected, 1)
1707+
assert_image_equal_tofile(img, expected)
17081708

17091709

17101710
def test_polygon2() -> None:

src/libImaging/Draw.c

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -501,55 +501,49 @@ polygon_generic(
501501
// Needed to draw consistent polygons
502502
xx[j] = xx[j - 1];
503503
j++;
504-
} else if (current->dx != 0 && j % 2 == 1 &&
505-
roundf(xx[j - 1]) == xx[j - 1]) {
504+
} else if ((ymin == current->ymin || ymin == current->ymax) &&
505+
current->dx != 0) {
506506
// Connect discontiguous corners
507507
for (k = 0; k < i; k++) {
508508
Edge *other_edge = edge_table[k];
509-
if ((current->dx > 0 && other_edge->dx <= 0) ||
510-
(current->dx < 0 && other_edge->dx >= 0)) {
509+
if ((ymin != other_edge->ymin && ymin != other_edge->ymax) ||
510+
other_edge->dx == 0) {
511511
continue;
512512
}
513513
// Check if the two edges join to make a corner
514-
if (xx[j - 1] ==
515-
(ymin - other_edge->y0) * other_edge->dx + other_edge->x0) {
514+
if (roundf(xx[j - 1]) ==
515+
roundf(
516+
(ymin - other_edge->y0) * other_edge->dx +
517+
other_edge->x0
518+
)) {
516519
// Determine points from the edges on the next row
517520
// Or if this is the last row, check the previous row
518-
int offset = ymin == ymax ? -1 : 1;
521+
int offset = ymin == current->ymax ? -1 : 1;
519522
adjacent_line_x =
520523
(ymin + offset - current->y0) * current->dx +
521524
current->x0;
522-
adjacent_line_x_other_edge =
523-
(ymin + offset - other_edge->y0) * other_edge->dx +
524-
other_edge->x0;
525-
if (ymin == current->ymax) {
526-
if (current->dx > 0) {
527-
xx[k] =
528-
fmax(
525+
if (ymin + offset >= other_edge->ymin &&
526+
ymin + offset <= other_edge->ymax) {
527+
adjacent_line_x_other_edge =
528+
(ymin + offset - other_edge->y0) * other_edge->dx +
529+
other_edge->x0;
530+
if (xx[j - 1] > adjacent_line_x + 1 &&
531+
xx[j - 1] > adjacent_line_x_other_edge + 1) {
532+
xx[j - 1] =
533+
roundf(fmax(
529534
adjacent_line_x, adjacent_line_x_other_edge
530-
) +
535+
)) +
531536
1;
532-
} else {
533-
xx[k] =
534-
fmin(
537+
} else if (xx[j - 1] < adjacent_line_x - 1 &&
538+
xx[j - 1] < adjacent_line_x_other_edge - 1) {
539+
xx[j - 1] =
540+
roundf(fmin(
535541
adjacent_line_x, adjacent_line_x_other_edge
536-
) -
537-
1;
538-
}
539-
} else {
540-
if (current->dx > 0) {
541-
xx[k] = fmin(
542-
adjacent_line_x, adjacent_line_x_other_edge
543-
);
544-
} else {
545-
xx[k] =
546-
fmax(
547-
adjacent_line_x, adjacent_line_x_other_edge
548-
) +
542+
)) -
549543
1;
550544
}
545+
break;
551546
}
552-
break;
553547
}
554548
}
555549
}

0 commit comments

Comments
 (0)