# Line

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

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

The Line class draws a line.

There is also a line function that just creates a line as a new path.

## Line class methods

The Line class inherits add, fill, stroke, fill_stroke, path, clip and other methods from Shape.

It has additional methods:

• of_start_end
• of_end

### of_start_end

Creates a line based on the start and end points.

of_start_end(start, end)

Parameter Type Description
start (number, number) A tuple of two numbers, giving the (x, y) position of the start of the line.
end (number, number) A tuple of two numbers, giving the (x, y) position of the end of the line.

### of_end

Creates a line based on the end points only.

of_end(end)

Parameter Type Description
end (number, number) A tuple of two numbers, giving the (x, y) position of the end of the line.

This method creates a line by specifying the end point only. The start point is set to a value of (0, 0).

This is mainly intended for use in composite paths, where lines are joined one after the other. Each line takes its start point as the previous lines end point. See the composite paths tutorial.

## line function

Adds a line as a new path, without the need to create a Line object in code.

line(ctx, start, end)

Parameter Type Description
ctx Context The Pycairo Context to draw to
start (number, number) A tuple of two number, giving the (x, y) position of the start of the line.
end (number, number) A tuple of two number, giving the (x, y) position of the end of the line.

## Example

Here is some example code that draws lines using the class and the utility function. The full code can be found on github.

from generativepy.drawing import make_image, setup
from generativepy.color import Color
from generativepy.geometry import Line, line

'''
Create lines using the geometry module.
'''

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

# The line function is a convenience function that adds a line as a new path.
# You can fill or stroke it as you wish.
line(ctx, (1, 1), (2, 3))
ctx.set_source_rgba(*Color(1, 0, 0))
ctx.set_line_width(0.1)
ctx.stroke()

# Line objects can only be stroked as they do not contain an area.
Line(ctx).of_start_end((3, 1), (4, 4)).stroke(Color('fuchsia'), 0.2)

make_image("/tmp/geometry-lines.png", draw, 500, 500)


Copyright (c) Axlesoft Ltd 2021