Ejemplo n.º 1
0
    def draw_polygon(ctx: Context, polygon: Polygon):
        if hasattr(polygon.exterior, 'coords'):
            start = True
            ctx.new_path()
            for x, y in polygon.exterior.coords:
                if start:
                    ctx.move_to(x, y)
                else:
                    ctx.line_to(x, y)

                start = False

        for interior in polygon.interiors:
            ctx.new_sub_path()
            start = True
            for x, y in interior.coords:
                if start:
                    ctx.move_to(x, y)
                else:
                    ctx.line_to(x, y)

                start = False
            ctx.close_path()

        ctx.close_path()
Ejemplo n.º 2
0
def draw_rounded_rectangle(ctx: Context, rectangle: Rectangle, radius: float):
    """
    Draw rectangles with rounded (circular arc) corners.
    :param ctx: Cairo render context.
    :param rectangle: Extent of the rectangle.
    :param radius: Radius of the corners.
    """
    from math import pi
    a, c = rectangle.position(Anchor.TOP_LEFT)
    b, d = rectangle.position(Anchor.BOTTOM_RIGHT)
    ctx.arc(a + radius, c + radius, radius, 2 * (pi / 2), 3 * (pi / 2))
    ctx.arc(b - radius, c + radius, radius, 3 * (pi / 2), 4 * (pi / 2))
    ctx.arc(b - radius, d - radius, radius, 0 * (pi / 2), 1 * (pi / 2))  # ;o)
    ctx.arc(a + radius, d - radius, radius, 1 * (pi / 2), 2 * (pi / 2))
    ctx.close_path()
Ejemplo n.º 3
0
    def draw_rect(self, g: Graphics, area: Bounds, radius: float) -> None:
        (x, y, w, h) = area.tuple

        if w == 0 or h == 0:
            return

        degrees = pi / 180.0

        g.new_sub_path()

        g.arc(x + w - radius, y + radius, radius, -90 * degrees, 0)
        g.arc(x + w - radius, y + h - radius, radius, 0, 90 * degrees)
        g.arc(x + radius, y + h - radius, radius, 90 * degrees, 180 * degrees)
        g.arc(x + radius, y + radius, radius, 180 * degrees, 270 * degrees)

        g.close_path()
Ejemplo n.º 4
0
def rounded_rectangle(ctx: cairo.Context, x, y, w, h, rx, ry):
    # https://www.cairographics.org/cookbook/roundedrectangles/
    arc_to_bezier = 0.55228475
    if rx > w - rx:
        rx = w / 2
    if ry > h - ry:
        ry = h / 2

    c1 = arc_to_bezier * rx
    c2 = arc_to_bezier * ry

    ctx.new_path()
    ctx.move_to(x + rx, y)
    ctx.rel_line_to(w - 2 * rx, 0.0)
    ctx.rel_curve_to(c1, 0.0, rx, c2, rx, ry)
    ctx.rel_line_to(0, h - 2 * ry)
    ctx.rel_curve_to(0.0, c2, c1 - rx, ry, -rx, ry)
    ctx.rel_line_to(-w + 2 * rx, 0)
    ctx.rel_curve_to(-c1, 0, -rx, -c2, -rx, -ry)
    ctx.rel_line_to(0, -h + 2 * ry)
    ctx.rel_curve_to(0.0, -c2, rx - c1, -ry, rx, -ry)
    ctx.close_path()