# Board index » cppbuilder » Geometry problem - Should be an Easy Question - Can you help?

## Geometry problem - Should be an Easy Question - Can you help?

Problem:  Need to take a visual basic ellipse (whose code can not be
changed) and draw the exact ellipse in cppbuilder.

Need a function to translate a visual basic ellipse drawn with the circle
command (that takes an aspect ratio, a radius, and a center set of points)
and translate it to the winapi ellipse/vcl ellipse that takes 2 sets of
points for each edge of a rectange in which the ellipse is bound.  For the
visual basic side of things, the radius was calculated by using

centerX = (X + startx) / 2
centerY = (Y + starty) / 2

Radius = ((Abs(X - centerX) ^ 2 + Abs(Y - centerY) ^ 2) ^ 0.5)

and the aspect was calculated by using

CircleAspect = Abs((Y * ScaleRatio - starty * ScaleRatio) / (X - startx))

startx and starty are where the mouse was originally clicked, and x,y is
where the mouseup condition occurs in vb.

Everything has to look the same and be proportional.

This seems like it should be a simple geometry problem.  The following on
the visual basic circle command should be of help:

How to Draw an Ellipse with Circle Statement in VB
- 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.

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

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,
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.

2. 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

3. 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

4. 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:

Thanx,

Davinci

## Re:Geometry problem - Should be an Easy Question - Can you help?

Thanx  - and oing back the other way?

Davinci

##### Quote
Kevin Berry <kbe...@vitrex.net> wrote in message

news:7pidlm\$6hg4@forums.borland.com...
##### Quote
> Given: CenterX = 5, CenterY = 10, Radius = 5 (always horizontal aspect),
> AspectRatio = 2 (vertical to horizontal, so twice as tall as wide)
> Determine: Left, Top, Right, Bottom

> Left = CenterX - Radius = 5 - 5 = 0
> Top = CenterY - (Radius * AspectRatio) = 10 - (5 * 2) = 0
> Right = CenterX + Radius = 5 + 5 = 10
> Bottom = CenterY + (Radius * AspectRatio) = 10 + (5 * 2) = 20

> Kevin Berry
> soluti...@peoplesavers.com

> >Need a function to translate a visual basic ellipse drawn with the circle
> >command (that takes an aspect ratio, a radius, and a center set of
points)
> >and translate it to the winapi ellipse/vcl ellipse that takes 2 sets of
> >points for each edge of a rectange in which the ellipse is bound.