Home

GDI+: Circular 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 Illustration

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 function play the same roll as those of the Graphics.DrawRectangle() method:

Definition of an Ellips

Here is an example:

 

private void button1_Click(object sender, System.EventArgs e)
{
	Graphics graph = this.CreateGraphics();

	Pen penCurrent = new Pen(Color.Red);
	graph.DrawEllipse(penCurrent, new Rectangle(20, 20, 226, 144));
}
Ellipse
 

Pies

A pie is a fraction of an ellipse delimited by two lines that span from the center of the ellipse to one side each. It can be illustrated as follows:

Pie Illustration

Like an ellipse (and like an arc), a pie is meant to fit in a rectangle. Based on this, a pie is delimited by a rectangular shaped that defines its location and the dimensions it would fit in.

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);

The first two versions allow you to specify a rectangle that would enclose the pie. The other two versions allow you to specify the location (with x and y) and dimensions of the pie. Besides the borders of the rectangle in which the pie would fit, a pie must specify its starting angle, startAngle, measured clockwise from the x-axis its starting point. A pie must also determine its sweep angle measured clockwise from the startAngle parameter to the end of the pie.

Here is an example:

private void button1_Click(object sender, System.EventArgs e)
{
	Graphics graph = this.CreateGraphics();

	Pen penCurrent = new Pen(Color.Red);
	graph.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. 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 draw an arc, you can use 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. Here is an example:

 

private void Form1_DoubleClick(object sender, System.EventArgs e)
{
	Graphics graph = this.CreateGraphics();

	Pen penCurrent = new Pen(Color.Red);
	graph.DrawArc(penCurrent, 20, 20, 200, 150, 225, 200);
}
Arc
 

Home Copyright © 2004-2009 FunctionX, Inc.