Module Module1

    Const PICWIDTH As Integer = 40
    Const PICHEIGHT As Integer = 40

    Sub fillPic(ByVal pic As Char(,), ByVal penColor As Char)
        For y As Integer = 0 To PICHEIGHT
            For x As Integer = 0 To PICWIDTH
                pic(x, y) = penColor
            Next
        Next
    End Sub
    Sub displayPic(ByVal pic As Char(,))
        For y As Integer = 0 To PICHEIGHT
            For x As Integer = 0 To PICWIDTH
                Console.Write(pic(x, y))
            Next
            Console.WriteLine()
        Next
        Console.ReadLine()
    End Sub
    Sub drawLine(ByVal pic As Char(,), ByVal penColor As Char, ByVal x1 As Integer, ByVal y1 As Integer, ByVal x2 As Integer, ByVal y2 As Integer)
        Dim x, y As Integer

        If x1 = x2 Then         ' Vertical Line
            ' We need to have a special case for a vertical line.
            ' The reason we need to treat a vertical line differently
            ' is that the slope of a vertical line is undefined since you
            ' would have to divide by zero when calculating the slope.
            ' In mathematics (and in computer programming) it is 
            ' an ERROR to divide by zero.

            For y = y1 To y2
                pic(x1, y) = penColor
            Next

        ElseIf y1 = y2 Then     'Horizontal Line
            ' The following is a special case for a horizontal line.
            '
            ' In a strict mathematical sense we don't need to treat
            ' horizontal lines as a special case. However, our algorithm
            ' below will cause a division by zero error for horizontal 
            ' lines too.
            For x = x1 To x2
                pic(x, y1) = penColor
            Next

        Else                    ' diagonal line

            ' Calculate the slope
            Dim m As Double = (y2 - y1) / (x2 - x1)

            ' For every x value on the line calculate the appropriate
            ' y value and display it on the line
            For x = x1 To x2
                ' The following comes from reorganizing the
                ' "point-slope" equation for a line, y-y1=m(x-x1), 
                ' where x1,y1 is a SPECIFIC point, x is a "free variable" - 
                ' i.e. x can take on any value and y is the value that
                ' is calculated from the specific value for x that 
                ' is chosen
                y = Math.Round(m * (x - x2) + y2)
                pic(x, y) = penColor
            Next

            ' For every y value on the line calculate the appropriate
            ' x value and display it on the line.
            For y = y1 To y2
                x = Math.Round((y - y2 + m * x2) / m)
                pic(x, y) = penColor
            Next
        End If

    End Sub
    Sub Main()
        Dim p1 As Char(,) = New Char(PICHEIGHT, PICWIDTH) {}
        Dim p2 As Char(,) = New Char(PICHEIGHT, PICWIDTH) {}
        Dim p3 As Char(,) = New Char(PICHEIGHT, PICWIDTH) {}
        Dim p4 As Char(,) = New Char(PICHEIGHT, PICWIDTH) {}

        fillPic(p1, ".")
        drawLine(p1, "1", 0, 0, PICWIDTH, 0)
        drawLine(p1, "2", 0, 0, PICWIDTH, 3)
        drawLine(p1, "3", 0, 0, PICWIDTH, 10)
        drawLine(p1, "4", 0, 0, PICWIDTH, 20)
        drawLine(p1, "5", 0, 0, PICWIDTH, 30)
        drawLine(p1, "6", 0, 0, PICWIDTH, PICHEIGHT)
        drawLine(p1, "7", 0, 0, 30, PICHEIGHT)
        drawLine(p1, "8", 0, 0, 20, PICHEIGHT)
        drawLine(p1, "9", 0, 0, 10, PICHEIGHT)
        drawLine(p1, "A", 0, 0, 5, PICHEIGHT)
        drawLine(p1, "B", 0, 0, 0, PICHEIGHT)

        displayPic(p1)

    End Sub

End Module