Why not adopt me?
NAME
Algorithm::Line::Bresenham - simple pixellated line-drawing algorithm
SYNOPSIS
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
DESCRIPTION
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.
Googling for 'Bresenham', and 'line drawing algorithms' gives some good overview. The code here are adapted from various sources, mainly from C code at https://gist.github.com/bert/1085538
FUNCTIONS
line
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
circle
my @points = circle ($x, $y, $radius)
Returns the points to draw a circle centered on $x,$y
with radius $radius
ellipse_rect
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.
basic_bezier
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
quad_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)
polyline
my @points = polyline ($x0, $y0, $x1, $y1, $x2, $y2)
Draws a polyline between points served as a list of x,y pairs
thick_line
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.
varthick_line
my @points= varthick_line($x0,$y0,$x1,$y1,$leftFn,$argL,$rightFn,$argR)
Variable thickness lines are implemented as described in http://kt8216.unixcab.org/murphy/index.html ; 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;
};
TODO and BUGS
polylines nurbs arc line width fills pattern fills
THANKS
Patches for the circle algorithm and a float value bug contributed by Richard Clamp, thanks!
AUTHOR
osfameron, osfameron@cpan.org saiftynet
LICENSE
Artistic (Perl)
INSTALLATION
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.
See http://www.perl.com/perl/misc/Artistic.html