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.
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
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
$x,$y
$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_bexier
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)
($x0,$y0)
($x2,$y2)
($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
polylines nurbs arc line width fills pattern fills
Patches for the circle algorithm and a float value bug contributed by Richard Clamp, thanks!
osfameron, osfameron@cpan.org saiftynet
Artistic (Perl)
Using cpan:
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
To install Algorithm::Line::Bresenham, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Algorithm::Line::Bresenham
CPAN shell
perl -MCPAN -e shell install Algorithm::Line::Bresenham
For more information on module installation, please visit the detailed CPAN module installation guide.