Main Page | See live article | Alphabetical index

Bresenham's line algorithm Visual basic code

An example of Bresenham's line algorithm in Visual Basic follows. The "Swap" procedure is not shown.

Private Sub BresLine(InitialX As Long, InitialY As Long, FinalX As Long, FinalY As Long)
   ' Bresenham's line algorithm for Microsoft Visual Basic 6.0
   ' Implementation by Robert Lee  July, 2002 Public Domain

Dim Steep As Boolean Dim DeltaX As Long, DeltaY As Long, Delta As Long Dim StepX As Long, StepY As Long Dim Coord As Long

Steep = False DeltaX = Abs(FinalX - InitialX) If (FinalX - InitialX) > 0 Then StepX = 1 Else StepX = -1 End If DeltaY = Abs(FinalY - InitialY) If (FinalY - InitialY) > 0 Then StepY = 1 Else StepY = -1 End If If DeltaY > DeltaX Then Steep = True Swap InitialX, InitialY Swap DeltaX, DeltaY Swap StepX, StepY End If Delta = (DeltaY * 2) - DeltaX For Coord = 0 To DeltaX - 1 If Steep Then Me.PSet (InitialY, InitialX) Else Me.PSet (InitialX, InitialY) End If While Delta >= 0 InitialY = InitialY + StepY Delta = Delta - (DeltaX * 2) Wend InitialX = InitialX + StepX Delta = Delta + (DeltaY * 2) Next Coord Me.PSet (FinalX, FinalY) End Sub