VB2008 Lesson 26: Managing Graphics Drawing Polygon and Pie


We have learned how to draw rectangle, ellipse ,circle and text in the preceding chapters, now let's learn how to draw polygons on the screen. Besides that, we shall also learn how to draw pie. 26.1 Drawing Polygons Polygon is a closed plane figure bounded by three or more straight sides. In order to draw a polygon on the screen, we need to define the coordinates of all the points (also known as vertices) that joined up to form the polygon. The syntax to define the points of a polygon with vertices A1,A2,A3,A4.......An is as follows;
Dim A1 As New Point(X1,Y1) Dim A2 As New Point(X2,Y2) Dim A3 As New Point(X3,Y3) Dim A4 As New Point(X4,Y4) . . Dim An as New
Point(Xn,Yn) After declaring the points, we need to define a point structure that group all the points together using the following syntax:
Finally, create the graphics object and use the DrawPolygon method to draw the polygon using the following syntax:
where myPen is the Pen object created using the following syntax:
Example 26.1 Drawing a Triangle A triangle is a polygon with three vertices. Here is the sample code:
Running the program produces the image below:
Example 26.2: Drawing a Quadrilateral A quadrilateral is a polygon consists of four sides,so you need to define four vertices. The following is the code:
The output is shown below:
26.2: Drawing Pie In order to draw a pie, you can use the DrawPie method of the graphics object. As usual, you need to create the Graphics and the Pen objects. The syntax for drawing a pie is:
Where X and Y are the coordinates of the bounding rectangle, other arguments are selfexplanatory. Both StartAngle and SweepAngle are measured in degree. SweepAngle can take possible or negative values. If the value is positive, it sweep through clockwise direction while negative means it sweep through anticlockwise direction. Example 26.3: Drawing a pie that starts with 0 degree and sweep clockwise through 60 degree.
The output
Copyright 2008 Dr.Liew Voon Kiong . All rights reserved Contact: admin@vbtutor.net [Privacy Policy] 
This Web Page Created with PageBreeze Free Website Builder