吴小林直线算法 吴小林算法是一种绘制抗锯齿直线的算法因为其较高的执行效率被发表在年月的 Computer Graphics和年月的Dr Dobbs Journal上 布雷森漢姆直線演算法绘制直线非常快但它不支持抗锯齿此外它不能处理线段端点的坐标不是整数的情况一个不成熟的反锯齿画线方法需要非常长的时间但吴的算法是相当快的虽然它仍然较布雷森汉姆直线算法慢该算法的基本思想是画两个像素点在岔在直线两边并按照直线相近的颜色着色而线段末端的像素点另外处理如果线段宽度小于一像素将会被作为特殊情况考虑 吴小林的Graphics Gems II一书描述了一个绘制圆的算法作为布雷森汉姆圆绘制算法的替代品 function plotx y c is plot the pixel at x y with brightness c where   c    integer part of x function ipartx is return floorx function roundx is return ipartx    fractional part of x function fpartx is return x  floorx function rfpartx is return   fpartx function drawLinexyxy is boolean steep  absy  y  absx  x if steep then swapx y swapx y end if if x  x then swapx x swapy y end if dx  x  x dy  y  y gradient  dy  dx if dx   then gradient   end if  handle first endpoint xend  roundx yend  y  gradient  xend  x xgap  rfpartx   xpxl  xend  this will be used in the main loop ypxl  ipartyend if steep then plotypxl xpxl rfpartyend  xgap plotypxl xpxl fpartyend  xgap else plotxpxl ypxl  rfpartyend  xgap plotxpxl ypxl fpartyend  xgap end if intery  yend  gradient  first yintersection for the main loop  handle second endpoint xend  roundx yend  y  gradient  xend  x xgap  fpartx   xpxl  xend this will be used in the main loop ypxl  ipartyend if steep then plotypxl  xpxl rfpartyend  xgap plotypxl xpxl fpartyend  xgap else plotxpxl ypxl rfpartyend  xgap plotxpxl ypxl fpartyend  xgap end if  main loop if steep then for x from xpxl   to xpxl   do begin plotipartintery  x rfpartintery plotipartintery x fpartintery intery  intery  gradient end else for x from xpxl   to xpxl   do begin plotx ipartintery rfpartintery plotx ipartintery fpartintery intery  intery  gradient end end if end function 注意如果在程序开始absdx  absdy为 true那么所有的绘图应该做X和Y逆转