Line


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

The Line class draws a line.

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

There are three different types of line:

  • A SEGMENT is a line drawn from the start point to the end point. It has finite length.
  • A RAY is a line drawn from the start point that passes through the end point then continues on forever. It is sometimes called a half line.
  • A LINE is a line that passes through the start and end points but continues forever in both directions.

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
  • as_line
  • as_ray
  • as_segment
  • as_type

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.

as_line

Specifies a full LINE (see above).

as_line(infinity=None)
Parameter Type Description
infinity number A length that is known to be long enough to go beyond the page boundary.

A line is meant to continue to infinity in both directions. In practice, we simulate this by ensuring that the line goes beyond the edge of the page in both directions.

Since the drawing context can be scaled, we can't use a fixed distance for this. You should supply a suitable size based on the page dimensions at the scaling that will be used when the line is drawn. Usually you can take the page width or height (whichever is larger) and multiply by 2 to give a suitable value.

Avoid using very large values (eg 1000 times the page size), as this can cause drawing artefacts in PyCairo.

as_ray

Specifies a line RAY (see above).

as_ray(infinity=None)
Parameter Type Description
infinity number A length that is known to be long enough to go beyond the page boundary, see description for as_line

as_segment

Specifies a line SEGMENT (see above). This is the default.

as_segment(infinity=None)
Parameter Type Description
infinity number This parameter isn't used for segments.

as_type

Specifies the line type.

as_type(extent_type, infinity=None)
Parameter Type Description
extent_type number RAY, SEGMENT or LINE (defined in generativepy.drawing
infinity number A length that is known to be long enough to go beyond the page boundary, see description for as_line

This function is an alternative to calling as_ray, as_segment, or as_line.

line function

Adds a line segment 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)
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 circle classes close closure cmyk colour comparison operator comprehension context context manager conversion creational pattern data types design pattern device space dictionary drawing duck typing efficiency 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 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 polygon positional parameter print pure function pycairo radial gradient range recipes rectangle recursion reduce rgb rotation scaling sector segment sequence singleton slice slicing sound spirograph sprite square str strategy stream string stroke structural pattern subpath symmetric encryption template text text metrics tinkerbell fractal transform translation transparency tuple turtle unpacking user space vectorisation webserver website while loop zip