Ant

This ant works on two simple rules. Using the number Pi, it visually turns right if it encounters an odd number, and visually turns left if it encounters an even number. Looping through one million digits of pi, a shape is created.

If it happens to repeat turns within a square, it adds to the ‘counter’ at that square, a variable recording the number of repeats. This counter is then used to determine the brightness of that square. This was quite a simple program to make, as, having had experience creating Langston’s ant before, it was just a matter of applying similar rules to a different situation.

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
Module Ant
    Const pi As String = My.Resources.Pi
    Dim Density(3839, 2159) As Integer ' Amount of times Ant has traversed.
    Dim Orientation As String = "North"

    Sub Main()
        ' Create A Bitmap Picture 3480 by 2160.
        Using BMP As New Drawing.Bitmap(3840, 2160)
            Dim x As Integer = 1920
            Dim y As Integer = 1080
            ' Starting Co-ords
            For Each i In pi
                If Val(i) Mod 2 = 0 Then
                    ' Turn Right
                    If Orientation = "North" Then
                        y -= 1
                        Orientation = "East"
                    ElseIf Orientation = "East" Then
                        x -= 1
                        Orientation = "South"
                    ElseIf Orientation = "South" Then
                        y += 1
                        Orientation = "West"
                    Else
                        x += 1
                        Orientation = "North"
                    End If
                Else
                    'Turn Left
                    If Orientation = "North" Then
                        y -= 1
                        Orientation = "West"
                    ElseIf Orientation = "East" Then
                        x -= 1
                        Orientation = "North"
                    ElseIf Orientation = "South" Then
                        y += 1
                        Orientation = "East"
                    Else
                        x += 1
                        Orientation = "South"
                    End If
                End If

                ' Wrap around edges
                If x < 0 Then x = 3839
                If y < 0 Then y = 2159
                If x >= 3840 Then x = 0
                If y >= 2160 Then y = 0

                Density(x, y) += 1
            Next

            For i = 0 To 3840 - 1
                For j = 0 To 2160 - 1
                    If Density(i, j) = 0 Then Continue For
                    Dim colour As New HSBColour(250, 0.5, 0.00549450549 * Density(i, j) + 0.15)
                    ' Brightness based on density
                    BMP.SetPixel(i, j, colour.HSBToRGB)
                Next
            Next
            BMP.Save("Ant.bmp", Drawing.Imaging.ImageFormat.Bmp)
        End Using
    End Sub
End Module

References

Please refer to Langton’s Ant