Algorithm::Line::Bresenham - simple pixellated line-drawing algorithm


 use Algorithm::Line::Bresenham qw/line/;
 my @points = line(3,3 => 5,0);
    # returns the list: [3,3], [4,2], [4,1], [5,0]
 my @points = circle(30,30,5);
    # returns the points to draw a circle centered at 30,30, radius 5


Bresenham is one of the canonical line drawing algorithms for pixellated grids. Given a start and an end-point, Bresenham calculates which points on the grid need to be filled to generate the line between them. This module has been extended to include cureves crcles ellipses and thick licnes, variable thickness lines.


 line ($from_x, $from_y => $to_x, $to_y);

Generates a list of all the intermediate points. This is returned as a list of array references. Previous versions used to include a callback parameter as a CODE ref to act on each point in turn. This version omits that for performance reasons


    my @points = circle ($x, $y, $radius)

Returns the points to draw a circle centered on $x,$y with radius $radius


    my @points = ellipse_rect ($x0, $y0, $x1, $y1)

Returns the points to an ellipse bound within a rectangle defined by the two coordinate pairs passed.


    my @points = basic_bezier ($x0, $y0, $x1, $y1, $x2, $y2)

This is not usefull on its own. Iteturns the points to segment of a bezier curve without a gradient sign change. It is a companion to the quad_bexier function that splits a bezier into segments with each gradient direction and these segments are computed in basic_bezier


    my @points = quad_bezier ($x0, $y0, $x1, $y1, $x2, $y2)

Draws a Bezier curve from ($x0,$y0) to ($x2,$y2) using control point ($x1,$y1)


    my @points = polyline ($x0, $y0, $x1, $y1, $x2, $y2)

Draws a polyline between points served as a list of x,y pairs


    my @points = thick_line ($x0, $y0, $x1, $y1,$thickness)

Draws a line thickened using Murphy's modication of Bresenham'salgorithm between two points of x,y pairs. This routine was further enahnced to provide variable thickness lines and uses multiple helper subroutines.


  my @points= varthick_line($x0,$y0,$x1,$y1,$leftFn,$argL,$rightFn,$argR)

Variable thickness lines are implemented as described in ; This allows passing of two subroutine references (so the left side and the right sides of the line can have differently varying thicknesses) along with a user originated parameter. The subroutine reference example is shown below:

   my $leftFn=sub{
      my ($arg,$p,$l)=@_;
      # C<$arg> is passed by calling routine,
      # C<$p> is point on line
      # C<$l> is length of line
          return $p % $arg;


polylines nurbs arc line width fills pattern fills


Patches for the circle algorithm and a float value bug contributed by Richard Clamp, thanks!


osfameron, saiftynet


Artistic (Perl)


Using cpan:

    $ cpan Algorithm::Line::Bresenham

Manual install:

    $ perl Makefile.PL
    $ make
    $ make install

Copyright (c) 2004-2022 saiftynet, osfameron. All rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.