Algorithm-Line-Bresenham
view release on metacpan or search on metacpan
lib/Algorithm/Line/Bresenham.pm view on Meta::CPAN
}
#/* algorithm fails for almost straight line, check error values */
if ($dx >= -$y || $dy <= -$x || $ex <= -$y || $ey >= -$x)
{
return (line ($x0, $y0, $x1, $y1), line ($x1, $y1, $x2, $y2)); #/* simple approximation */
}
$dx -= $xy;
$ex = $dx + $dy;
$dy -= $xy; #/* error of 1.step */
my @points;
while(1)
{ #/* plot curve */
push @points,[$x0, $y0];
$ey = 2 * $ex - $dy; #/* save value for test of y step */
if (2 * $ex >= $dx)
{ #/* x step */
last if ($x0 == $x2);
$x0 += $sx;
$dy -= $xy;
$ex += $dx += $y;
}
if ($ey <= 0)
{ #/* y step */
last if ($y0 == $y2);
$y0 += $sy;
$dx -= $xy;
$ex += $dy += $x;
}
}
return @points;
}
=head2 C<quad_bezier>
my @points = quad_bezier ($x0, $y0, $x1, $y1, $x2, $y2)
Draws a Bezier curve from C<($x0,$y0)> to C<($x2,$y2)> using control
point C<($x1,$y1)>
=cut
sub quad_bezier{ # adapted from http://members.chello.at/easyfilter/bresenham.html
my ($x0, $y0, $x1, $y1, $x2, $y2)=@_;# /* plot any quadratic Bezier curve */
my $x = $x0-$x1;
my $y = $y0-$y1;
my $t = $x0-2*$x1+$x2;
my $r;
my @points;
if ($x*($x2-$x1) > 0) { #/* horizontal cut at P4? */
if ($y*($y2-$y1) > 0){ #/* vertical cut at P6 too? */
if (abs(($y0-2*$y1+$y2)/$t*$x) > abs($y)) { #/* which first? */
$x0 = $x2; $x2 = $x+$x1; $y0 = $y2; $y2 = $y+$y1;# /* swap points */
} #/* now horizontal cut at P4 comes first */
}
$t = ($x0-$x1)/$t;
$r = (1-$t)*((1-$t)*$y0+2.0*$t*$y1)+$t*$t*$y2;# /* By(t=P4) */
$t = ($x0*$x2-$x1*$x1)*$t/($x0-$x1); #/* gradient dP4/dx=0 */
$x = int($t+0.5); $y = int($r+0.5);
$r = ($y1-$y0)*($t-$x0)/($x1-$x0)+$y0; #/* intersect P3 | P0 P1 */
push @points, basic_bezier($x0,$y0, $x,int($r+0.5), $x,$y);
$r = ($y1-$y2)*($t-$x2)/($x1-$x2)+$y2; #/* intersect P4 | P1 P2 */
$x0 = $x1 = $x; $y0 = $y; $y1 = int($r+0.5);# /* P0 = P4, P1 = P8 */
}
if (($y0-$y1)*($y2-$y1) > 0) { #/* vertical cut at P6? */
$t = $y0-2*$y1+$y2; $t = ($y0-$y1)/$t;
$r = (1-$t)*((1-$t)*$x0+2.0*$t*$x1)+$t*$t*$x2; # /* Bx(t=P6) */
$t = ($y0*$y2-$y1*$y1)*$t/($y0-$y1); #/* gradient dP6/dy=0 */
$x = int($r+0.5); $y = int($t+0.5);
$r = ($x1-$x0)*($t-$y0)/($y1-$y0)+$x0; #/* intersect P6 | P0 P1 */
push @points, basic_bezier($x0,$y0, int($r+0.5),$y, $x,$y);
$r = ($x1-$x2)*($t-$y2)/($y1-$y2)+$x2; #/* intersect P7 | P1 P2 */
$x0 = $x; $x1 = int($r+0.5); $y0 = $y1 = $y;# /* P0 = P6, P1 = P7 */
}
push @points, basic_bezier($x0,$y0, $x1,$y1, $x2,$y2); #/* remaining part */
return @points;
}
=head2 C<polyline>
my @points = polyline ($x0, $y0, $x1, $y1, $x2, $y2)
Draws a polyline between points served as a list of x,y pairs
=cut
sub polyline{
my @vertices;
push @vertices,[shift,shift] while (@_>1);
my @points;
foreach my $vertex(0..(@vertices-2)){
push @points,line(@{$vertices[$vertex]},@{$vertices[$vertex+1]});
pop @points if ($vertex < (@vertices-2)); # remove duplicated points
}
return @points;
}
=head2 C<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.
=head2 C<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;
};
=cut
## Variable thickness lines using Murphy's Modification of Bresenham Line Algorithm**
## Codes ported from C in http://kt8216.unixcab.org/murphy/index.html
#* X BASED LINES *
sub x_perpendicular{
my ($x0,$y0,$dx,$dy,$xstep,$ystep,$einit,$w_left,$w_right,$winit)=@_;
( run in 0.989 second using v1.01-cache-2.11-cpan-39bf76dae61 )