# generativepy.geometry module

Martin McBride, 2020-08-01
Tags geometry
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.

## 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.

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.

## Rectangle

The Rectangle class draws a rectangle. It inherits add, fill, stroke, and fill_stroke from Shape.

• of_corner_size

Plus the rectangle function.

### of_corner_size

Adds a rectangle based on the position and size.

of_corner_size(x, y, width, height)

Parameter Type Description
x number The x position of the top left corner.
y number The y position of the top left corner.
width number The width.
height number The height.

### rectangle function

Adds a rectangle as a new path, without the need to create a Rectangle object in code.

rectangle(ctx, x, y, width, height)

Parameter Type Description
ctx Context The Pycairo Context to draw to
x number The x position of the top left corner.
y number The y position of the top left corner.
width number The width.
height number The height.

## Text

No text object has currently been implmented. You can draw text with the text function.

### text function

Adds a rectangle based on the position and size.

text(ctx, txt, x, y, font=None, size=None, color=None, alignx=LEFT, aligny=BASELINE, flip=False)

Parameter Type Description
ctx Context The Pycairo Context to draw to
txt string The text content.
x number The x position of the text.
y number The y position of the text.
font string Name of the font to use.
size number The size of the text.
color Color The colour of the text.
alignx number The horizontal alignment of the text.
aligny number The vertical alignment of the text.
flip boolean True to flip the text (draw it upside down).

This draws a line of text at position (x, y).

font selects the font face. If it is not supplied, the currently selected font face will be used.

size selects the font size. For western fonts, the font size is approximately equal to the height of the font in user units. This my vary slightly for different font faces, and non-western fonts (for example Chinese fonts). If it is not supplied, the currently selected font size will be used.

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

alignx sets the horizontal alignment to LEFT, CENTER or RIGHT. aligny sets the horizontal vertical to BASELINE, BOTTOM, CENTER or TOP. The alignment constants are definined in the drawing module.

flip draws the text upside down. This mode is primarily intended for use when you also flip user space so that the y-coordinate increases top to bottom (see the setup function). If you flip user space you wil also need to flip text to make it appear the right way up.