Lesson 30: Drawing Polygon & Pie in VB2019

Master complex shapes with GDI+ using DrawPolygon and DrawPie methods

Key Takeaway

Visual Basic 2019 uses the DrawPolygon and DrawPie methods to create complex shapes by defining points and angles for precise geometric rendering.

After mastering text rendering in the previous lesson, we'll now explore how to draw polygons and pie shapes in VB2019. These advanced shapes allow you to create complex visual elements for your applications.

Polygon Fundamentals

Create multi-sided shapes by defining vertices

Pie Drawing

Create circular segments with precise angles

Point Structures

Define coordinates with Point objects

Pen Customization

Control line thickness and color

30.1 Drawing Polygons in VB2019

A polygon is a closed plane figure bounded by three or more straight sides. To draw a polygon in VB2019, you need to define the coordinates of all vertices and group them into a Point structure.

Polygon Drawing Fundamentals

The DrawPolygon method has the following syntax:

Syntax.vb
myGraphics.DrawPolygon(myPen, myPoints)
Parameter Description Required
myPen Pen object defining line thickness and color Yes
myPoints Array of Point objects defining vertices Yes

Creating Point Arrays

Point objects define the vertices of your polygon. You can create them individually and group them into an array:

PointExamples.vb
' Define individual points
Dim pointA As New Point(10, 10)
Dim pointB As New Point(100, 50)
Dim pointC As New Point(60, 150)

' Group points into an array
Dim myPoints As Point() = {pointA, pointB, pointC}

Triangle

3-sided polygon

Quadrilateral

4-sided polygon

Example 30.1: Drawing a Triangle

This example creates a blue triangle with 5px thick lines:

Form1.vb
Private Sub BtnDraw_Click(sender As Object, e As EventArgs) Handles BtnDraw.Click
    ' Create the Graphics object
    Dim myGraphics As Graphics = Me.CreateGraphics
    
    ' Define three points for the triangle
    Dim pointA As New Point(10, 10)
    Dim pointB As New Point(100, 50)
    Dim pointC As New Point(60, 150)
    
    ' Group points into an array
    Dim myPoints As Point() = {pointA, pointB, pointC}
    
    ' Create a blue pen with 5px thickness
    Dim myPen As New Pen(Drawing.Color.Blue, 5)
    
    ' Draw the triangle
    myGraphics.DrawPolygon(myPen, myPoints)
End Sub
Triangle drawn on form

Figure 30.1: Triangle drawn with DrawPolygon

Example 30.2: Drawing a Quadrilateral

This example creates a quadrilateral with four vertices:

Form1.vb
Private Sub BtnDraw_Click(sender As Object, e As EventArgs) Handles BtnDraw.Click
    ' Create the Graphics object
    Dim myGraphics As Graphics = Me.CreateGraphics
    
    ' Define four points for the quadrilateral
    Dim pointA As New Point(10, 10)
    Dim pointB As New Point(100, 50)
    Dim pointC As New Point(120, 150)
    Dim pointD As New Point(60, 200)
    
    ' Group points into an array
    Dim myPoints As Point() = {pointA, pointB, pointC, pointD}
    
    ' Create a blue pen with 5px thickness
    Dim myPen As New Pen(Drawing.Color.Blue, 5)
    
    ' Draw the quadrilateral
    myGraphics.DrawPolygon(myPen, myPoints)
End Sub
Quadrilateral drawn on form

Figure 30.2: Quadrilateral drawn with DrawPolygon

30.2 Drawing Pie Shapes

Pie shapes are circular segments defined by a bounding rectangle and start/sweep angles. In VB2019, you can draw pie shapes using the DrawPie method.

Pie Drawing Fundamentals

The DrawPie method has the following syntax:

Syntax.vb
myGraphics.DrawPie(myPen, X, Y, width, height, startAngle, sweepAngle)
Parameter Description Required
myPen Pen object defining line thickness and color Yes
X, Y Coordinates of the top-left corner of the bounding rectangle Yes
width, height Dimensions of the bounding rectangle Yes
startAngle Starting angle in degrees (0 = 3 o'clock position) Yes
sweepAngle Angle to sweep (positive = clockwise, negative = counterclockwise) Yes

60° Pie Segment

Start: 0°, Sweep: 60°

120° Pie Segment

Start: 0°, Sweep: 120°

Example 30.3: Drawing a Pie Segment

This example creates a pie segment starting at 0° and sweeping clockwise through 60°:

Form1.vb
Private Sub BtnDraw_Click(sender As Object, e As EventArgs) Handles BtnDraw.Click
    ' Create the Graphics object
    Dim myGraphics As Graphics = Me.CreateGraphics
    
    ' Create a blue pen with 5px thickness
    Dim myPen As New Pen(Drawing.Color.Blue, 5)
    
    ' Draw a pie segment
    ' (X, Y, width, height, startAngle, sweepAngle)
    myGraphics.DrawPie(myPen, 50, 50, 150, 150, 0, 60)
End Sub
Pie segment drawn on form

Figure 30.3: Pie segment drawn with DrawPie

Angle Direction Effect Example
Positive Sweep Clockwise direction DrawPie(..., 0, 90)
Negative Sweep Counter-clockwise direction DrawPie(..., 0, -90)

Lesson Summary

In this lesson, you've learned how to create complex shapes in VB2019:

DrawPolygon Method

Create multi-sided shapes by defining vertices

Point Structures

Define and group coordinates for polygon vertices

DrawPie Method

Create circular segments with precise angles

Angle Control

Control direction with positive/negative sweep angles

With polygon and pie shapes mastered, you're ready to explore coloring techniques. In the next lesson, we'll learn to fill shapes with colors.

Next Lesson

Learn to fill shapes with colors in Lesson 31: Coloring Shapes.

Related Resources

VB2019 Graphics Guide

Comprehensive guide to all graphics capabilities in VB2019

Explore Guide

Graphics Sample Code

Practical VB2019 code samples for graphics programming

View Examples

VB2019 Paperback

Comprehensive guide to Visual Basic 2019

Get on Amazon

VB2019 Kindle Edition

Digital version of Visual Basic 2019 Made Easy

Get on Kindle