Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved connecting discontiguous corners #8659

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified Tests/images/imagedraw/discontiguous_corners_polygon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion Tests/test_imagedraw.py
Original file line number Diff line number Diff line change
Expand Up @@ -1683,7 +1683,7 @@ def test_discontiguous_corners_polygon() -> None:
BLACK,
)
expected = os.path.join(IMAGES_PATH, "discontiguous_corners_polygon.png")
assert_image_similar_tofile(img, expected, 1)
assert_image_equal_tofile(img, expected)


def test_polygon2() -> None:
Expand Down
58 changes: 26 additions & 32 deletions src/libImaging/Draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -501,55 +501,49 @@ polygon_generic(
// Needed to draw consistent polygons
xx[j] = xx[j - 1];
j++;
} else if (current->dx != 0 && j % 2 == 1 &&
roundf(xx[j - 1]) == xx[j - 1]) {
} else if ((ymin == current->ymin || ymin == current->ymax) &&
current->dx != 0) {
// Connect discontiguous corners
for (k = 0; k < i; k++) {
Edge *other_edge = edge_table[k];
if ((current->dx > 0 && other_edge->dx <= 0) ||
(current->dx < 0 && other_edge->dx >= 0)) {
if ((ymin != other_edge->ymin && ymin != other_edge->ymax) ||
other_edge->dx == 0) {
continue;
}
// Check if the two edges join to make a corner
if (xx[j - 1] ==
(ymin - other_edge->y0) * other_edge->dx + other_edge->x0) {
if (roundf(xx[j - 1]) ==
roundf(
(ymin - other_edge->y0) * other_edge->dx +
other_edge->x0
)) {
// Determine points from the edges on the next row
// Or if this is the last row, check the previous row
int offset = ymin == ymax ? -1 : 1;
int offset = ymin == current->ymax ? -1 : 1;
adjacent_line_x =
(ymin + offset - current->y0) * current->dx +
current->x0;
adjacent_line_x_other_edge =
(ymin + offset - other_edge->y0) * other_edge->dx +
other_edge->x0;
if (ymin == current->ymax) {
if (current->dx > 0) {
xx[k] =
fmax(
if (ymin + offset >= other_edge->ymin &&
ymin + offset <= other_edge->ymax) {
adjacent_line_x_other_edge =
(ymin + offset - other_edge->y0) * other_edge->dx +
other_edge->x0;
if (xx[j - 1] > adjacent_line_x + 1 &&
xx[j - 1] > adjacent_line_x_other_edge + 1) {
xx[j - 1] =
roundf(fmax(
adjacent_line_x, adjacent_line_x_other_edge
) +
)) +
1;
} else {
xx[k] =
fmin(
} else if (xx[j - 1] < adjacent_line_x - 1 &&
xx[j - 1] < adjacent_line_x_other_edge - 1) {
xx[j - 1] =
roundf(fmin(
adjacent_line_x, adjacent_line_x_other_edge
) -
1;
}
} else {
if (current->dx > 0) {
xx[k] = fmin(
adjacent_line_x, adjacent_line_x_other_edge
);
} else {
xx[k] =
fmax(
adjacent_line_x, adjacent_line_x_other_edge
) +
)) -
1;
}
break;
}
break;
}
}
}
Expand Down
Loading