in reply to Plotting A Line

What you are looking for is Bresenhams algorithm for drawing a line. I don't know of an implementation of it for Perl, as most drawing in Perl is done on a higher level by using OpenGL or SVG though. The concept of Bresenhams algorithm is relatively easy - you step down the longer distance and keep a counter for when you have to move one column in the other direction. The fun thing is to understand how and why the algorithm works without requiring floating point math.

perl -MHTTP::Daemon -MHTTP::Response -MLWP::Simple -e ' ; # The $d = new HTTP::Daemon and fork and getprint $d->url and exit;#spider ($c = $d->accept())->get_request(); $c->send_response( new #in the HTTP::Response(200,$_,$_,qq(Just another Perl hacker\n))); ' # web

Replies are listed 'Best First'.
Re: Plotting A Line
by Abigail-II (Bishop) on Oct 27, 2003 at 22:11 UTC
    Bresenham is an algorithm for devices that only have 1 bit pixels, on and off. You also get very nasty artifacts. A line from (0, 0) to (8, 2) would look like:
    +-+-+-+-+-+-+-+-+-+ | | | | | | |*|*|*| +-+-+-+-+-+-+-+-+-+ | | | |*|*|*| | | | +-+-+-+-+-+-+-+-+-+ |*|*|*| | | | | | | +-+-+-+-+-+-+-+-+-+

    Now, if all your pixels are either black or white, this isn't too bad. But the human eye is very good in seeing the non-smoothness, the "staircase" sticks out like a sore thumb.

    But if you have a grayscale (or full colour) device available, there are much better techniques. One technique revolves around the observation a line needs thickness to be seen. So, if you want to draw a line from say (0, 0) to (8, 2), assume you have a rectangle of width 1 and length 2 * sqrt (17), running from (0, 0) to (8, 2). This will cover some of the pixels partially. If a pixel is covered for say, 61%, the pixel should get a gray value that's 61% of pure black.

    Now, this still isn't perfect, but it's a step better. There are lots of line drawing algorithms, each of them finding a different balance between simplicity, speed, good results and avoidance of artifacts.

    Abigail

      Adding to what Abigail's written:
      Michael Abrash covered the topic of eye-pleasing lines in detail in Zen of Graphics Programming. This book's quite fascinating, and pretty well written. And although most of the examples were (quite naturally) not in perl, all that's really needed to absorb most of the book's functionality are some basic math skills, typically algebra. You can also purchase a subscription to Byte magazine and get access to a seemingly similar book, where chapter 35 would be of particular interest.