Microsoft KB Archive/83906

= How to Draw an Ellipse with Circle Statement in VB =

Article ID: 83906

Article Last Modified on 12/12/2003

-

APPLIES TO


 * Microsoft Visual Basic 2.0 Standard Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 2.0 Professional Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 1.0 Standard Edition

-



This article was previously published under Q83906



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
 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 &quot;ScaleWidth = &quot;; Format$(Form1.ScaleWidth, &quot;#&quot;) Print &quot;ScaleHeight = &quot;; Format$(Form1.ScaleHeight, &quot;#&quot;)

' 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 query words: 2.00 3.00

Keywords: KB83906

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.