| 1 |
from igraph.drawing.utils import calculate_corner_radii |
| 2 |
from igraph.utils import consecutive_pairs |
| 3 |
|
| 4 |
from .base import AbstractCairoDrawer |
| 5 |
|
| 6 |
__all__ = ("CairoPolygonDrawer",) |
| 7 |
|
| 8 |
|
| 9 |
class CairoPolygonDrawer(AbstractCairoDrawer): |
| 10 |
"""Class that is used to draw polygons in Cairo. |
| 11 |
|
| 12 |
The corner points of the polygon can be set by the C{points} |
| 13 |
property of the drawer, or passed at construction time. Most |
| 14 |
drawing methods in this class also have an extra C{points} |
| 15 |
argument that can be used to override the set of points in the |
| 16 |
C{points} property.""" |
| 17 |
|
| 18 |
def __init__(self, context, bbox=(1, 1)): |
| 19 |
"""Constructs a new polygon drawer that draws on the given |
| 20 |
Cairo context. |
| 21 |
|
| 22 |
@param context: the Cairo context to draw on |
| 23 |
@param bbox: ignored, leave it at its default value |
| 24 |
""" |
| 25 |
super().__init__(context, bbox) |
| 26 |
|
| 27 |
def draw_path(self, points, corner_radius=0): |
| 28 |
"""Sets up a Cairo path for the outline of a polygon on the given |
| 29 |
Cairo context. |
| 30 |
|
| 31 |
@param points: the coordinates of the corners of the polygon, |
| 32 |
in clockwise or counter-clockwise order. |
| 33 |
@param corner_radius: if zero, an ordinary polygon will be drawn. |
| 34 |
If positive, the corners of the polygon will be rounded with |
| 35 |
the given radius. |
| 36 |
""" |
| 37 |
self.context.new_path() |
| 38 |
|
| 39 |
if len(points) < 2: |
| 40 |
# Well, a polygon must have at least two corner points |
| 41 |
return |
| 42 |
|
| 43 |
ctx = self.context |
| 44 |
if corner_radius <= 0: |
| 45 |
# No rounded corners, this is simple |
| 46 |
ctx.move_to(*points[-1]) |
| 47 |
for point in points: |
| 48 |
ctx.line_to(*point) |
| 49 |
return |
| 50 |
|
| 51 |
# Rounded corners. First, we will take each side of the |
| 52 |
# polygon and find what the corner radius should be on |
| 53 |
# each corner. If the side is longer than 2r (where r is |
| 54 |
# equal to corner_radius), the radius allowed by that side |
| 55 |
# is r; if the side is shorter, the radius is the length |
| 56 |
# of the side / 2. For each corner, the final corner radius |
| 57 |
# is the smaller of the radii on the two sides adjacent to |
| 58 |
# the corner. |
| 59 |
corner_radii = calculate_corner_radii(points, corner_radius) |
| 60 |
|
| 61 |
# Okay, move to the last corner, adjusted by corner_radii[-1] |
| 62 |
# towards the first corner |
| 63 |
ctx.move_to(*(points[-1].towards(points[0], corner_radii[-1]))) |
| 64 |
# Now, for each point in points, draw a line towards the |
| 65 |
# corner, stopping before it in a distance of corner_radii[idx], |
| 66 |
# then draw the corner |
| 67 |
u = points[-1] |
| 68 |
for idx, (v, w) in enumerate(consecutive_pairs(points, True)): |
| 69 |
radius = corner_radii[idx] |
| 70 |
ctx.line_to(*v.towards(u, radius)) |
| 71 |
aux1 = v.towards(u, radius / 2) |
| 72 |
aux2 = v.towards(w, radius / 2) |
| 73 |
ctx.curve_to( |
| 74 |
aux1.x, aux1.y, aux2.x, aux2.y, *v.towards(w, corner_radii[idx]) |
| 75 |
) |
| 76 |
u = v |
| 77 |
|
| 78 |
def draw(self, points): |
| 79 |
"""Draws the polygon using the current stroke of the Cairo context. |
| 80 |
|
| 81 |
@param points: the coordinates of the corners of the polygon, |
| 82 |
in clockwise or counter-clockwise order. |
| 83 |
""" |
| 84 |
self.draw_path(points) |
| 85 |
self.context.stroke() |