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
 
 
 
 
 
TitleMake a kaleidoscope program in Visual Basic .NET
DescriptionThis example shows how to make a kaleidoscope program in Visual Basic .NET.
Keywordskaleidoscope, drawing, art
CategoriesVB.NET, Graphics
 
When you click and draw on this program's form, the code draws other curves related to yours. For example, it might draw a mirror image of what you draw or it might repeat your drawing rotated by multiples of 30 degrees.

The MouseDown, MouseMove, and MouseUp routines do most of the work. MouseDown simply starts drawing and MouseUp ends it.

The MouseMove event handler draws your curve and its modified copies. It draws a line from the previous point (m_X, m_Y) to the mouse's current position. Then depending on the type of drawing (reflection, rotation, etc.) the program transforms the previous and current points and draws a line between them.

 
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e _
    As System.Windows.Forms.MouseEventArgs) Handles _
    MyBase.MouseDown
    m_X = e.X
    m_Y = e.Y
    m_Drawing = True
End Sub

Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e _
    As System.Windows.Forms.MouseEventArgs) Handles _
    MyBase.MouseMove
    Dim i As Integer
    Dim angle As Double
    Dim dx1 As Integer
    Dim dy1 As Integer
    Dim dx2 As Integer
    Dim dy2 As Integer
    Dim x1 As Integer
    Dim y1 As Integer
    Dim x2 As Integer
    Dim y2 As Integer

    If Not m_Drawing Then Exit Sub

    Dim gr As Graphics = Me.CreateGraphics()
    gr.DrawLine(Pens.Black, m_X, m_Y, e.X, e.Y)
    Select Case m_CurrentStyle
        Case "Reflection X"
            gr.DrawLine(Pens.Black, 2 * m_Cx - m_X, m_Y, 2 _
                * m_Cx - e.X, e.Y)
        Case "Reflection Y"
            gr.DrawLine(Pens.Black, m_X, 2 * m_Cy - m_Y, _
                e.X, 2 * m_Cy - e.Y)
        Case "Reflection XY"
            gr.DrawLine(Pens.Black, 2 * m_Cx - m_X, m_Y, 2 _
                * m_Cx - e.X, e.Y)
            gr.DrawLine(Pens.Black, m_X, 2 * m_Cy - m_Y, _
                e.X, 2 * m_Cy - e.Y)
            gr.DrawLine(Pens.Black, 2 * m_Cx - m_X, 2 * _
                m_Cy - m_Y, 2 * m_Cx - e.X, 2 * m_Cy - e.Y)
        Case "Rotation 2"
            dx1 = m_X - m_Cx
            dy1 = m_Y - m_Cy
            dx2 = e.X - m_Cx
            dy2 = e.Y - m_Cy
            gr.DrawLine(Pens.Black, m_Cx - dx1, m_Cy - dy1, _
                m_Cx - dx2, m_Cy - dy2)
        Case "Rotation 4"
            dx1 = m_X - m_Cx
            dy1 = m_Y - m_Cy
            dx2 = e.X - m_Cx
            dy2 = e.Y - m_Cy
            gr.DrawLine(Pens.Black, m_Cx - dx1, m_Cy - dy1, _
                m_Cx - dx2, m_Cy - dy2)
            gr.DrawLine(Pens.Black, m_Cx - dy1, m_Cy + dx1, _
                m_Cx - dy2, m_Cy + dx2)
            gr.DrawLine(Pens.Black, m_Cx + dy1, m_Cy - dx1, _
                m_Cx + dy2, m_Cy - dx2)
        Case "Rotation 8"
            x1 = m_X
            y1 = m_Y
            x2 = e.X
            y2 = e.Y
            For i = 1 To 8
                gr.DrawLine(Pens.Black, x1, y1, x2, y2)
                RotatePointAround(m_Cx, m_Cy, PI / 4, x1, _
                    y1)
                RotatePointAround(m_Cx, m_Cy, PI / 4, x2, _
                    y2)
            Next i
        Case "Rotation Other"
            x1 = m_X
            y1 = m_Y
            x2 = e.X
            y2 = e.Y
            For i = 1 To CInt((2 * PI) / m_Angle)
                gr.DrawLine(Pens.Black, x1, y1, x2, y2)
                RotatePointAround(m_Cx, m_Cy, m_Angle, x1, _
                    y1)
                RotatePointAround(m_Cx, m_Cy, m_Angle, x2, _
                    y2)
            Next i
    End Select

    m_X = e.X
    m_Y = e.Y
End Sub

Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e _
    As System.Windows.Forms.MouseEventArgs) Handles _
    MyBase.MouseUp
    m_Drawing = False
End Sub
 
Subroutine RotatePointAround rotates a point around another point. It subtracts thue center of rotation from the point to effectively translate to the origin. It then applies a simple rotation transformation and translates the center of rotation back to its original location.
 
' Rotate the point (X, Y) around the point (cx, cy) by the
' given angle.
Private Sub RotatePointAround(ByVal cx As Integer, ByVal cy _
    As Integer, ByVal angle As Double, ByRef X As Integer, _
    ByRef Y As Integer)
    Dim sin_angle As Double
    Dim cos_angle As Double
    Dim new_x As Double

    sin_angle = Sin(angle)
    cos_angle = Cos(angle)
    X = X - cx
    Y = Y - cy
    new_x = cx + X * cos_angle + Y * sin_angle
    Y = CInt(cy - X * sin_angle + Y * cos_angle)
    X = CInt(new_x)
End Sub
 
For much more information on geometric transformations, see my book Visual Basic Graphics Programming.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated