求助各位客官,DDA画直线程序?
请哪位大神给我一个DDA画直线程序,不胜感激!!!
百度了下,原来dda就是步进方法画函数下一点,由于在电脑中像素点是以+1或-1步进的,所以只要判断函数结果和当前结果差的绝对值是否大于1,大于则y=y+dy,否则x=x+dx,具体画直线的程序如下:
Private Type PointApi
x As Long
y As Long
End Type
Private Function GetLineNextPoint(startP As PointApi, endP As PointApi, curP As PointApi) As PointApi
'已知直线的起点坐标startP,终点坐标endP,当前点坐标curP,计算下一点坐标,未做curP坐标不再线段上的判断
Dim k As Single, b As Single, x As Long, y As Long
'k用于存储斜率,b是坐标调整(直线方程为y=kx+b)
Dim dx As Single, dy As Single '用于存储步进方向(DDA中所说的微分)
dx = endP.x - startP.x
dy = endP.y - startP.y
x = curP.x
y = curP.y
If dx <> 0 Then
'有斜率的情况处理(斜率无穷大则另行处理)
k = dy / dx '计算斜率
b = startP.y - k * startP.x '计算纵坐标调整值
dx = dx / Abs(dx)
If dy <> 0 Then
dy = dy / Abs(dy)
Else
dy = 0
End If
'上述代码获取x、y坐标的步进方向(+1则坐标点前进,-1则坐标点后退)
If Abs(y - (k * (x + dx) + b)) < 1 Then
GetLineNextPoint.x = x + dx
GetLineNextPoint.y = y
Else
GetLineNextPoint.x = x
GetLineNextPoint.y = y + dy
End If
'上述判断代码得到下一点的坐标值
Else
'斜率为无穷大的情况,则下一点的坐标为y坐标加dy
If dy <> 0 Then
dy = dy / Abs(dy)
Else
dy = 0
End If
GetLineNextPoint.x = x
GetLineNextPoint.y = y + dy
End If
End Function
[ 本帖最后由 lowxiong 于 2013-3-11 16:27 编辑 ]