Today, I saw a netizen asking how to create a sector on csdn, So I thought of a graphical report I had done a year ago, and there was also a problem of creating a sector. PartCode, Post to share with you
The method used to draw a slice is actually a pseudo slice. I understand that it is actually made up of multiple isosceles triangles. When one side of a triangle is shorter, the more smooth the slices are.
Basic Method for creating an arc path
1 Basic Method for creating an arc path # Region Basic Method for creating an arc path
2 /**/ /// <Summary>
3 /// Draw an arc
4 /// </Summary>
5 /// <Param name = "X"> Arc center x </Param>
6 /// <Param name = "Y"> Arc center y </Param>
7 /// <Param name = "width"> Width of the external rectangle of the arc circle </Param>
8 /// <Param name = "height"> Height of the external rectangle of the arc circle </Param>
9 /// <Param name = "startangle"> Starting Angle </Param>
10 /// <Param name = "sweepangle"> End Angle </Param>
11 Public Static Point [] createarcpath ( Int X, Int Y, Int Width, Int Height, single startangle, single sweepangle)
12 {
13 Single [] xangle = New Single [ 12 ]; // The size of the array actually determines the smoothness of the arc.
14 Single [] yangle = New Single [ 12 ];
15 Point [] anglepoints = New Point [ 12 ];
16 Single Angle = Startangle;
17 Single angleincrement = (Sweepangle - Startangle) / 10 ;
18 For ( Int I = 0 ; I < 11 ; I ++ )
19 {
20 Xangle [I] = ( Float ) (X + (Math. Cos (Angle * (Math. Pi / 180 )) * (Width / 2 )));
21 Yangle [I] = ( Float ) (Y + (Math. Sin (Angle * (Math. Pi / 180 )) * (Height / 2 )));
22 Angle + = Angleincrement;
23 }
24 Xangle [ 11 ] = ( Float ) (X + (Math. Cos (sweepangle * (Math. Pi / 180 )) * (Width / 2 )));
25 Yangle [ 11 ] = ( Float ) (Y + (Math. Sin (sweepangle * (Math. Pi / 180 )) * (Height / 2 )));
26 Anglepoints [ 0 ] = New Point (x, y );
27 For ( Int I = 0 ; I < 11 ; I ++ )
28 {
29Anglepoints [I+ 1]= NewPoint ((Int) Xangle [I], (Int) Yangle [I]);
30}
31 Return Anglepoints;
32 }
33 # Endregion
Finally, we can use the e. Graphics. fillpolygon method to fill the image. The Code is as follows:
Code
For ( Int I = 0 ; I < M_perdateitem.length; I ++ )
{
Sweepangle = Strartangle + 360 * M_perdateitem [I] / 100 ;
Totper + = M_perdateitem [I];
Point [] arcpath = Pathutil. createarcpath (arccenter. X, arccenter. y, 170 , 170 , Strartangle, sweepangle );
Strartangle = Sweepangle;
E. Graphics. fillpolygon ( New Solidbrush (m_colors [I]), arcpath );
}
Similarly, if we only want to draw slices without filling them, call E. Graphics. drawpolygon.