Important alert: (current site time 7/16/2013 5:20:51 AM EDT)
 

VB icon

anti-aliased line

Email
Submitted on: 3/1/2006 2:55:30 PM
By: peter bone  
Level: Intermediate
User Rating: By 1 Users
Compatibility: Delphi 7, Delphi 6, Delphi 5
Views: 15131
author picture
(About the author)
 
     draw an anti-aliased line using the Wu algorithm

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
!**************************************
! Name: anti-aliased line
! Description:draw an anti-aliased line using the Wu algorithm
! By: peter bone
!
! Inputs:A bitmap to draw the line on
End point coordinates of the line
Color of line
!
! Returns:the bitmap with an anti-aliased line drawn on it
!
!This code is copyrighted and has! limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=1425&lngWId=7!for details.!**************************************

type
 TRGB = record
r : byte;
g : byte;
b : byte;
 end;
 TRGBArray = array[0..32767] of TRGB;
 pRGBArray = ^TRGBArray;
 TScanlines = array of pRGBArray;
 TPointFloat = record
X : Real;
Y : Real;
 end;
Var
 Scanlines : TScanlines;
// create the scanlines array before calling wuline
Bmp := TBitmap.Create;
Bmp.PixelFormat := pf24bit;
Bmp.Width := 100; 
Bmp.Height := 100;
for y := 0 to 99 do Scanlines[y] := Bmp.Scanline[y];
// anti-aliased line. uses fixed point arithmetic
procedure T3DWorld.WuLine(x1, y1, x2, y2, W, H : integer ; R, G, B : byte);
var
 deltax, deltay, X, Y, start, finish : integer;
 LM, LR : integer;
 dxi, dyi, dydxi : integer;
begin
 deltax := abs(x2 - x1); // Calculate deltax and deltay for initialisation
 deltay := abs(y2 - y1);
 if (deltax = 0) or (deltay = 0) then begin
if SuperSample > 0 then begin
 SuperBmp.Canvas.Pen.Color := (B shl 16) + (G shl 8) + R;
 SuperBmp.Canvas.MoveTo(x1, y1);
 SuperBmp.Canvas.LineTo(x2, y2);
end else begin
 OffScrBmp.Canvas.Pen.Color := (B shl 16) + (G shl 8) + R;
 OffScrBmp.Canvas.MoveTo(x1, y1);
 OffScrBmp.Canvas.LineTo(x2, y2);
end;
exit;
 end;
 if deltax > deltay then begin // horizontal or vertical
if y2 > y1 then // determine rise and run
 dydxi := -deltay shl 16 div deltax
else
 dydxi := deltay shl 16 div deltax;
if x2 < x1 then begin
 start := x2; // right to left
 finish := x1;
 dyi := y2 shl 16;
end else begin
 start := x1; // left to right
 finish := x2;
 dyi := y1 shl 16;
 dydxi := -dydxi; // inverse slope
end;
if finish >= W then finish := W - 1;
for X := start to finish do begin
 Y := dyi shr 16;
 if (X < 0) or (Y < 0) or (Y > H-2) then begin
Inc(dyi, dydxi);
Continue;
 end;
 LM := dyi - Y shl 16; // fractional part of dyi - in fixed-point
 LR := 65536 - LM;
 Scanlines[Y][X].B := (B*LR + Scanlines[Y][X].B*LM) shr 16;
 Scanlines[Y][X].G := (G*LR + Scanlines[Y][X].G*LM) shr 16;
 Scanlines[Y][X].R := (R*LR + Scanlines[Y][X].R*LM) shr 16;
 Inc(Y);
 Scanlines[Y][X].B := (B*LM + Scanlines[Y][X].B*LR) shr 16;
 Scanlines[Y][X].G := (G*LM + Scanlines[Y][X].G*LR) shr 16;
 Scanlines[Y][X].R := (R*LM + Scanlines[Y][X].R*LR) shr 16;
 Inc(dyi, dydxi); // next point
end;
 end else begin
if x2 > x1 then // determine rise and run
 dydxi := -deltax shl 16 div deltay
else
 dydxi := deltax shl 16 div deltay;
if y2 < y1 then begin
 start := y2; // right to left
 finish := y1;
 dxi := x2 shl 16;
end else begin
 start := y1; // left to right
 finish := y2;
 dxi := x1 shl 16;
 dydxi := -dydxi; // inverse slope
end;
if finish >= H then finish := H - 1;
for Y := start to finish do begin
 X := dxi shr 16;
 if (Y < 0) or (X < 0) or (X > W-2) then begin
Inc(dxi, dydxi);
Continue;
 end;
 LM := dxi - X shl 16;
 LR := 65536 - LM;
 Scanlines[Y][X].B := (B*LR + Scanlines[Y][X].B*LM) shr 16;
 Scanlines[Y][X].G := (G*LR + Scanlines[Y][X].G*LM) shr 16;
 Scanlines[Y][X].R := (R*LR + Scanlines[Y][X].R*LM) shr 16;
 Inc(X);
 Scanlines[Y][X].B := (B*LM + Scanlines[Y][X].B*LR) shr 16;
 Scanlines[Y][X].G := (G*LM + Scanlines[Y][X].G*LR) shr 16;
 Scanlines[Y][X].R := (R*LM + Scanlines[Y][X].R*LR) shr 16;
 Inc(dxi, dydxi); // next point
end;
 end;
end;


Other 20 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
5/26/2006 10:03:08 PMdherry

good !!!
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.