Private Declare Function SetPixelV Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long) As Long
'**************************************
' Name: Draw Line with Bresenham Algorit
' hm
' Description:Draws a line with Bresenha
' m's algorithm.
' By: Kamilche
'
'
' Inputs:The HDC to the window, the star
' t and end locations of the line, and the
' color.
'
' Returns:Draws a line on the desired HD
' C.
'
'Assumes:None
'
'Side Effects:Slow! LineTo is far faster
' .
'This code is copyrighted and has limite
' d warranties.
'Please see http://www.Planet-Source-Cod
' e.com/xq/ASP/txtCodeId.8979/lngWId.1/qx/
' vb/scripts/ShowCode.htm
'for details.
'**************************************
Private Sub DrawBresenhamLine(hdc As Long, x1 As Long, y1 As Long, x2 As Long, y2 As Long, c As Long)
'Man this routine is slow, if anyone has
' a better one I'm listening.
Dim temp As Long
Dim dy As Long
Dim dy_neg As Long
Dim dx_neg As Long
Dim switch_x_y As Long
Dim neg_slope As Long
Dim tempx, tempy As Long
Dim dx As Long
Dim n As Long
Dim m As Double
Dim d As Long
Dim incrH As Long
Dim incrHV As Long
Dim x As Long
Dim y As Long
dy_neg = 1: dx_neg = 1
dx = x2 - x1
If dx = 0 Then
If y1 > y2 Then
For n = y2 To y1
SetPixelV hdc, x1, n, c
Next n
Exit Sub
Else
For n = y1 To y2
SetPixelV hdc, x1, n, c
Next n
Exit Sub
End If
End If
dy = y2 - y1
If dy = 0 Then
If x1 > x2 Then
For n = x2 To x1
SetPixelV hdc, n, y1, c
Next n
Exit Sub
Else
For n = x1 To x2
SetPixelV hdc, n, y1, c
Next n
Exit Sub
End If
End If
m = CDbl(dy / dx)
If m > 1 Or m < -1 Then
temp = x1
x1 = y1
y1 = temp
temp = x2
x2 = y2
y2 = temp
dx = x2 - x1
dy = y2 - y1
m = CDbl(dy / dx)
switch_x_y = 1
End If
If x1 > x2 Then
temp = x1
x1 = x2
x2 = temp
temp = y1
y1 = y2
y2 = temp
dx = x2 - x1
dy = y2 - y1
m = CDbl(dy / dx)
End If
If m < 0 Then
If dy < 0 Then
dy_neg = -1
dx_neg = 1
Else
dy_neg = 1
dx_neg = -1
End If
neg_slope = 1
End If
d = 2 * (dy * dy_neg) - (dx * dx_neg)
incrH = 2 * dy * dy_neg
incrHV = 2 * ((dy * dy_neg) - (dx * dx_neg))
x = x1
y = y1
tempx = x
tempy = y
If switch_x_y = 1 Then
temp = x
x = y
y = temp
End If
SetPixelV hdc, x, y, c
x = tempx
y = tempy
Do While (x < x2)
If d <= 0 Then
x = x + 1
d = d + incrH
Else
d = d + incrHV
x = x + 1
If neg_slope = 0 Then
y = y + 1
Else
y = y - 1
End If
End If
tempx = x
tempy = y
If switch_x_y = 1 Then
temp = x
x = y
y = temp
End If
SetPixelV hdc, x, y, c
x = tempx
y = tempy
Loop
End Sub