#! perl -slw
use strict;
sub lpoints {
my( $x1, $y1, $x2, $y2 ) = @_;
# Switch ends in need be
( $x1, $x2 ) = ( $x2, $x1 ) if $x2 < $x1;
( $y1, $y2 ) = ( $y2, $y1 ) if $y2 < $y1;
# Calculate deltas
my( $dx, $dy ) = ( $x2 - $x1, $y2 - $y1 );
#return the point if the first is equal to the second
return [$x1, $y1] unless $dx or $dy;
# calculate the gradient. Account for vertical and horizontal line
+s
my $m = $dx != 0 ? $dy / $dx : $dx / $dy;
# if dx != 0 then do it X-wise
if( $dx ) {
map {
my $y = int $y1;
$y1 += $m;
[ $_, $y ]
} $x1 .. $x2;
}
else {
map {
my $x = int $x1;
$x1 += $m;
[ $x, $_ ]
} $y1 .. $y2;
}
}
# Test 9 possibilities.
for my $Y ( -10, 0, +10 ) {
for my $X ( -10, 0, +10 ) {
printf "\n[0,0] -> [% 3d,% 3d] :: ", $X, $Y;
printf "[$_->[0],$_->[1]], " for lpoints 0, 0, $X, $Y;
}
}
__END__
P:\test>test3
[0,0] -> [-10,-10] :: [-10,-10], [-9,-9], [-8,-8], [-7,-7], [-6,-6],
[-5,-5], [-4,-4], [-3,-3], [-2,-2], [-1,-1], [0,
+0],
[0,0] -> [ 0,-10] :: [0,-10], [0,-9], [0,-8], [0,-7], [0,-6], [0,-5],
+
[0,-4], [0,-3], [0,-2], [0,-1], [0,0],
[0,0] -> [ 10,-10] :: [0,-10], [1,-9], [2,-8], [3,-7], [4,-6], [5,-5],
+
[6,-4], [7,-3], [8,-2], [9,-1], [10,0],
[0,0] -> [-10, 0] :: [-10,0], [-9,0], [-8,0], [-7,0], [-6,0], [-5,0],
+
[-4,0], [-3,0], [-2,0], [-1,0], [0,0],
[0,0] -> [ 0, 0] :: [0,0],
[0,0] -> [ 10, 0] :: [0,0], [1,0], [2,0], [3,0], [4,0], [5,0], [6,0],
+
[7,0], [8,0], [9,0], [10,0],
[0,0] -> [-10, 10] :: [-10,0], [-9,1], [-8,2], [-7,3], [-6,4], [-5,5],
+
[-4,6], [-3,7], [-2,8], [-1,9], [0,10],
[0,0] -> [ 0, 10] :: [0,0], [0,1], [0,2], [0,3], [0,4], [0,5],
[0,6], [0,7], [0,8], [0,9], [0,10],
[0,0] -> [ 10, 10] :: [0,0], [1,1], [2,2], [3,3], [4,4], [5,5],
[6,6], [7,7], [8,8], [9,9], [10,10],
Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
Hooray!
|