10.7 Drawing Arc and Pie Shapes

 <  Day Day Up  >  

You want to create an arc or pie shape.


Technique

Creating an arc or a pie is analogous to the method used for drawing lines and polygons in the last recipe. An arc uses a Pen object to draw an arc shape starting at a specified point. The arc is drawn by projecting a line from the start point using a specified start angle. Rendering the arc begins at the endpoint of the line and continues by creating an elliptical path ending at the endpoint, which is based on a sweep angle. Figure 10.3 shows the result of creating an arc from the following code. One point of confusion is that the start angle is measured clockwise from the X axis rather than the counter-clockwise method defined within a regular Cartesian coordinate system:

 
 graphics.DrawArc( new Pen( new SolidBrush(Color.Black)), 200, 200,     200, 200, 45.0f, 180.0f ); 
Figure 10.3. The DrawArc method draws a semicircular shape.

graphics/10fig03.gif

Comments

One of the many uses of the shape-drawing methods of GDI+ is charting data. The DrawRectangle or FillRectangle methods are well suited for bar charts . The DrawLine method is perfect for creating line charts, and the DrawPolygon method makes creating area charts simple. Throw in a few HatchBrush objects with different hatch styles to differentiate data points, and you can see how you can use all these objects to create the next charting application or control. Arcs and pies are naturally suited for pie and doughnut charts.

Creating an arc and creating a pie are virtually identical because the only difference is that a pie is simply an arc whose endpoints join together at the center of the partial ellipse. Each method uses a start and ending point as well as a width and height. When rendering begins with a call to DrawArc , DrawPie , or FillPie , GDI+ starts at the initial X and Y coordinates and moves in the direction specified as the start angle to a distance that is half of the width and height set in the method parameters. The actual rendering of the arc or pie then begins by drawing an elliptical shape that stops when the sweep angle is reached. A clockwise direction means a sweep angle of 90 degrees with a start angle of 0 would be a U shaped semicircle . If you are rendering a pie, then two lines are drawn from the two endpoints and meet in the center point of the shape, as shown in Figure 10.3.

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

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