Shapes

Martin McBride, 2020-08-18
Tags geometry shape
Categories generativepy generative art

The geometry module provides classes for drawing shapes. While you can draw any shape you wish using the Pycairo API, the geometry classes make it easier and more readable.

For each shape there is typically:

• A utility function. This simply creates the shape and adds it to the path.
• A shape class. This allows you to create a shape with more control. In most cases there will be different ways to create the shape, and you can choose the most convenient option. Once the shape is created, you can fill it, stroke it, fill and stroke it, or just add it to the current path.

Shape classes use fluent notation to make the code readable.

All shapes inherit from the Shape class described here. The available shapes are all defined in the geometry module, but they are documented in separate pages:

There are also some line decorators that can be used to annotate lines:

Example - rectangle

To create a rectangle using the utility function you can do this (assuming we have a valid ctx, for example inside a draw function:

rectangle(ctx, 1, 2, 4, 3)
ctx.set_source_rgba(*Color(1, 0, 0))
ctx.fill()


The rectangle function adds a rectangle to the path with its corner at (1, 2), and with width 4, height 3.

We then set the colour to red and fill the rectangle.

We can do a similar thing with a Rectangle object like this:

Rectangle(ctx).of_corner_size(1, 2, 4, 3).stroke(Color(0, .5, 0), 0.1)


This time we define the same rectangle, but instead of filling it we stroke it with a green line of thickness 0.1 units.

Class contructors

All classes have the same constructor. The constructor, of course, takes the name of the shape.

Shape(ctx)
Rectangle(ctx)
# etc...

Parameter Type Description
ctx Context The Pycairo Context to draw to

Creates a shape object with ctx as its target drawing context.

Shape class

The Shape class is an abstract base class (you cannot create a Shape object). It adds several methods taht are available to all the concrete shape classes:

• add adds the shape to the context.
• fill fills the shape.
• stroke strokes the shape.
• fill_stroke fills and strokes the shape.

Adds the shape to the context but does not draw it. The shape is added as a new path.

add()


No parameters

fill

Adds the shape to the context and fills it.

fill(color=None)

Parameter Type Description
color generativepy.Color The fill colour.

If color is supplied, the shape will be filled with that colour.

If no value is supplied, the shape will be filled with whatever paint source is current for the context.

stroke

Adds the shape to the context and strokes it.

stroke(color=None, line_width=1)

Parameter Type Description
color generativepy.Color The stroke colour.
line_width number The line width.

If color is supplied:

• The shape will be stroked with that colour.
• The line width will be set to line_width (that defaults to 1).

If no color value is supplied, the shape will be stroked with whatever paint source is current for the context, using the line_width specified by the context. In other words, if no color is supplied, the line_width is ignored.

fill_stroke

Adds the shape to the context and fills then strokes it.

fill_stroke(fill_color, stroke_colour, line_width=1)

Parameter Type Description
fill_color generativepy.Color The fill colour.
stroke_color generativepy.Color The stroke colour.
line_width number The line width.

For fill_stroke, the two colour parameters fill_color and stroke_color are not optional. You must supply both colours, which means that the shape will be filled and stroked with flat colours.

To fill and stroke a shape with alternative sources (such as patterns or gradients) you should add the shape, then stroke and fill it with native Context calls.