For lazy people, here's a perl implementation of this algorithm:
my($x1,$y1, $x2,$y2) = @_; use integer; my($dx, $dy, $incr1, $incr2, $d, $x, $y, $xend, $yend, $xdirfl +ag, $ydirflag); $dx = abs($x2-$x1); $dy = abs($y2-$y1); if ($dy <= $dx) { $d = 2*$dy - $dx; $incr1 = 2*$dy; $incr2 = 2 * ($dy - $dx); if ($x1 > $x2) { $x = $x2; $y = $y2; $ydirflag = (-1); $xend = $x1; } else { $x = $x1; $y = $y1; $ydirflag = 1; $xend = $x2; } set_point($x,$y); if ((($y2 - $y1) * $ydirflag) > 0) { while ($x < $xend) { $x++; if ($d <0) { $d+=$incr1; } else { $y++; $d+=$incr2; } _set_point($x,$y); } } else { while ($x < $xend) { $x++; if ($d <0) { $d+=$incr1; } else { $y--; $d+=$incr2; } set_point($x,$y); } } } else { $d = 2*$dx - $dy; $incr1 = 2 * $dx; $incr2 = 2 * ($dx - $dy); if ($y1 > $y2) { $y = $y2; $x = $x2; $yend = $y1; $xdirflag = (-1); } else { $y = $y1; $x = $x1; $yend = $y2; $xdirflag = 1; } set_point($x,$y); if ((($x2 - $x1) * $xdirflag) > 0) { while ($y < $yend) { $y++; if ($d < 0) { $d+=$incr1; } else { $x++; $d+=$incr2; } set_point($x,$y); } } else { while ($y < $yend) { $y++; if ($d < 0) { $d+=$incr1; } else { $x--; $d+=$incr2; } set_point($x,$y); } }

In reply to Re^2: Draw a line by eserte
in thread Draw a line by chiburashka

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.