Results 1 to 1 of 1

Thread: VB.NET - Drawing Lines From Scratch (Bresenham)

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2018
    Posts
    61

    VB.NET - Drawing Lines From Scratch (Bresenham)

    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.

    vb.net Code:
    1. Private Sub DrawLine(ByVal Canvas As Bitmap, ByVal X0 As Integer, ByVal Y0 As Integer, ByVal X1 As Integer, ByVal Y1 As Integer)
    2.  
    3.         If X0 > X1 Then
    4.             Dim Tx As Integer = X0
    5.             Dim Ty As Integer = Y0
    6.             X0 = X1 : Y0 = Y1
    7.             X1 = Tx : Y1 = Ty
    8.         End If
    9.  
    10.         Dim Delta_X As Integer = X1 - X0
    11.         Dim Delta_Y As Integer = Y1 - Y0
    12.  
    13.         If Delta_X = 0 And Delta_Y = 0 Then
    14.             Canvas.SetPixel(X0, Y0, Color.Black)
    15.         ElseIf Delta_X = 0 Then
    16.             If Delta_Y > 0 Then
    17.                 For I = Y0 To Y1
    18.                     Canvas.SetPixel(X0, I, Color.Black)
    19.                 Next
    20.             ElseIf Delta_Y < 0 Then
    21.                 For I = Y1 To Y0
    22.                     Canvas.SetPixel(X0, I, Color.Black)
    23.                 Next
    24.             End If
    25.         ElseIf Delta_Y = 0 Then
    26.             If Delta_X > 0 Then
    27.                 For I = X0 To X1
    28.                     Canvas.SetPixel(I, Y0, Color.Black)
    29.                 Next
    30.             ElseIf Delta_X < 0 Then
    31.                 For I = X1 To X0
    32.                     Canvas.SetPixel(I, Y0, Color.Black)
    33.                 Next
    34.             End If
    35.         Else
    36.             If Math.Abs(Delta_Y) > Math.Abs(Delta_X) Then
    37.                 Dim Delta_Err As Single = (Delta_X / Delta_Y)
    38.                 Dim Err As Single = 0
    39.  
    40.                 If Delta_Err > 0 Then
    41.                     Dim X_Value As Integer = X0
    42.  
    43.                     For I = Y0 To Y1
    44.                         Canvas.SetPixel(X_Value, I, Color.Black)
    45.                         Err += Delta_Err
    46.                         If Err > 0.5 Then
    47.                             X_Value += 1
    48.                             Err -= 1
    49.                         End If
    50.                     Next
    51.                 Else
    52.                     Dim X_Value As Integer = X1
    53.  
    54.                     For I = Y1 To Y0
    55.                         Canvas.SetPixel(X_Value, I, Color.Black)
    56.                         Err += Delta_Err
    57.                         If Err < -0.5 Then
    58.                             X_Value -= 1
    59.                             Err += 1
    60.                         End If
    61.                     Next
    62.                 End If
    63.             Else
    64.                 Dim Delta_Err As Single = Delta_Y / Delta_X
    65.                 Dim Err As Single = 0
    66.  
    67.                 If Delta_Err > 0 Then
    68.                     Dim Y_Value As Integer = Y0
    69.  
    70.                     For I = X0 To X1
    71.                         Canvas.SetPixel(I, Y_Value, Color.Black)
    72.                         Err += Delta_Err
    73.                         If Err > 0.5 Then
    74.                             Y_Value += 1
    75.                             Err -= 1
    76.                         End If
    77.                     Next
    78.                 Else
    79.                     Dim Y_Value As Integer = Y0
    80.  
    81.                     For I = X0 To X1
    82.                         Canvas.SetPixel(I, Y_Value, Color.Black)
    83.                         Err += Delta_Err
    84.                         If Err < -0.5 Then
    85.                             Y_Value -= 1
    86.                             Err += 1
    87.                         End If
    88.                     Next
    89.                 End If
    90.             End If
    91.         End If
    92.     End Sub
    Last edited by KBConsole; Apr 23rd, 2021 at 11:27 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width