Created
February 27, 2014 21:28
-
-
Save skion/9259926 to your computer and use it in GitHub Desktop.
Rough PIL draw.arc() replacement that supports a line width.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def arc(draw, bbox, start, end, fill, width=1, segments=100): | |
""" | |
Hack that looks similar to PIL's draw.arc(), but can specify a line width. | |
""" | |
# radians | |
start *= math.pi / 180 | |
end *= math.pi / 180 | |
# angle step | |
da = (end - start) / segments | |
# shift end points with half a segment angle | |
start -= da / 2 | |
end -= da / 2 | |
# ellips radii | |
rx = (bbox[2] - bbox[0]) / 2 | |
ry = (bbox[3] - bbox[1]) / 2 | |
# box centre | |
cx = bbox[0] + rx | |
cy = bbox[1] + ry | |
# segment length | |
l = (rx+ry) * da / 2.0 | |
for i in range(segments): | |
# angle centre | |
a = start + (i+0.5) * da | |
# x,y centre | |
x = cx + math.cos(a) * rx | |
y = cy + math.sin(a) * ry | |
# derivatives | |
dx = -math.sin(a) * rx / (rx+ry) | |
dy = math.cos(a) * ry / (rx+ry) | |
draw.line([(x-dx*l,y-dy*l), (x+dx*l, y+dy*l)], fill=fill, width=width) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment