Ignoring anti-aliasing possibilities...

#! 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!


In reply to Re: Plotting A Line by BrowserUk
in thread Plotting A Line by mdog

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.