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:

Overloads Public Sub DrawEllipse(ByVal pen As Pen, ByVal rect As Rectangle)
Overloads Public Sub DrawEllipse(ByVal pen As Pen, ByVal RectangleF rect)
Overloads Public Sub DrawEllipse(ByVal pen As Pen, ByVal x As Integer, ByVal  y As Integer, _
			      ByVal width As Integer, ByVal height As Integer)
Overloads Public Sub DrawEllipse(ByVal pen As Pen, ByVal x As Single, ByVal y As Single, _
			      ByVal width As Single, ByVal height As Single)

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

Ellipse 2

Here is an example:

Private Sub Form1_Paint(ByVal sender As Object, _
		ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        Dim penCurrent As New Pen(Color.Red)

        e.Graphics.DrawEllipse(penCurrent, New Rectangle(20, 20, 226, 144))
End Sub
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 Sub DrawPie(ByVal pen As Pen, _
                                 ByVal rect As Rectangle, _
                                 ByVal startAngle As Single, _
                                 ByVal sweepAngle As Single)
Public Sub DrawPie(ByVal pen As Pen, _
                                 ByVal rect As RectangleF, _
                                 ByVal startAngle As Single, _
                                 ByVal sweepAngle As Single)
Public Sub DrawPie(ByVal pen As Pen, _
                                ByVal  x As Integer, _
                                 ByVal y As Integer, _
                                 ByVal width As Integer, _
                                 ByVal height As Integer, _
                                 ByVal startAngle As Integer, _
                                 ByVal sweepAngle As Integer)
Public Sub DrawPie(ByVal pen As Pen, _
                                 ByVal x As Single, _
                                 ByVal y As Single, _
                                 ByVal width As Single, _
                                 ByVal height As Single, _
                                 ByVal startAngle As Single, _
                                 ByVal sweepAngle As Single)

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 Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        Dim penCurrent As New Pen(Color.Red)
        e.Graphics.DrawPie(penCurrent, 20, 20, 200, 100, 45, 255)
End Sub

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 Sub DrawArc(ByVal pen As Pen, _
                                ByVal rect As Rectangle, _
                                ByVal Single startAngle, ByVal Single sweepAngle)
Public Sub DrawArc(ByVal pen As Pen, _
                                ByVal rect RectangleF, _
                                ByVal startAngle As Single, ByVal sweepAngle As Single)
Public Sub DrawArc(ByVal pen As Pen, _
                                ByVal x As Integer, ByVal y As Integer, ByVal width As Integer, ByVal height As Integer, _
                                ByVal startAngle As Integer, ByVal sweepAngle As Integer)
Public Sub DrawArc(ByVal pen As Pen, _
                                ByVal x As Single, ByVal y As Single, ByVal width As Single, ByVal height As Single, _
                                ByVal startAngle As Single, ByVal sweepAngle As Single)

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 Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) _
			Handles MyBase.Paint
        Dim penCurrent As New Pen(Color.Red)
        
        e.Graphics.DrawArc(penCurrent, 20, 20, 200, 150, 225, 200)
End Sub
 

Home Copyright © 2004-2012, FunctionX