Bresenham algorithms (Post #5)

So for this post I'll be implementing the following algorithms in VB.Net and draw them in our GDI+ rendering window we created in the previous posts.
  1. Bresenham line
  2. Bresenham circle 

For the first algorithm (Line)

Public Sub BresenhamLineAlgo(ByVal Xs As Integer, ByVal Ys As Integer, ByVal Xt As Integer, ByVal Yt As Integer)

        Dim X, Y As Integer
        Dim SquaredY As Integer = (Yt - Ys) * 2 'Our y difference
        Dim SlopeError As Integer = SquaredY - (Xt - Xs)

        Y = Ys
        For X = Xs To Xt Step 1

            SlopeError += SquaredY

            If (SlopeError >= 0) Then
                Y = Y + 1
                SlopeError -= (Xt - Xs) * 2
            End If
            BresenhamLine(X).X = X
            BresenhamLine(X).Y = Y
        Next
    End Sub

Note that I declared a point array and added the following code to SoftRender("DRAW") 

Point array declaration

Public BresenhamLine(100) As Point

SoftRender("DRAW")

For X = 0 To 100
    QRect = New Rectangle(GridWorld(BresenhamLine(X).X, BresenhamLine(X).Y).X, GridWorld(BresenhamLine(X).X, BresenhamLine(X).Y).Y, GridWidth, GridWidth)
    Gs.FillRectangle(Brushes.Red, QRect)
    Next

Now when calling this sub (example : BresenhamLineAlgo(3, 3, 22, 12)) we'll get the following result.

Bear in mind that the above code will draw a bresenham line but on Target X,Y > Starting X,Y. You need to add conditions for when target point X and Y are lower that the starting point and twitch the loop.

And for the second algorithm (Circle)

We calculate one octance of the circle and mirror it to the other octanes

Declarations

    Public BresenhamCircle(200) As Point
    Public BresenhamCycles As Integer

Rendering change

            For X = 0 To 100
                QRect = New Rectangle(GridWorld(BresenhamCircle(X).X, BresenhamCircle(X).Y).X, GridWorld(BresenhamCircle(X).X, BresenhamCircle(X).Y).Y, GridWidth, GridWidth)
                Gs.FillRectangle(Brushes.Red, QRect)
            Next

Mirroring sub

    Public Sub BresenhamMirror(ByVal Xc As Integer, ByVal Yc As Integer, ByVal X As Integer, ByVal Y As Integer)
        BresenhamCycles += 1
        BresenhamCircle(BresenhamCycles).X = Xc + X
        BresenhamCircle(BresenhamCycles).Y = Yc + Y
        BresenhamCycles += 1
        BresenhamCircle(BresenhamCycles).X = Xc - X
        BresenhamCircle(BresenhamCycles).Y = Yc + Y
        BresenhamCycles += 1
        BresenhamCircle(BresenhamCycles).X = Xc + X
        BresenhamCircle(BresenhamCycles).Y = Yc - Y
        BresenhamCycles += 1
        BresenhamCircle(BresenhamCycles).X = Xc - X
        BresenhamCircle(BresenhamCycles).Y = Yc - Y
        BresenhamCycles += 1
        BresenhamCircle(BresenhamCycles).X = Xc + Y
        BresenhamCircle(BresenhamCycles).Y = Yc + X
        BresenhamCycles += 1
        BresenhamCircle(BresenhamCycles).X = Xc - Y
        BresenhamCircle(BresenhamCycles).Y = Yc + X
        BresenhamCycles += 1
        BresenhamCircle(BresenhamCycles).X = Xc + Y
        BresenhamCircle(BresenhamCycles).Y = Yc - X
        BresenhamCycles += 1
        BresenhamCircle(BresenhamCycles).X = Xc - Y
        BresenhamCircle(BresenhamCycles).Y = Yc - X
    End Sub

And for the calculation sub

 Public Sub BresenhamCircleAlgo(ByVal Xs As Integer, ByVal Ys As Integer, ByVal R As Integer)
        Dim X As Integer = 0
        Dim Y As Integer = 0 + R
        Dim D As Integer = 3 - 2 * R
        While Y >= X
            BresenhamMirror(Xs, Ys, X, Y)
            X = X + 1
            If D > 0 Then
                Y -= 1
                D = D + 4 * (X - Y) + 10
            Else
                D = D + 4 * X + 6
            End If
        End While
    End Sub

Now when we call the sub (example : BresenhamCircleAlgo(12, 12, 8)) we'll have a result like shown below.

Examples of Bresenham algorithms use.


Casting a ray which detects a wall and a simple circle with changing radius animation

Comments