# Fill and stroke in generativepy

Martin McBride, 2020-10-04
Tags generativepy tutorial fill stroke rectangle
Categories generativepy generativepy tutorial

generativepy has moved to the pythoninformer.com website, and this page might not be up to date. Please visit the new location.

This tutorial covers the basics of filling and stroking shapes in generativepy.

## Fill and stroke example code

Here is a sample Python program for creating various filled and outlined rectangles. The code is explained later in this article:

from generativepy.drawing import make_image, setup
from generativepy.color import Color
from generativepy.geometry import Rectangle, Circle

def draw(ctx, width, height, frame_no, frame_count):
setup(ctx, width, height, width=6.5, background=Color(0.8))

Rectangle(ctx).of_corner_size((0.5, 0.5), 1, 3)\
.fill(Color('magenta'))

Rectangle(ctx).of_corner_size((2, 0.5), 1, 3)\
.stroke(Color('blue'), .1)

Rectangle(ctx).of_corner_size((3.5, 0.5), 1, 3)\
.stroke(Color('black'), .2)\
.fill(Color('yellow'))

Rectangle(ctx).of_corner_size((5, 0.5), 1, 3)\
.fill(Color('yellow'))\
.stroke(Color('black'), .2)

make_image("fill-stroke.png", draw, 650, 400)


Here is the resulting image:

## Filling a rectangle

This section of the code draws a filled rectangle:

    Rectangle(ctx).of_corner_size((0.5, 0.5), 1, 3)\
.fill(Color('magenta'))


In the main code, we have set the image size to 650 pixels, and set the user width to 6.5 in the setup function, This means that 1 unit is user spaces is equivalent to 100 pixels.

To fill the rectangle we:

• Call Rectangle(ctx) to start the construction of the rectangle.
• Call of_corner_size to specify a rectangle of 1 by 3 units, with its top left corner at position (0.5, 0.5).
• Call fill to fill the rectangle with magenta.

## Stroking a rectangle

Stroking a rectangle is similar to filling a rectangle, but we use the stroke function instead of fill:

    Rectangle(ctx).of_corner_size((2, 0.5), 1, 3)\
.stroke(Color('blue'), .1)


We create a rectangle in the same way (but in a slightly different position, to the side of the previous rectangle).

The stroke function takes a colour (blue in this case) and also a parameter that indicates the thickness of 0.1 for the outline. This is measured in user units, and as we saw above 0.1 units is 10 pixels in this example.

## Filling and stroking a rectangle

We can apply fill and stroke to the same shape, like this:

    Rectangle(ctx).of_corner_size((3.5, 0.5), 1, 3)\
.stroke(Color('black'), .2)\
.fill(Color('yellow'))

Rectangle(ctx).of_corner_size((5, 0.5), 1, 3)\
.fill(Color('yellow'))\
.stroke(Color('black'), .2)


As the code shows, we can apply stroke and fill in either order, with slightly different effects.

### Fill and stroke order

This image explains the how fill and stroke work when applied to a rectangle (and in fact to any other shape):

• Area shows the base rectangle we are using.
• Fill shows what happens when we fill the rectangle in green (with no stroke). The green fills the entire dotted area.
• Stroke shows the rectangle being stroked, with no fill. The stroke follows the outline of the rectangle. The stroke is centered on the rectangle boundary, so it is half inside and half outside the boundary, so the overall marked area is bigger than the base rectangle.
• Fill then stroke first fills the rectangle, then strokes it. The stroke covers part of the filled area, so the green area is smaller than the base rectangle.
• Stroke then fill first strokes the rectangle, then fills it. The fill covers part of the stroked area, The green area is the exact size of the base rectangle, and the stroke appears to be half the requested width (because it is half covered by the fill).

There is no right or wrong way to do it, it depends on the effect you wish to achieve.