Xiaolin Wu's line algorithm: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎{{header|Tcl}}: color parsing works better with [scan], allows overall code to be more efficient)
m (→‎{{header|Tcl}}: link to tk library)
Line 119: Line 119:


=={{header|Tcl}}==
=={{header|Tcl}}==
{{libheader|Tk}}
Uses code from [[Basic bitmap storage#Tcl]]
Uses code from [[Basic bitmap storage#Tcl]]
<lang tcl>package require Tcl 8.5
<lang tcl>package require Tcl 8.5

Revision as of 16:46, 15 May 2009

Task
Xiaolin Wu's line algorithm
You are encouraged to solve this task according to the task description, using any language you may know.

Implement the Xiaolin Wu's line algorithm as described in Wikipedia. This algorithm draw antialiased lines. See Bresenham's line algorithm for aliased lines.

C

This implementation follows straightforwardly the pseudocode given on Wikipedia. (Further analysis of the code could give suggestions for improvements).

<lang c>void draw_line_antialias(

       image img,
       unsigned int x0, unsigned int y0,
       unsigned int x1, unsigned int y1,
       color_component r,
       color_component g,
       color_component b );</lang>

<lang c>inline void _dla_changebrightness(rgb_color_p from, rgb_color_p to, float br) {

 if ( br > 1.0 ) br = 1.0;
 /* linear... Maybe something more complex could give better look */
 to->red = br * (float)from->red;
 to->green = br * (float)from->green;
 to->blue = br * (float)from->blue;

}

  1. define plot_(X,Y,D) do{ rgb_color f_; \
 f_.red = r; f_.green = g; f_.blue = b;			\
 _dla_plot(img, (X), (Y), &f_, (D)) ; }while(0)

inline void _dla_plot(image img, int x, int y, rgb_color_p col, float br) {

 rgb_color oc;
 _dla_changebrightness(col, &oc, br);
 put_pixel_clip(img, x, y, oc.red, oc.green, oc.blue);

}

  1. define ipart_(X) ((int)(X))
  2. define round_(X) ((int)(((double)(X))+0.5))
  3. define fpart_(X) (((double)(X))-(double)ipart_(X))
  4. define rfpart_(X) (1.0-fpart_(X))
  1. define swap_(a, b) do{ __typeof__(a) tmp; tmp = a; a = b; b = tmp; }while(0)

void draw_line_antialias(

 image img,
 unsigned int x1, unsigned int y1,
 unsigned int x2, unsigned int y2,
 color_component r,
 color_component g,
 color_component b )

{

 double dx = (double)x2 - (double)x1;
 double dy = (double)y2 - (double)y1;
 if ( fabs(dx) > fabs(dy) ) {
   if ( x2 < x1 ) {
     swap_(x1, x2);
     swap_(y1, y2);
   }
   double gradient = dy / dx;
   double xend = round_(x1);
   double yend = y1 + gradient*(xend - x1);
   double xgap = rfpart_(x1 + 0.5);
   int xpxl1 = xend;
   int ypxl1 = ipart_(yend);
   plot_(xpxl1, ypxl1, rfpart_(yend)*xgap);
   plot_(xpxl1, ypxl1+1, fpart_(yend)*xgap);
   double intery = yend + gradient;
   xend = round_(x2);
   yend = y2 + gradient*(xend - x2);
   xgap = fpart_(x2+0.5);
   int xpxl2 = xend;
   int ypxl2 = ipart_(yend);
   plot_(xpxl2, ypxl2, rfpart_(yend) * xgap);
   plot_(xpxl2, ypxl2 + 1, fpart_(yend) * xgap);
   int x;
   for(x=xpxl1+1; x <= (xpxl2-1); x++) {
     plot_(x, ipart_(intery), rfpart_(intery));
     plot_(x, ipart_(intery) + 1, fpart_(intery));
     intery += gradient;
   }
 } else {
   if ( y2 < y1 ) {
     swap_(x1, x2);
     swap_(y1, y2);
   }
   double gradient = dx / dy;
   double yend = round_(y1);
   double xend = x1 + gradient*(yend - y1);
   double ygap = rfpart_(y1 + 0.5);
   int ypxl1 = yend;
   int xpxl1 = ipart_(xend);
   plot_(xpxl1, ypxl1, rfpart_(xend)*ygap);
   plot_(xpxl1, ypxl1+1, fpart_(xend)*ygap);
   double interx = xend + gradient;
   yend = round_(y2);
   xend = x2 + gradient*(yend - y2);
   ygap = fpart_(y2+0.5);
   int ypxl2 = yend;
   int xpxl2 = ipart_(xend);
   plot_(xpxl2, ypxl2, rfpart_(xend) * ygap);
   plot_(xpxl2, ypxl2 + 1, fpart_(xend) * ygap);
   int y;
   for(y=ypxl1+1; y <= (ypxl2-1); y++) {
     plot_(ipart_(interx), y, rfpart_(interx));
     plot_(ipart_(interx) + 1, y, fpart_(interx));
     interx += gradient;
   }
 }

}

  1. undef swap_
  2. undef plot_
  3. undef ipart_
  4. undef fpart_
  5. undef round_
  6. undef rfpart_</lang>

Tcl

Library: Tk

Uses code from Basic bitmap storage#Tcl <lang tcl>package require Tcl 8.5 package require Tk

proc ::tcl::mathfunc::ipart x {expr {int($x)}} proc ::tcl::mathfunc::fpart x {expr {$x - int($x)}} proc ::tcl::mathfunc::rfpart x {expr {1.0 - fpart($x)}}

proc drawAntialiasedLine {image colour p1 p2} {

   lassign $p1 x1 y1
   lassign $p2 x2 y2
   set steep [expr {abs($y2 - $y1) > abs($x2 - $x1)}]
   if {$steep} {
       lassign [list $x1 $y1] y1 x1
       lassign [list $x2 $y2] y2 x2
   }
   if {$x1 > $x2} {
       lassign [list $x1 $x2] x2 x1
       lassign [list $y1 $y2] y2 y1
   }
   set deltax [expr {$x2 - $x1}]
   set deltay [expr {abs($y2 - $y1)}]
   set gradient [expr {1.0 * $deltay / $deltax}]
   
   # handle the first endpoint
   set xend [expr {round($x1)}]
   set yend [expr {$y1 + $gradient * ($xend - $x1)}]
   set xgap [expr {rfpart($x1 + 0.5)}]
   set xpxl1 $xend
   set ypxl1 [expr {ipart($yend)}]
   plot $image $colour $steep $xpxl1 $ypxl1 [expr {rfpart($yend)*$xgap}]
   plot $image $colour $steep $xpxl1 [expr {$ypxl1+1}] [expr {fpart($yend)*$xgap}]
   set itery [expr {$yend + $gradient}]
   # handle the second endpoint
   set xend [expr {round($x2)}]
   set yend [expr {$y2 + $gradient * ($xend - $x2)}]
   set xgap [expr {rfpart($x2 + 0.5)}]
   set xpxl2 $xend
   set ypxl2 [expr {ipart($yend)}]
   plot $image $colour $steep $xpxl2 $ypxl2 [expr {rfpart($yend)*$xgap}]
   plot $image $colour $steep $xpxl2 [expr {$ypxl2+1}] [expr {fpart($yend)*$xgap}]
   for {set x [expr {$xpxl1 + 1}]} {$x < $xpxl2} {incr x} {
       plot $image $colour $steep $x [expr {ipart($itery)}] [expr {rfpart($itery)}]
       plot $image $colour $steep $x [expr {ipart($itery) + 1}] [expr {fpart($itery)}]
       set itery [expr {$itery + $gradient}]
   }

}

proc plot {image colour steep x y c} {

   set point [expr {$steep ? [list $y $x] : [list $x $y]}]
   set newColour [antialias $colour [getPixel $image $point] $c]
   setPixel $image $newColour $point

}

proc antialias {newColour oldColour c} {

   # get the new colour r,g,b
   if {[scan $newColour "#%2x%2x%2x%c" nr ng gb -] != 3} {
       scan [colour2rgb $newColour] "#%2x%2x%2x" nr ng nb
   }
   # get the current colour r,g,b
   scan $oldColour "#%2x%2x%2x" cr cg cb
   
   # blend the colours in the ratio defined by "c"
   set blend "#"
   foreach new [list $nr $ng $nb] curr [list $cr $cg $cb] {
       append blend [format {%02x} [expr {round($new*$c + $curr*(1.0-$c))}]]
   }
   return $blend

}

proc colour2rgb {color_name} {

   set colour "#"
   foreach part [winfo rgb . $color_name] {
       append colour [format %02x [expr {$part >> 8}]]
   }
   return $colour

}

set img [newImage 500 500] fill $img blue for {set a 10} {$a < 500} {incr a 60} {

   drawAntialiasedLine $img yellow {10 10} [list 490 $a]
   drawAntialiasedLine $img yellow {10 10} [list $a 490]

} toplevel .wu label .wu.l -image $img pack .wu.l</lang>