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

Remove spikes when drawing thin pieslices #5460

Merged
merged 2 commits into from May 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 30 additions & 0 deletions Tests/test_imagedraw.py
Expand Up @@ -538,6 +538,36 @@ def test_pieslice_wide():
assert_image_equal_tofile(im, "Tests/images/imagedraw_pieslice_wide.png")


def test_pieslice_no_spikes():
im = Image.new("RGB", (161, 161), "white")
draw = ImageDraw.Draw(im)
cxs = (
[140] * 3
+ list(range(140, 19, -20))
+ [20] * 5
+ list(range(20, 141, 20))
+ [140] * 2
)
cys = (
list(range(80, 141, 20))
+ [140] * 5
+ list(range(140, 19, -20))
+ [20] * 5
+ list(range(20, 80, 20))
)

for cx, cy, angle in zip(cxs, cys, range(0, 360, 15)):
draw.pieslice(
[cx - 100, cy - 100, cx + 100, cy + 100], angle, angle + 1, fill="black"
)
draw.point([cx, cy], fill="red")

im_pre_erase = im.copy()
draw.rectangle([21, 21, 139, 139], fill="white")

assert_image_equal(im, im_pre_erase)


def helper_point(points):
# Arrange
im = Image.new("RGB", (W, H))
Expand Down
16 changes: 16 additions & 0 deletions src/libImaging/Draw.c
Expand Up @@ -1347,6 +1347,22 @@ pie_init(clip_ellipse_state *s, int32_t a, int32_t b, int32_t w, float al, float
s->root->l = lc;
s->root->r = rc;
s->root->type = ar - al < 180 ? CT_AND : CT_OR;

// add one more semiplane to avoid spikes
if (ar - al < 90) {
clip_node *old_root = s->root;
clip_node *spike_clipper = s->nodes + s->node_count++;
s->root = s->nodes + s->node_count++;
s->root->l = old_root;
s->root->r = spike_clipper;
s->root->type = CT_AND;

spike_clipper->l = spike_clipper->r = NULL;
spike_clipper->type = CT_CLIP;
spike_clipper->a = (xl + xr) / 2.0;
spike_clipper->b = (yl + yr) / 2.0;
Comment on lines +1362 to +1363
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's my explanation of what is happening here.
This is taking the average of the two points on the outside of the circle. Going through the center of the circle as well, that gives us a line down the middle of the pieslice.
Then by swapping x and y (normally y would be with a), and multiplying one of them by -1, it gives us a perpendicular line. Because the line also goes through the center of the circle, that cuts off the spike.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, that's one way to put it
line equation Ax+By+C=0 can be also written as rn + C = 0, r(x, y), n(A, B) - i.e. n(A, B) is a normal vector for line

spike_clipper->c = 0;
}
}

void
Expand Down