Drawing Arcs

Arcs are portions of ellipses and are measured in degrees, beginning at a starting angle and continuing for a specified number of degrees called the arc angle. An arc is said to sweep (traverse) its arc angle, beginning from its starting angle. Arcs that sweep in a clockwise direction are measured in positive degrees, whereas arcs that sweep in a counterclockwise direction are measured in negative degrees. Figure 17.16 depicts two arcs. Note that the arc at the left of the figure sweeps upward from zero degrees to approximately 110 degrees. Similarly, the arc at the right of the figure sweeps downward from zero degrees to approximately 110 degrees.

Figure 17.16. Positive and negative arc angles.

Notice the dashed boxes around the arcs in Fig. 17.16. Each arc is drawn as part of an oval (the rest of which is not visible). When drawing an oval, we specify the oval's dimensions in the form of a bounding rectangle that encloses the oval. The boxes in Fig. 17.16 correspond to these bounding rectangles. The Graphics methods used to draw arcsDrawArc, DrawPie and FillPieare summarized in Fig. 17.17.

Figure 17.17. Graphics methods for drawing arcs.

(This item is displayed on page 830 in the print version)

Graphics Methods And Descriptions

Note: Many of these methods are overloadedconsult the documentation for a complete listing.


 

[View full width]

DrawArc( Pen p, int x, int y, int width, int height, int startAngle, int sweepAngle )  

Draws an arc beginning from angle startAngle (in degrees) and sweeping sweepAngle degrees. The ellipse is defined by a bounding rectangle of width, height and upper-left corner (x,y). The Pen determines the color, border width and style of the arc.

 

 

[View full width]

DrawPie( Pen p, int x, int y, int width, int height, int startAngle, int sweepAngle )  

Draws a pie section of an ellipse beginning from angle startAngle (in degrees) and sweeping sweepAngle degrees. The ellipse is defined by a bounding rectangle of width, height and upper-left corner (x,y). The Pen determines the color, border width and style of the arc.

 

 

[View full width]

FillPie( Brush b, int x, int y, int width, int height, int startAngle, int sweepAngle )  

Functions similarly to DrawPie, except draws a solid arc (i.e., a sector). The Brush determines the fill pattern for the solid arc.

The program in Fig. 17.18 draws six images (three arcs and three filled pie slices) to demonstrate the arc methods listed in Fig. 17.17. To illustrate the bounding rectangles that determine the sizes and locations of the arcs, the arcs are displayed inside red rectangles that have the same x-y coordinates, width and height arguments as those that define the bounding rectangles for the arcs.

Figure 17.18. Drawing various arcs on a Form.

 1 // Fig. 17.18: DrawingArcs.cs
 2 // Drawing various arcs on a Form.
 3 using System;
 4 using System.Drawing;
 5 using System.Windows.Forms;
 6
 7 // draws various arcs
 8 public partial class DrawArcs : Form
 9 {
10 // default constructor
11 public DrawArcs()
12 {
13 InitializeComponent();
14 } // end constructor
15
16 // draw arcs
17 private void DrawArcs_Paint( object sender, PaintEventArgs e )
18 {
19 // get graphics object
20 Graphics graphicsObject = e.Graphics;
21 Rectangle rectangle1 = new Rectangle( 15, 35, 80, 80 );
22 SolidBrush brush1 = new SolidBrush( Color.Firebrick );
23 Pen pen1 = new Pen( brush1, 1 );
24 SolidBrush brush2 = new SolidBrush( Color.DarkBlue );
25 Pen pen2 = new Pen( brush2, 1 );
26
27 // start at 0 and sweep 360 degrees
28 graphicsObject.DrawRectangle( pen1, rectangle1 );
29 graphicsObject.DrawArc( pen2, rectangle1, 0, 360 );
30 31 // start at 0 and sweep 110 degrees 32 rectangle1.Location = new Point( 100, 35 ); 33 graphicsObject.DrawRectangle( pen1, rectangle1 ); 34 graphicsObject.DrawArc( pen2, rectangle1, 0, 110 ); 35 36 // start at 0 and sweep -270 degrees 37 rectangle1.Location = new Point( 185, 35 ); 38 graphicsObject.DrawRectangle( pen1, rectangle1 ); 39 graphicsObject.DrawArc( pen2, rectangle1, 0, -270 ); 40 41 // start at 0 and sweep 360 degrees 42 rectangle1.Location = new Point( 15, 120 ); 43 rectangle1.Size = new Size( 80, 40 ); 44 graphicsObject.DrawRectangle( pen1, rectangle1 ); 45 graphicsObject.FillPie( brush2, rectangle1, 0, 360 ); 46 47 // start at 270 and sweep -90 degrees 48 rectangle1.Location = new Point( 100, 120 ); 49 graphicsObject.DrawRectangle( pen1, rectangle1 ); 50 graphicsObject.FillPie( brush2, rectangle1, 270, -90 ); 51 52 // start at 0 and sweep -270 degrees 53 rectangle1.Location = new Point( 185, 120 ); 54 graphicsObject.DrawRectangle( pen1, rectangle1 ); 55 graphicsObject.FillPie( brush2, rectangle1, 0, -270 ); 56 } // end method DrawArcs_Paint 57 } // end class DrawArcs

Lines 2025 create the objects that we need to draw various arcsa Graphics object, a Rectangle, SolidBrushes and Pens. Lines 2829 then draw a rectangle and an arc inside the rectangle. The arc sweeps 360 degrees, forming a circle. Line 32 changes the location of the Rectangle by setting its Location property to a new Point. The Point constructor takes as arguments the x- and y-coordinates of the new point. The Location property determines the upper-left corner of the Rectangle. After drawing the rectangle, the program draws an arc that starts at 0 degrees and sweeps 110 degrees. Because the angles increase in a clockwise direction, the arc sweeps downward.

Lines 3739 perform similar functions, except that the specified arc sweeps 270 degrees. The Size property of a Rectangle determines the arc's height and width. Line 43 sets the Size property to a new Size object, which changes the size of the rectangle.

The remainder of the program is similar to the portions described above, except that a SolidBrush is used with method FillPie. The resulting arcs, which are filled, can be seen in the bottom half of the sample output (Fig. 17.18).





Visual C# How to Program
Visual C# 2005 How to Program (2nd Edition)
ISBN: 0131525239
EAN: 2147483647
Year: 2004
Pages: 600
Simiral book on Amazon

Flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net