Algoritmo de dibujo lineal de Bresenham VB.NET


Algoritmo de dibujo lineal de Bresenham EN VISUAL BASIC.NET

Public Sub DrawLine(ByRef btmp As Bitmap, ByVal x As Integer, ByVal y As Integer, _
                    ByVal endX As Integer, ByVal endY As Integer, ByVal newColor As Color)
    Dim dx As Integer = Math.Abs(endX - x) ' calculate the change in x
    Dim dy As Integer = Math.Abs(endY - y) ' calculate the change in y
    Dim sx As Integer = Math.Sign(endX - x) ' calculate the sign of the change in x (pos/neg)
    Dim sy As Integer = Math.Sign(endY - y) ' calculate the sign of the change in y (pos/neg)
    Dim err As Integer = dx - dy ' calculate error (change in x minus change in y)
    Dim e2 As Integer = 0 ' initialize twice error to zero

    ' while the (x,y) vertex is not equal
    ' to any of the (endX,endY) coordinates
    While Not (x = endX AndAlso y = endY)
        ' if the x- and y-coordinates are within the bounds of the image
        If x >= 0 AndAlso x < btmp.Width AndAlso y >= 0 AndAlso y < btmp.Height Then
            btmp.SetPixel(x, y, newColor) ' color current pixel the new color
        End If

        ' calculate twice the error
        e2 = 2 * err

        ' if twice error is greater than
        ' the negative change in y
        If e2 > -dy Then
            err -= dy ' Subtract the change in y from the error
            x += sx ' increment/decrement x by the change in its sign from x to endX
        End If

        ' if twice error is less
        ' than the change in x
        If e2 < dx Then
            err += dx ' Add the change in x to the error
            y += sy ' increment/decrement y by the change in its sign from y to endY
        End If
    End While
End Sub

Comentarios