这篇文章讲的非常详细,这里列出几点注意事项
1、bresenham的起始点是整数点,所有初始的 error值 由 y1 来计算, 即 m - 0.5
2、当error值大于0时,取了上面的点,A点变成了 y+1 上的A点,所以必须减去1 才能做正确判断。具体参照下面内容的公式推导。
贴上AS3代码
package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.display.StageScaleMode;
import flash.events.UncaughtErrorEvent;
import flash.geom.Point;
[SWF(frameRate="60", backgroundColor="0",height="600",width="800")]
public class LineTest extends Sprite
{
private var bmd:BitmapData = new BitmapData(800,600, false, 0);
private var bmp:Bitmap = new Bitmap(bmd);
private var error_factor:Number = 0.5;
public function LineTest()
{
stage.scaleMode = StageScaleMode.NO_SCALE;
addChild(bmp);
draw_help3(new Point(0,300), new Point(800,300), 0xffffff);
draw_help3(new Point(400,0), new Point(400,600), 0xffffff);
draw_help3(new Point(400,300), new Point(500,500),0x00ffff);
draw_help3(new Point(400,300), new Point(500,400),0x00ff00);
draw_help3(new Point(400,300), new Point(300,500),0xffff00);
draw_help3(new Point(400,300), new Point(300,400),0x0000ff);
draw_help3(new Point(100,600), new Point(800,10),0xff0000);
}
private function draw_help3(start:Point, end:Point, color:uint):void
{
bmd.lock();
var dx:int = end.x - start.x;
var dy:int = end.y - start.y;
var x:int = start.x;
var y:int = start.y;
var xInc:int;
var yInc:int;
var i:int;
if(dx >=0)
{
xInc = 1;
}else
{
xInc = -1;
dx = -dx;//总共要走多少步
}
if(dy >= 0)
{
yInc = 1;
}else
{
yInc = -1;
dy = -dy;
}
//比较值的时候,都是按照第一象限来比较,只不过,步进的时候,按照 xInc, yInc 来步进
var k2dx:int = 2*dy;
var error2dx:int = k2dx - 1;
var k2dy:int = 2*dx;
var error2dy:int = k2dy - 1;
if(dx >= dy)//近X轴线
{
for(i = 0; i <= dx ; i++)
{
bmd.setPixel32(x,y, color);
if(error2dx > 0)
{
y += yInc;
error2dx = error2dx + k2dx - 2*dx;
}else
{
error2dx = error2dx + k2dx;
}
x += xInc;
}
}else//近Y轴线
{
for(i = 0; i <= dy ; i++)
{
bmd.setPixel32(x,y, color);
if(error2dy > 0)
{
x += xInc;
error2dy = error2dy + k2dy - 2*dy;
}else
{
error2dy = error2dy + k2dy;
}
y += yInc;
}
}
bmd.unlock();
}
private function draw_help2(start:Point, end:Point):void
{
bmd.lock();
var dx:int = end.x - start.x;
var dy:int = end.y - start.y;
var x:int = start.x;
var y:int = start.y;
var k2dx:int = 2*dy;
var error2dx:int = k2dx - 1;
for(var i:int = 0; i < dx ; i++)
{
bmd.setPixel32(x,y, 0xffff00);
if(error2dx > 0)
{
y++;
error2dx = error2dx + k2dx - 2*dx;
}else
{
error2dx = error2dx + k2dx;
}
x++;
}
bmd.unlock();
}
private function draw_help(start:Point, end:Point):void
{
bmd.lock();
//必须是整点
var dx:int = end.x - start.x;
var dy:int = end.y - start.y;
var x:int = start.x;
var y:int = start.y;
var k:Number = dy/dx;
var error:Number = k - 0.5;//初始值
for(var i:int = 0; i < dx ; i++)
{
bmd.setPixel32(x,y, 0xffffff);
if(error > 0)//取右上的点
{
y++;
error = error + k - 1;
}else
{
error = error + k;
}
x++;
}
bmd.unlock();
}
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
http://wenku.baidu.com/link?url=ova6h39VQ4ilLMIQ51qHpjeuuKu7yLtD1M53NQQc6rUSI9UHymh2r2V8ZZdn6R3bForxZzFf65KO_JCY6flz5A4TU8F4uEHs0N82iCmdQwG
从上面介绍的DDA算法可以看到,由于在循环中涉及实型数据的加减运算,因此直线的生成速度较慢。
在生成直线的算法中,Bresenham算法是最有效的算法之一。Bresenham算法是一种基于误差判别式来生成直线的方法。
一、直线Bresenham算法描述:
二、直线Bresenham算法思想之一:
三、直线Bresenham算法思想之二:
四、直线Bresenham算法实现:
五、直线Bresenham算法完善:
六、直线Bresenham算法演示:
七、直线Bresenham算法特点:
八、直线Bresenham算法程序: