Fill and stroke in generativepy


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

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

If you found this article useful, you might be interested in the book Computer Graphics in Python or other books by the same author.

Prev

Popular tags

2d arrays abstract data type alignment and animation arc array arrays behavioural pattern bezier curve built-in function callable object chain circle classes close closure cmyk colour combinations comparison operator comprehension context context manager conversion count creational pattern data types design pattern device space dictionary drawing duck typing efficiency ellipse else encryption enumerate fill filter font font style for loop function function composition function plot functools game development generativepy tutorial generator geometry gif gradient greyscale higher order function hsl html image image processing imagesurface immutable object index inner function input installing iter iterable iterator itertools l system lambda function len line linear gradient linspace list list comprehension logical operator lru_cache magic method mandelbrot mandelbrot set map monad mutability named parameter numeric python numpy object open operator optional parameter or partial application path pattern permutations polygon positional parameter print pure function python standard library radial gradient range recipes rectangle recursion reduce repeat rgb rotation scaling sector segment sequence setup shape singleton slice slicing sound spirograph sprite square str stream string stroke structural pattern subpath symmetric encryption template text text metrics tinkerbell fractal transform translation transparency triangle tuple turtle unpacking user space vectorisation webserver website while loop zip