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
 
 
 
 
 
TitleDraw a rotated ellipse
Keywordsellipse, rotate
CategoriesGraphics
 
Iterate an angle theta from 0 to 2 * PI radians. For each value of theta, X = wid * Cos(theta) and Y = hgt * Sin(theta) represents a point on the ellipse centered at the origin.

Rotate the ellipse by applying the equations:

    RX =  X * cos_angle + Y * sin_angle
    RY = -X * sin_angle + Y * cos_angle

Then add a translation to center the ellipse at (cx, cy).

 
' Draw an ellipse centered at (cx, cy) with dimensions
' wid and hgt rotated angle degrees.
Private Sub DrawEllipse(ByVal cx As Single, ByVal cy As _
    Single, ByVal wid As Single, ByVal hgt As Single, ByVal _
    angle As Single)
Const PI = 3.14159265
Dim sin_angle As Single
Dim cos_angle As Single
Dim theta As Single
Dim dtheta As Single
Dim X As Single     ' A point on the ellipse.
Dim Y As Single
Dim RX As Single    ' The point rotated.
Dim RY As Single

    Cls

    angle = angle * PI / 180
    sin_angle = Sin(angle)
    cos_angle = Cos(angle)

    theta = 0
    dtheta = 2 * PI / 50

    ' Find the first point.
    X = wid * Cos(theta)
    Y = hgt * Sin(theta)

    RX = cx + X * cos_angle + Y * sin_angle
    RY = cy - X * sin_angle + Y * cos_angle
    CurrentX = RX
    CurrentY = RY

    Do While theta < 2 * PI
        theta = theta + dtheta
        X = wid * Cos(theta)
        Y = hgt * Sin(theta)

        RX = cx + X * cos_angle + Y * sin_angle
        RY = cy - X * sin_angle + Y * cos_angle

        Line -(RX, RY)
    Loop
End Sub
 
For a lot more information on drawing graphical objects rotated, translated, stretched, and projected from three dimensions into two, see my book Ready-to-Run Visual Basic Graphics Programming.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated