Home

GDI+: Circle-Based Shapes

 

Ellipses and Circles

An ellipse is a closed continuous line whose points are positioned so that two points exactly opposite each other have the exact same distant from a central point. It can be illustrated as follows:

Ellipse

Because an ellipse can fit in a rectangle, in GDI+ programming, an ellipse is defined with regards to a rectangle it would fit in. To draw an ellipse, you can use the Graphics::DrawEllipse() method that comes in four versions whose syntaxes are:

public: void DrawEllipse(Pen *pen, Rectangle rect);
public: void DrawEllipse(Pen *pen, RectangleF rect);
public: void DrawEllipse(Pen *pen, int x, int y, int width, int height);
public: void DrawEllipse(Pen *pen, float x, float y, float width, float height);

The arguments of this method play the same roll as those of the Graphics::DrawRectangle() method:

Ellipse 2

Here is an example:

private: System::Void Form1_Paint(System::Object *  sender,
                                                        System::Windows::Forms::PaintEventArgs *  e)
 {
	 Pen *penCurrent = new Pen(Color::Red);
				 
	 e->Graphics->DrawEllipse(penCurrent, Rectangle(20, 20, 226, 144));
}
Ellipse
 

Pies

A pie is a fraction of an ellipse delimited by a starting angle and an angle that constitutes the desired portion to make up a pie. It can be illustrated as follows:

Pie Illustration

To draw a pie, you can use the Graphics::DrawPie() method that comes in various versions as follows:

public: void DrawPie(Pen *pen,
                                 Rectangle rect,
                                 float startAngle,
                                 float sweepAngle);
public: void DrawPie(Pen *pen,
                                 RectangleF rect,
                                 float startAngle,
                                 float sweepAngle);
public: void DrawPie(Pen *pen,
                                 int x,
                                 int y,
                                 int width,
                                 int height,
                                 int startAngle,
                                 int sweepAngle);
public: void DrawPie(Pen *pen,
                                 float x,
                                 float y,
                                 float width,
                                 float height,
                                 float startAngle,
                                 float sweepAngle);

A pie is based on an ellipse ( like an arc). The ellipse would fit in a rectangle passed as the rect argument. The rectangle can also be specified by its location (x, y) and its dimensions (width and height).

Inside of the parent rectangle in which an ellipse would be drawn, you set a starting angle. This angle is measured from 0 up counted clockwise (like the numbers of an analog clock). This means that an angle of 90 represents 6 o'clock and not 12 o'clock. This starting angle is passed as the startAngle argument.

After specifying the starting angle, you must specify the amount of angle covered by the pie. This also is measured clockwise. This value is passed as the sweepAngle argument.

Here is an example:

private: System::Void Form1_Paint(System::Object *  sender, System::Windows::Forms::PaintEventArgs *  e)
{
	 Pen *penCurrent = new Pen(Color::Red);
	 e->Graphics->DrawPie(penCurrent, 20, 20, 200, 100, 45, 255);
}

This would produce:

Pie

Arcs

An arc is a portion or segment of an ellipse, meaning an arc is a non-complete ellipse. While a pie is a closed shape, an arc is not: it uses only a portion of the line that defines an ellipse. Because an arc must confirm to the shape of an ellipse, it is defined as it fits in a rectangle and can be illustrated as follows:

Arc

To support arcs, the Graphics class is equipped with the DrawArc() method that is provided in four versions whose syntaxes are:

public: void DrawArc(Pen *pen,
                                Rectangle rect,
                                float startAngle, float sweepAngle);
public: void DrawArc(Pen *pen,
                                RectangleF rect,
                                float startAngle, float sweepAngle);
public: void DrawArc(Pen *pen,
                                int x, int y, int width, int height,
                                int startAngle, int sweepAngle);
public: void DrawArc(Pen *pen,
                                float x, float y, float width, float height,
                                float startAngle, float sweepAngle);

The ellipse that would contain the arc must be drawn in a Rectangle or a RectangleF rect. You can also define that ellipse by the coordinates of its inscribed rectangle x, y, and its dimensions width, height.  Besides the borders of the rectangle in which the arc would fit, an arc must specify its starting angle, startAngle, measured clockwise from the x-axis its starting point. An arc must also determine its sweep angle measured clockwise from the startAngle parameter to the end of the arc. These two value follow the same definitions we reviewed for the Graphics::Pie() method.

Here is an example:

 

private: System::Void Form1_Paint(System::Object *  sender, System::Windows::Forms::PaintEventArgs *  e)
{
	 Pen *penCurrent = new Pen(Color::Red);
	 e->Graphics->DrawArc(penCurrent, 20, 20, 200, 150, 225, 200);
}
 

Home Copyright © 2004-2010 FunctionX, Inc.