Howdy fellow programmers,
Learning to draw lines on a computer screen is part of the basic useful stuff.
Reinventing the wheel being my main focus I looked around to find out how, but most if not all documents on the subject are pure algebra or implemented in languages that may differ from VB.NET.
Therefore I made my own implementation of Bresenham's Algorithm in VB.NET to give a general idea of the concept at hand to those freaked out by algebra lectures.
PS: This is not yet fully optimized speed wise but unless you do very heavy 3D computations the difference with a more optimized version won't be visible.
The code is presented raw, I'll eventually update this post with a properly commented version.
Learning to draw lines on a computer screen is part of the basic useful stuff.
Reinventing the wheel being my main focus I looked around to find out how, but most if not all documents on the subject are pure algebra or implemented in languages that may differ from VB.NET.
Therefore I made my own implementation of Bresenham's Algorithm in VB.NET to give a general idea of the concept at hand to those freaked out by algebra lectures.
PS: This is not yet fully optimized speed wise but unless you do very heavy 3D computations the difference with a more optimized version won't be visible.
The code is presented raw, I'll eventually update this post with a properly commented version.
vb.net Code:
Private Sub DrawLine(ByVal Canvas As Bitmap, ByVal X0 As Integer, ByVal Y0 As Integer, ByVal X1 As Integer, ByVal Y1 As Integer) If X0 > X1 Then Dim Tx As Integer = X0 Dim Ty As Integer = Y0 X0 = X1 : Y0 = Y1 X1 = Tx : Y1 = Ty End If Dim Delta_X As Integer = X1 - X0 Dim Delta_Y As Integer = Y1 - Y0 If Delta_X = 0 And Delta_Y = 0 Then Canvas.SetPixel(X0, Y0, Color.Black) ElseIf Delta_X = 0 Then If Delta_Y > 0 Then For I = Y0 To Y1 Canvas.SetPixel(X0, I, Color.Black) Next ElseIf Delta_Y < 0 Then For I = Y1 To Y0 Canvas.SetPixel(X0, I, Color.Black) Next End If ElseIf Delta_Y = 0 Then If Delta_X > 0 Then For I = X0 To X1 Canvas.SetPixel(I, Y0, Color.Black) Next ElseIf Delta_X < 0 Then For I = X1 To X0 Canvas.SetPixel(I, Y0, Color.Black) Next End If Else If Math.Abs(Delta_Y) > Math.Abs(Delta_X) Then Dim Delta_Err As Single = (Delta_X / Delta_Y) Dim Err As Single = 0 If Delta_Err > 0 Then Dim X_Value As Integer = X0 For I = Y0 To Y1 Canvas.SetPixel(X_Value, I, Color.Black) Err += Delta_Err If Err > 0.5 Then X_Value += 1 Err -= 1 End If Next Else Dim X_Value As Integer = X1 For I = Y1 To Y0 Canvas.SetPixel(X_Value, I, Color.Black) Err += Delta_Err If Err < -0.5 Then X_Value -= 1 Err += 1 End If Next End If Else Dim Delta_Err As Single = Delta_Y / Delta_X Dim Err As Single = 0 If Delta_Err > 0 Then Dim Y_Value As Integer = Y0 For I = X0 To X1 Canvas.SetPixel(I, Y_Value, Color.Black) Err += Delta_Err If Err > 0.5 Then Y_Value += 1 Err -= 1 End If Next Else Dim Y_Value As Integer = Y0 For I = X0 To X1 Canvas.SetPixel(I, Y_Value, Color.Black) Err += Delta_Err If Err < -0.5 Then Y_Value -= 1 Err += 1 End If Next End If End If End If End Sub