Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleDraw a regular polygon in Visual Basic .NET
DescriptionThis example shows how to draw a regular polygon in Visual Basic .NET.
Keywordspolygon, regular polygon, draw, VB.NET
CategoriesVB.NET, Graphics
 
The program allocates an array to hold the polygon's points. It then loops through the array creating points offset from the form's center. The points lie on an ellipse that would touch the form's four edges.

The angle from the center to the first point is 0 radians (to the right). Each point after that is at the angle 2 * Pi / m_N radians larger than the previous angle (where m_N is the number of points).

 
' The number of points.
Dim m_N As Integer = 7

' Draw the regular polygon.
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As _
    System.Windows.Forms.PaintEventArgs) Handles _
    MyBase.Paint
    ' Make the points for a polygon.
    Dim pts(m_N - 1) As Point
    Dim cx As Integer = Me.ClientSize.Width \ 2
    Dim cy As Integer = Me.ClientSize.Height \ 2
    Dim theta As Double = 0
    Dim dtheta As Double = 2 * Math.PI / m_N
    For i As Integer = 0 To pts.Length - 1
        pts(i).X = CInt(cx + cx * Cos(theta))
        pts(i).Y = CInt(cy + cy * Sin(theta))
        theta += dtheta
    Next i

    ' Draw the polygon.
    e.Graphics.DrawPolygon(Pens.Blue, pts)
End Sub
 
(Actually the polygon isn't truly regular unless the form's client ares is square.)
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated