     Home Search   What's New Index Books Links Q & A Newsletter Banners   Feedback Tip Jar      MSDN Visual Basic Community  Title Use transformations to draw a rotated ellipse in Visual Basic 6 This example shows how to use transformations to draw a rotated ellipse in Visual Basic 6. It uses routines that translate, rotate, and scale to make a rotated ellipse. transformation, ellipse, rotated ellipse, rotation, Visual Basic 6 Algorithms, Graphics

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```

When the form receives a Paint event, it draws the rotated ellipse. The following code uses the CirclePoint subroutine to get the point on the circle corresponding to theta = 0. It then calls subroutines:

• ScalePoint to squash the ellipse by a factor of 0.5 vertically
• RotatePoint to rotate the ellipse 30 degrees around the origin
• TranslatePoint to move the result so it is centered at the point (1440, 1440) measured in twips

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 Form_Paint()
Const PI As Single = 3.14159265
Const DTHETA As Single = PI / 20
Const RADIUS As Single = 1440

Dim x0 As Single
Dim y0 As Single
Dim x As Single
Dim y As Single
Dim theta As Single

' Get the first point.
CirclePoint 0, RADIUS, x, y
ScalePoint 1, 0.5, x, y
RotatePoint PI / 6, x, y
TranslatePoint 1440, 1440, x, y
CurrentX = x
CurrentY = y
x0 = x
y0 = y

' Draw other points.
For theta = DTHETA To 2 * PI Step DTHETA
CirclePoint theta, RADIUS, x, y
ScalePoint 1, 0.5, x, y
RotatePoint PI / 6, x, y
TranslatePoint 1440, 1440, x, y
Line -(x, y)
Next theta

' Close the circle.
Line -(x0, y0)
End Sub```  Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.  Updated 