|
|
Title | Draw an interesting colored curved Truchet Tiling |
Description | This example shows how to draw an interesting colored curved Truchet Tiling in Visual Basic. |
Keywords | Truchet Tiling, curve, draw, graphics, tile, tiling |
Categories | Graphics |
|
|
This example's Paint event handler generates a 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.
After drawing the basic tiling, the program examines the points at the center of each tile. If that point is the original background color, then the program calls subroutine FloodAtPoint to fill the area with a random color. (This means some areas near the form's edges do not get colored but the result is still pretty interesting.)
The form's AutoRedraw property is True so the form does not need a Paint event handler.
|
|
Private Sub DrawPicture()
Const PI As Double = 3.14159265
Const RADIUS As Single = 12
Const WID As Single = 2 * RADIUS
Dim r As Integer
Dim c As Integer
Dim max_r As Integer
Dim max_c As Integer
Dim X As Single
Dim Y As Single
Cls
max_c = ScaleWidth \ WID + 1
max_r = ScaleHeight \ WID + 1
Y = 0
For r = 0 To max_r
X = 0
For c = 0 To max_c
If Rnd > 0.5 Then
Me.Circle (X, Y), RADIUS, , 1.5 * PI, 2 * PI
Me.Circle (X + WID, Y + WID), RADIUS, , 0.5 _
* PI, PI * 1.1
Else
Me.Circle (X + WID, Y), RADIUS, , PI, 1.5 * _
PI
Me.Circle (X, Y + WID), RADIUS, , 0, 0.55 * _
PI
End If
X = X + WID
Next c
Y = Y + WID
Next r
Line (0, 0)-(ScaleWidth - 1, ScaleHeight - 1), , B
' Flood areas.
Y = RADIUS
For r = 0 To max_r
X = RADIUS
For c = 0 To max_c
If Point(X, Y) = BackColor Then
FloodAtPoint X, Y
End If
X = X + WID
Next c
Y = Y + WID
Next r
End Sub
|
|
If you click on the image, the form's MouseDown event handler calls subroutine FloodAtPoint. That routine picks a random color and then uses the FloodFill API function to fill the indicated area.
|
|
' Flood fill at this point with a random color.
Private Sub FloodAtPoint(ByVal X As Single, ByVal Y As _
Single)
Dim color As Long
' Pick a random color other than black and
' the point's current color.
Do
color = QBColor(Int(15 * Rnd + 1))
Loop While color = Me.Point(X, Y)
' Create and select a brush of this color.
Me.FillStyle = vbFSSolid
Me.FillColor = color
' Do the flood, stopping when we
' reach black pixels.
FloodFill Me.hdc, X, Y, vbBlack
Me.FillStyle = vbFSTransparent
End Sub
|
|
For more information on graphics programming in Visual Basic, see my book Visual Basic Graphcis Programming.
|
|
|
|
|
|