#!/usr/bin/env python3
"""Examples of various ways to draw a line."""
import cairo

from src.ExtendedSurface import ExtendedSurface

# Create the ExtendedSurface object and set the background to white
es = ExtendedSurface(600, 800)
es.set_background()

# Draw a black line from (50, 250) to (200, 50)
es.draw.line(50, 250, 200, 50)

# Draw a blue line 10 pixels thick from (350, 350) to (500, 100)
es.draw.line(350, 350, 500, 100, color=(0, 0, 255), line_width=10)

# Draw a red line 20 pixels thick from (50, 500) to (250, 700) with a rounded
# line cap.
es.draw.line(50,
             500,
             250,
             700,
             line_cap=cairo.LINE_CAP_ROUND,
             color=(255, 0, 0),
             line_width=20)

# Draw an X in the bottom-right quadrant
es.draw.line("center", "center", "right", "bottom")
es.draw.line("center", "bottom", "right", "center")

# Draw gridlines for reference (outline + vertical/horizontal center lines)
Example #2
0
from src.ExtendedSurface import ExtendedSurface


def random_rectangles(surface, num_rectangles):
    """Draw rectangles randomly all over the ExtendedSurface object."""
    for _ in range(num_rectangles):
        width = random.randint(0, surface.get_width() / 2)
        height = random.randint(0, surface.get_height() / 2)
        x = random.randint(0, surface.get_width() - width)
        y = random.randint(0, surface.get_height() - height)
        color = random.choices(range(256), k=4)
        surface.draw.rectangle(x, y, width, height, color=color)


# Create an ExtendedSurface object, sized 600x800 pixels
es = ExtendedSurface(600, 800)

# Set the background to red
es.set_background((255, 0, 0))

# Draw 10 rectangles randomly placed on it
random_rectangles(es, 10)

# Crop it to (400, 500) starting at (50, 50)
es.crop(50, 50, 400, 500)

# Create a second ExtendedSurface object, sized 100x200 pixels
es2 = ExtendedSurface(100, 200)

# Set the background to yellow
es2.set_background((255, 255, 0))