Posted By

icebob on 08/02/10


Tagged

antialias


Versions (?)

Antialiased a line


 / Published in: Delphi
 

  1. procedure AALine(x1,y1,x2,y2 : single; color : tcolor; canvas : tcanvas);
  2.  
  3. function CrossFadeColor(FromColor,ToColor : TColor; Rate : Single) : TColor;
  4. var r,g,b : byte;
  5. begin
  6. r:=Round(GetRValue(FromColor)*Rate+GetRValue(ToColor)*(1-Rate));
  7. g:=Round(GetGValue(FromColor)*Rate+GetGValue(ToColor)*(1-Rate));
  8. b:=Round(GetBValue(FromColor)*Rate+GetBValue(ToColor)*(1-Rate));
  9. Result:=RGB(r,g,b);
  10. end;
  11.  
  12. procedure hpixel(x : single; y : integer);
  13. var FadeRate : single;
  14. begin
  15. FadeRate:=x-trunc(x);
  16. with canvas do
  17. begin
  18. pixels[trunc(x),y]:=CrossFadeColor(Color,Pixels[Trunc(x),y],1-FadeRate);
  19. pixels[trunc(x)+1,y]:=CrossFadeColor(Color,Pixels[Trunc(x)+1,y],FadeRate);
  20. end;
  21. end;
  22.  
  23. procedure vpixel(x : integer; y : single);
  24. var FadeRate : single;
  25. begin
  26. FadeRate:=y-trunc(y);
  27. with canvas do
  28. begin
  29. pixels[x,trunc(y)]:=CrossFadeColor(Color,Pixels[x,Trunc(y)],1-FadeRate);
  30. pixels[x,trunc(y)+1]:=CrossFadeColor(Color,Pixels[x,Trunc(y)+1],FadeRate);
  31. end;
  32. end;
  33.  
  34. var i : integer;
  35. ly,lx,currentx,currenty,deltax,deltay,l,skipl : single;
  36. begin
  37. if (x1<>x2) or (y1<>y2) then
  38. begin
  39. currentx:=x1;
  40. currenty:=y1;
  41. lx:=abs(x2-x1);
  42. ly:=abs(y2-y1);
  43.  
  44. if lx>ly then
  45. begin
  46. l:=trunc(lx);
  47. deltay:=(y2-y1)/l;
  48. if x1>x2 then
  49. begin
  50. deltax:=-1;
  51. skipl:=(currentx-trunc(currentx));
  52. end else
  53. begin
  54. deltax:=1;
  55. skipl:=1-(currentx-trunc(currentx));
  56. end;
  57. end else
  58. begin
  59. l:=trunc(ly);
  60. deltax:=(x2-x1)/l;
  61. if y1>y2 then
  62. begin
  63. deltay:=-1;
  64. skipl:=(currenty-trunc(currenty));
  65. end else
  66. begin
  67. deltay:=1;
  68. skipl:=1-(currenty-trunc(currenty));
  69. end;
  70. end;
  71.  
  72. currentx:=currentx+deltax*skipl;
  73. currenty:=currenty+deltay*skipl;{}
  74.  
  75. for i:=1 to trunc(l) do
  76. begin
  77. if lx>ly then vpixel(trunc(currentx),currenty) else hpixel(currentx,trunc(currenty));
  78. currentx:=currentx+deltax;
  79. currenty:=currenty+deltay;
  80. end;
  81. end;
  82. end;

Report this snippet  

You need to login to post a comment.