chiburashka has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's minimum standard of quality and will not be displayed.

Replies are listed 'Best First'.
Re: Draw a line
by Abigail-II (Bishop) on Jun 03, 2004 at 13:59 UTC
    print <<'--'; (x1,y1) (x2,y2) *-------------------------* --

    Abigail

Re: Draw a line
by BUU (Prior) on Jun 03, 2004 at 11:29 UTC
    Pick up a pen. Place on surface. Move hand.

    Honestly, what the hell kind of question is this? If you would specify a tool kit, what you've tried and what you're desired results are, maybe we could actually *help* you (then again, considering who it is..). As is, you've posted a worthless question because we have no way of answering, short of blind guessing.

    If you want to ask a question, first do a google search to see if you can find the information on your own. Next try to experiment on your own. Try various methods and see if you can't muddle your way to a solution. Thirdly you may ask an *intelligent* question on perlmonks or some other forum. Intelligent means you've documented what you want, what you've tried and what, if any constraints you have. Then some body could actually reply with a decent response.
Re: Draw a line
by Corion (Patriarch) on Jun 03, 2004 at 10:26 UTC

    Nowadays, most graphics toolkits already supply you with a function line_to or line, which draws the line for you, so I recommend that you read through the documentation of your graphics toolkit and find what the name and the usage of the corresponding function is.

    If your graphics toolkit only has a set_pixel function and no line function, then using Bresenhams algorithm for drawing lines is most likely one of the fast methods to draw lines. Understanding how the algorithm works is also very rewarding, as the algorithm operates without floating point numbers.

    If you are more looking for the general concept of drawing a line between two points, the drawing of a line between two points can be interpreted as finding all points (x,y) on the line passing through (x1,y1);(x2,y2) where x1 <= x < x2.

      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); } }
Re: Draw a line
by borisz (Canon) on Jun 03, 2004 at 10:25 UTC
    With GD you can do $img->line(0,0,150,150, $color);
    Boris
Re: Draw a line
by eserte (Deacon) on Jun 03, 2004 at 10:18 UTC
    Use GD or Imager or ImageMagick or Tk::Canvas or ...
Re: Draw a line
by zentara (Cardinal) on Jun 03, 2004 at 13:00 UTC
    You could do it directly in postscript, and then convert it to whatever graphic format you wanted.
    %! %% Example of simple line drawing /inch {72 mul} def % Define an inch->point conversion function newpath % Start a new path 1 inch 1 inch moveto % Set the current point an inch in and up 2 inch 1 inch lineto % Create an inch long line segment to the righ +t stroke % Draw the line showpage % Paint and eject the page

    I'm not really a human, but I play one on earth. flash japh
      Nice idea!
      There is a CPAN-module which makes it easy to create a postscript (I never used it, but the description says it is simple :-))
Re: Draw a line
by mawe (Hermit) on Jun 03, 2004 at 10:31 UTC
    Hi!
    A little example with Tk:
    use Tk; my ($x1,$y1,$x2,$y2) = (100,100,130,130); my $top = MainWindow->new; my $can = $top->Canvas()->pack; $can->createLine($x1,$y1,$x2,$y2); MainLoop;
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Draw a line
by mutated (Monk) on Jun 03, 2004 at 12:52 UTC
    This should work for you:
    use strict; use esp; esp::make_me_a_magical_line(x1,y1,x2,y2); esp::thank_you;


    daN.