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
 
 
 
 
 
TitleUse transformations to draw an animated atom in Visual Basic 6
DescriptionThis example shows how to use transformations to draw an animated atom in Visual Basic 6. It uses routines that translate, rotate, and scale to make rotated ellipses.
Keywordsanimation, atom, animated atom, transformation, ellipse, rotated ellipse, rotation, Visual Basic 6
CategoriesAlgorithms, Graphics, Multimedia
 
The following subroutine returns a point (x, y) on a circle. The angle theta determines the point's position around the circle.
 
Private Sub CirclePoint(ByVal theta As Single, ByVal _
    circle_radius As Single, ByRef x As Single, ByRef y As _
    Single)
    x = circle_radius * Cos(theta)
    y = circle_radius * Sin(theta)
End Sub
 
The following routines rotate a point around the origin, scale a point in the X and Y directions, and translate a point in the X and Y directions.
 
Private Sub RotatePoint(ByVal theta As Single, ByRef x As _
    Single, ByRef y As Single)
Dim new_x As Single
Dim new_y As Single

    new_x = x * Cos(theta) + y * Sin(theta)
    new_y = x * Sin(theta) - y * Cos(theta)
    x = new_x
    y = new_y
End Sub

Private Sub ScalePoint(ByVal scale_x As Single, ByVal _
    scale_y As Single, ByRef x As Single, ByRef y As Single)
    x = x * scale_x
    y = y * scale_y
End Sub

Private Sub TranslatePoint(ByVal tx As Single, ByVal ty As _
    Single, ByRef x As Single, ByRef y As Single)
    x = x + tx
    y = y + ty
End Sub
 
Subroutine EllipsePoint returns a point on a scaled, rotated, and translated ellipse. It uses CirclePoint to get a point on a circle. It then scales, rotates, and translates the point appropriately.
 
Private Sub EllipsePoint(ByRef x As Single, ByRef y As _
    Single, ByVal theta As Single, ByVal radius As Single, _
    ByVal sx As Single, ByVal sy As Single, ByVal angle As _
    Single, ByVal tx As Single, ByVal ty As Single)
    CirclePoint theta, radius, x, y
    ScalePoint sx, sy, x, y
    RotatePoint angle, x, y
    TranslatePoint tx, ty, x, y
End Sub
 
Subroutine DrawEllipse makes an ellipse that is scaled, rotated, and translated. It calls EllipsePoint to get the point on the ellipse corresponding to theta = 0. The code moves the current drawing position to the resulting point and saves the point for later.

Next the code loops through values of theta up to 2 * PI, drawing lines to each in turn. It finishes by reconnecting to the first point.

 
Private Sub DrawEllipse(ByVal radius As Single, ByVal sx As _
    Single, ByVal sy As Single, ByVal angle As Single, _
    ByVal tx As Single, ByVal ty As Single, ByVal clr As _
    OLE_COLOR)
Const DTHETA As Single = PI / 20
Dim x0 As Single
Dim y0 As Single
Dim x As Single
Dim y As Single
Dim theta As Single

    ' Get the first point.
    EllipsePoint x, y, 0, radius, sx, sy, angle, tx, ty
    CurrentX = x
    CurrentY = y
    x0 = x
    y0 = y

    ' Draw other points.
    For theta = DTHETA To 2 * PI Step DTHETA
        EllipsePoint x, y, theta, radius, sx, sy, angle, _
            tx, ty
        Line -(x, y), clr
    Next theta

    ' Close the circle.
    Line -(x0, y0), clr
End Sub
 
When the form's Timer fires, the event handler calls the form's Paint event handler.

The Paint event handler draws ellipses to represent the electrons' orbits. It uses EllipsePoint to get points on each ellipse and draws the electrons there. The module-level variable m_Alpha keeps track of the positions of the electrons along their orbits. Note that the code multiplies m_Alpha by a different amount for each electron so the electrons don't move at the same speed.

 
Private Const PI As Single = 3.14159265
Private Const DALPHA As Single = PI / 6
Private m_Alpha As Single

Private Sub tmrRotate_Timer()
    Form_Paint
End Sub

Private Sub Form_Paint()
Const E_RADIUS As Single = 60
Dim cx As Single
Dim cy As Single
Dim x As Single
Dim y As Single

    Cls

    ' Draw the orbits and electrons.
    cx = 1440
    cy = 1440
    DrawEllipse 1440, 0.5, 0.15, PI / 3, cx, cy, vbRed
    EllipsePoint x, y, m_Alpha, 1440, 0.5, 0.15, PI / 3, _
        cx, cy
    Me.FillColor = vbRed
    Me.Circle (x, y), E_RADIUS, vbRed

    DrawEllipse 1440, 0.5, 0.15, -PI / 3, cx, cy, vbRed
    EllipsePoint x, y, m_Alpha * 0.9, 1440, 0.5, 0.15, -PI _
        / 3, cx, cy
    Me.FillColor = RGB(0, 128, 64)
    Me.Circle (x, y), E_RADIUS, RGB(0, 128, 64)

    DrawEllipse 1440, 0.5, 0.15, 0, cx, cy, vbRed
    EllipsePoint x, y, -m_Alpha * 0.8, 1440, 0.5, 0.15, 0, _
        cx, cy
    Me.FillColor = vbBlue
    Me.Circle (x, y), E_RADIUS, vbBlue

    ' Draw the nucleus.
    Me.FillColor = vbBlack
    Me.Circle (cx, cy), 120, vbBlack

    ' Rotate a bit.
    m_Alpha = m_Alpha + DALPHA
End Sub
 
(Yes, I know this is not how atoms really work, but it looks cooler than a probability fog.)
 
 
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated