Previous Page
Next Page

Recipe 7.8. Drawing an Ellipse

Problem

You want to draw an ellipse (oval) at runtime.

Solution

Use the Pen.drawEllipse( ) method.

Discussion

An ellipse is a more abstract form of a circle. Rather than having a single, uniform radius, an ellipse is defined by two radii that intersect at right angles. The two radii are called the major and minor radii, and also sometimes simply the x radius and y radius.

Drawing an ellipse is just slightly more complex than drawing a circle. However, unlike circles, the Graphics class has no method for drawing ellipses. Therefore, the simplest way to programmatically create an ellipse is to use one of the methods of the Pen class. The drawEllipse( ) method allows for the following four parameters:



x

The x coordinate of the center of the ellipse.



y

The y coordinate of the center of the ellipse.



xRadius

The radius of the ellipse in the x direction (major axis).



yRadius

The radius of the ellipse in the y direction (minor axis).

The following code defines a Pen object and then draws an ellipse:

var pen:Pen = new Pen(sampleSprite.graphics);

pen.drawEllipse(100, 100, 100, 50);


Previous Page
Next Page
Converted from CHM to HTML with chm2web Pro 2.85 (unicode)