Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleDraw an interesting curved Truchet Tiling in Visual Basic .NET
DescriptionThis example shows how to draw an interesting curved Truchet Tiling in Visual Basic .NET.
KeywordsTruchet Tiling, curve, draw, graphics, tile, tiling, VB.NET
CategoriesGraphics, VB.NET
 

When the program starts and when its form resizes, the MakeBitmap subroutine generates a new Truchet Tiling. It makes the image by tiling the form with one of the two tiles shown on the right, randomly picking a tile for each position. For more on Truchet Tilings, see Eric W. Weisstein's article Truchet Tiling at MathWorld--A Wolfram Web Resource.

 
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As _
    System.Windows.Forms.PaintEventArgs) Handles _
    MyBase.Paint
    e.Graphics.DrawImage(m_Bitmap, 0, 0)
End Sub

Private Sub Form1_Resize(ByVal sender As Object, ByVal e As _
    System.EventArgs) Handles MyBase.Resize
    m_Bitmap = MakeBitmap()
    Me.Invalidate()
End Sub

' Make a bitmap displaying the image.
Private Function MakeBitmap() As Bitmap
    Const RADIUS As Single = 12
    Const WID As Single = 2 * RADIUS
    Dim max_r As Integer
    Dim max_c As Integer
    Dim X As Single
    Dim Y As Single

    Dim bm As New Bitmap(Me.ClientSize.Width, _
        Me.ClientSize.Height)
    Dim gr As Graphics = Graphics.FromImage(bm)

    gr.Clear(Me.BackColor)
    max_c = ClientRectangle.Width \ WID + 1
    max_r = ClientRectangle.Height \ WID + 1
    Y = 0
    For r As Integer = 0 To max_r
        X = 0
        For c As Integer = 0 To max_c
            ' Note that angles are in clockwise degrees.
            If m_Random.Next(0, 2) = 0 Then
                gr.DrawArc(Pens.Black, X - RADIUS, Y - _
                    RADIUS, WID, WID, 0, 90)
                gr.DrawArc(Pens.Black, X + RADIUS, Y + _
                    RADIUS, WID, WID, 180, 90)
            Else
                gr.DrawArc(Pens.Black, X - RADIUS, Y + _
                    RADIUS, WID, WID, 270, 90)
                gr.DrawArc(Pens.Black, X + RADIUS, Y - _
                    RADIUS, WID, WID, 90, 90)
            End If
            X = X + WID
        Next c
        Y = Y + WID
    Next r

    gr.DrawRectangle(Pens.Black, 0, 0, Me.ClientSize.Width, _
        Me.ClientSize.Height)

    gr.Dispose()
    Return bm
End Function
 
If you left-click on the image, the program uses an unsafe flood fill method to color the clicked area. If you right-click on the image, the program uses a safe method to fill the clicked area. The program displays the time each method took in the Output window. In one set of tests, SafeFloodFill took about 5 times as long as UnsafeFloodFill.

For more information about flood fills in Visual Basic .NET, see Flood fill areas using safe and unsafe methods in Visual Basic .NET.

For more information about Stacks and unsafe bitmap operations, see my book Visual Basic 2005 Programmer's Reference.

 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated