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
DescriptionThis example shows how to make a kaleidoscope program in Visual Basic 6.
Keywordskaleidoscope, drawing, art
CategoriesGraphics
 
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 Form_MouseDown(Button As Integer, Shift As _
    Integer, X As Single, Y As Single)
    m_X = X
    m_Y = Y
    m_Drawing = True
End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As _
    Integer, X As Single, Y As Single)
Dim i As Integer
Dim angle As Double
Dim dx1 As Double
Dim dy1 As Double
Dim dx2 As Double
Dim dy2 As Double
Dim x1 As Double
Dim y1 As Double
Dim x2 As Double
Dim y2 As Double

    If Not m_Drawing Then Exit Sub

    Select Case m_CurrentStyle
        Case "Reflection X"
            Line (m_X, m_Y)-(X, Y)
            Line (2 * m_Cx - m_X, m_Y)-(2 * m_Cx - X, Y)
        Case "Reflection Y"
            Line (m_X, m_Y)-(X, Y)
            Line (m_X, 2 * m_Cy - m_Y)-(X, 2 * m_Cy - Y)
        Case "Reflection XY"
            Line (m_X, m_Y)-(X, Y)
            Line (2 * m_Cx - m_X, m_Y)-(2 * m_Cx - X, Y)
            Line (m_X, 2 * m_Cy - m_Y)-(X, 2 * m_Cy - Y)
            Line (2 * m_Cx - m_X, 2 * m_Cy - m_Y)-(2 * m_Cx _
                - X, 2 * m_Cy - Y)
        Case "Rotation 2"
            Line (m_X, m_Y)-(X, Y)
            dx1 = m_X - m_Cx
            dy1 = m_Y - m_Cy
            dx2 = X - m_Cx
            dy2 = Y - m_Cy
            Line (m_Cx - dx1, m_Cy - dy1)-(m_Cx - dx2, m_Cy _
                - dy2)
        Case "Rotation 4"
            Line (m_X, m_Y)-(X, Y)
            dx1 = m_X - m_Cx
            dy1 = m_Y - m_Cy
            dx2 = X - m_Cx
            dy2 = Y - m_Cy
            Line (m_Cx - dx1, m_Cy - dy1)-(m_Cx - dx2, m_Cy _
                - dy2)
            Line (m_Cx - dy1, m_Cy + dx1)-(m_Cx - dy2, m_Cy _
                + dx2)
            Line (m_Cx + dy1, m_Cy - dx1)-(m_Cx + dy2, m_Cy _
                - dx2)
        Case "Rotation 8"
            x1 = m_X
            y1 = m_Y
            x2 = X
            y2 = Y
            For i = 1 To 8
                Line (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 = X
            y2 = Y
            For i = 1 To CInt((2 * PI) / m_Angle)
                Line (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 = X
    m_Y = Y
End Sub

Private Sub Form_MouseUp(Button As Integer, Shift As _
    Integer, X As Single, Y As Single)
    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 Double, ByVal cy _
    As Double, ByVal angle As Double, ByRef X As Double, _
    ByRef Y As Double)
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 = cy - X * sin_angle + Y * cos_angle
    X = 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