Lesson 30

Drawing Polygon & Pie

In previous lessons, you learned how to draw rectangles, ellipses, circles, and text in Visual Basic 2015. In this lesson, you will learn how to draw more advanced graphics: polygons and pie shapes.

30.1 Drawing Polygons

A polygon is a closed plane figure bounded by three or more straight sides. To draw a polygon on the screen, you must define all the points, also called vertices, that join together to form the shape. :contentReference[oaicite:1]{index=1}

The syntax to define the points of a polygon is:

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, you group them together in a point structure like this:

Dim myPoints As Point() = {A1, A2, A3, ..., An}

Finally, create the Graphics object and use the DrawPolygon method:

Dim myGraphics As Graphics = Me.CreateGraphics()
myGraphics.DrawPolygon(myPen, myPoints)

where myPen is the Pen object, for example:

myPen = New Pen(Drawing.Color.Blue, 5)

Example 30.1 – Drawing a Triangle

A triangle is a polygon with three vertices. The code is:

Private Sub BtnDraw_Click(sender As Object, e As EventArgs) Handles BtnDraw.Click

    Dim myPen As Pen
    Dim A As New Point(10, 10)
    Dim B As New Point(100, 50)
    Dim C As New Point(60, 150)
    Dim myPoints As Point() = {A, B, C}

    myPen = New Pen(Drawing.Color.Blue, 5)

    Dim myGraphics As Graphics = Me.CreateGraphics()
    myGraphics.DrawPolygon(myPen, myPoints)

End Sub

Running the program produces the image shown below.

Triangle drawn in Visual Basic 2015

Figure 30.1 – Drawing a triangle

Example 30.2 – Drawing a Quadrilateral

A quadrilateral is a polygon with four sides, so you need four vertices.

Private Sub BtnDraw_Click(sender As Object, e As EventArgs) Handles BtnDraw.Click

    Dim myPen As Pen
    Dim A As New Point(10, 10)
    Dim B As New Point(100, 50)
    Dim C As New Point(120, 150)
    Dim D As New Point(60, 200)
    Dim myPoints As Point() = {A, B, C, D}

    myPen = New Pen(Drawing.Color.Blue, 5)

    Dim myGraphics As Graphics = Me.CreateGraphics()
    myGraphics.DrawPolygon(myPen, myPoints)

End Sub

The output image is shown below.

Quadrilateral drawn in Visual Basic 2015

Figure 30.2 – Drawing a quadrilateral

30.2 Drawing a Pie

To draw a pie, use the DrawPie method of the Graphics object. As usual, you must first create the Graphics and Pen objects. The syntax is:

myGraphics.DrawPie(myPen, X, Y, width, height, StartAngle, SweepAngle)

Where:

  • X, Y are the coordinates of the bounding rectangle
  • width and height determine the size of the pie
  • StartAngle is the starting angle in degrees
  • SweepAngle is the angle through which the arc is drawn

Both angles are measured in degrees. If the sweep angle is positive, it moves clockwise. If it is negative, it moves anticlockwise. :contentReference[oaicite:2]{index=2}

Example 30.3 – Drawing a Pie

The following example draws a pie that starts at 0 degrees and sweeps clockwise through 60 degrees.

Private Sub BtnDraw_Click(sender As Object, e As EventArgs) Handles BtnDraw.Click

    Dim myPen As Pen
    myPen = New Pen(Drawing.Color.Blue, 5)

    Dim myGraphics As Graphics = Me.CreateGraphics()
    myGraphics.DrawPie(myPen, 50, 50, 150, 150, 0, 60)

End Sub

The output image is shown below.

Pie shape drawn in Visual Basic 2015

Figure 30.3 – Drawing a pie

Build on This Foundation

Continue to VB2026

After learning the basics of checkbox controls in VB2015, move to the newest VB2026 tutorial for a more modern VB.NET learning path.

Explore VB2026 →

Visual Basic Programming

Visual Basic Programming

Use this Top Release book to reinforce your tutorial learning with a more structured guide.

Key Takeaways:
  • A polygon is formed by joining multiple points.
  • Use Point objects and group them into a Point() array.
  • Use DrawPolygon to draw triangles, quadrilaterals, and other polygons.
  • Use DrawPie to draw pie segments based on angles.

Next: Coloring Shapes

Go to Lesson 31 →