How to Draw an Ellipse with Circle Statement in VB

Last reviewed: June 21, 1995
Article ID: Q83906
The information in this article applies to:

- Standard and Professional Editions of Microsoft Visual Basic for

  Windows, versions 2.0 and 3.0
- Microsoft Visual Basic programming system for Windows, version 1.0

SUMMARY

This article describes how to use the Circle statement to draw an ellipse of a specified width and height by calculating the radius and aspect ratio appropriate for the dimensions of the ellipse and the units of measurement, determined by the ScaleMode property.

MORE INFORMATION

The Circle statement takes two arguments that determine the shape of the ellipse drawn: the radius and the aspect ratio. For example:

   Circle (x, y), radius,,,, aspect

The aspect ratio is the y-radius divided by the x-radius of the ellipse drawn. An aspect ratio of 1.0 (the default) yields a perfect (non-elliptical) circle. If the aspect ratio is less than one, the radius argument specifies the x-radius. If the aspect ratio is greater than one, the radius argument specifies the y-radius. Both the x-radius and the y-radius are measured in units of the x-axis.

Steps to Create Example Program

1. Run Visual Basic, or from the File menu, choose New Project (ALT,
   F, N) if Visual Basic is already running. Form1 is created by
   default.

  • Enter the following code in the Form1_Click event procedure:

    Sub Form_Click ()
        Cls
    
        ' Set x-axis units different from y-axis to demonstrate
        ' that the ellipse still comes out right.
        Form1.ScaleWidth = Rnd * 100
        Form1.ScaleHeight = Rnd * 100
        Print "ScaleWidth  = "; Format$(Form1.ScaleWidth, "#")
        Print "ScaleHeight = "; Format$(Form1.ScaleHeight, "#")
    
        ' Print the dimensions of the ellipse.
        ' Draw an ellipse centered on the form and touching the
        ' borders.
        w = Form1.ScaleWidth / 2
        h = Form1.ScaleHeight / 2
        Call ellipse(Form1, w, h, w, h)
    End Sub
    
    

  • Enter the following code in the general Declarations section:

    ' ellipse(frm, x, y, w, h)
    '   Purpose
    '     Draws an ellipse on a form.
    '   Parameters
    '     frm  -- the form to draw on
    '     x, y -- specify the center of the ellipse.
    '     w, h -- specify the width and height.
    '
    Sub ellipse (frm As Form, ByVal x!, ByVal y!, ByVal w!, ByVal h!)
        Dim swt As Long    ' ScaleWidth  in twips
        Dim sht As Long    ' ScaleHeight in twips
        Dim k As Double    ' conversion factor for x-units to y-units
        Dim ar As Double   ' aspect ratio
        Dim r As Single    ' radius
        Dim save_mode As Integer   ' for saving and restoring ScaleMode
        Dim save_width As Single   ' for saving and restoring ScaleWidth
        Dim save_height As Single  ' for saving and restoring ScaleHeight
    
        ' Check arguments.
        If w <= 0 Or h <= 0 Then Stop
    
        ' Determine form dimensions in twips.
        save_mode = frm.ScaleMode  ' save Scale... properties
        save_width = frm.ScaleWidth
        save_height = frm.ScaleHeight
        frm.ScaleMode = 1          ' set units to twips
        swt = frm.ScaleWidth
        sht = frm.ScaleHeight
        frm.ScaleMode = save_mode  ' restore Scale... properties
        If frm.ScaleMode = 0 Then
            frm.ScaleWidth = save_width
            frm.ScaleHeight = save_height
        End If
    
    ' Compute conversion factor of x-axis units to y-axis units.
        k = frm.ScaleWidth / frm.ScaleHeight * sht / swt
    
    ' Compute aspect ratio and radius.
        ar = k * h / w
        If ar <= 1 Then
            r = w
        Else
            r = k * h
        End If
    
        ' Draw the ellipse.
        frm.Circle (x, y), r, , , , ar
    End Sub
    
    

  • Press F5 to run the program. Then click the form.

    The program draws an ellipse centered in the form, and touching the sides of the form. Resize the form and/or click the form again to repeat the demonstration.


  • Additional reference words: 1.00 2.00 3.00
    KBCategory: kbgraphic kbprg kbcode
    KBSubcategory: APrgGrap


    THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

    Last reviewed: June 21, 1995
    © 1998 Microsoft Corporation. All rights reserved. Terms of Use.