You only have to change the above code a little to draw any multi-sided, equilateral polygons.

Const POLYGONSIDES As Integer = 4 ' change number of sides for your polygon

Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint

Dim g As Graphics

Dim myPts() As Point ' create an array of points

Dim SideAngle As Single

Dim X, Y, i As Integer

Dim p As New Pen(Color.Blue, 1)

ReDim myPts(POLYGONSIDES - 1)

SideAngle = 2 * Math.PI / POLYGONSIDES

For i = 0 To POLYGONSIDES - 1

X = Location_X + Radius * Math.Sin(Direction + i * SideAngle)

Y = Location_Y - Radius * Math.Cos(Direction + i * SideAngle)

myPts(i) = New Point(X, Y)

Next i

g = e.Graphics

g.DrawPolygon(p, myPts)

End Sub

The SideAngle variable in the previous example represents the angle within a cirle that represents the chord created by one side. The SideAngle is calculated by taking the radian angles for a circle (2*PI) and dividing it by the number of sides. Radius holds the length of the radius of the circle in which the polygon is inscribed. The Direction variable represents the base angle for the first point on the polygon. The subsequent points are found by adding a multiple of the unit angle (SideAngle).

Drawing a rectangle of varying width and height is going to be a little more complicated than the equilateral triangle.

The following code is from a rectangle class that I created. To draw the rectangle, I first find the center by dividing Width and Height by 2.

Public Sub DrawRect(ByVal g As Graphics)

Dim RectPts(3) As Point

Dim Theta As Single

Dim HalfWid, HalfHgt As Integer

HalfWid = Width / 2

HalfHgt = Height / 2

Rad = Math.Sqrt(HalfWid * HalfWid + HalfHgt * HalfHgt)

Theta = Math.Acos(HalfWid / Rad)

RectPts(0) = GetPt(Theta)

RectPts(1) = GetPt(Math.PI - Theta)

RectPts(2) = GetPt(Math.PI + Theta)

RectPts(3) = GetPt(-Theta)

g.DrawPolygon(PenColor, RectPts)

End Sub

Private Function GetPt(ByVal Offset As Single) As Point

Dim tAngle As Single

tAngle = Rotation / 180 * Math.PI + Offset

GetPt.X = CenterX + Rad * Math.Cos(tAngle)

GetPt.Y = CenterY + Rad * Math.Sin(tAngle)

End Function

Excuse me. Gotta be somewhere else now. Will catch up with the explanation later.

Const POLYGONSIDES As Integer = 4 ' change number of sides for your polygon

Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.Paint

Dim g As Graphics

Dim myPts() As Point ' create an array of points

Dim SideAngle As Single

Dim X, Y, i As Integer

Dim p As New Pen(Color.Blue, 1)

ReDim myPts(POLYGONSIDES - 1)

SideAngle = 2 * Math.PI / POLYGONSIDES

For i = 0 To POLYGONSIDES - 1

X = Location_X + Radius * Math.Sin(Direction + i * SideAngle)

Y = Location_Y - Radius * Math.Cos(Direction + i * SideAngle)

myPts(i) = New Point(X, Y)

Next i

g = e.Graphics

g.DrawPolygon(p, myPts)

End Sub

The SideAngle variable in the previous example represents the angle within a cirle that represents the chord created by one side. The SideAngle is calculated by taking the radian angles for a circle (2*PI) and dividing it by the number of sides. Radius holds the length of the radius of the circle in which the polygon is inscribed. The Direction variable represents the base angle for the first point on the polygon. The subsequent points are found by adding a multiple of the unit angle (SideAngle).

Drawing a rectangle of varying width and height is going to be a little more complicated than the equilateral triangle.

The following code is from a rectangle class that I created. To draw the rectangle, I first find the center by dividing Width and Height by 2.

Public Sub DrawRect(ByVal g As Graphics)

Dim RectPts(3) As Point

Dim Theta As Single

Dim HalfWid, HalfHgt As Integer

HalfWid = Width / 2

HalfHgt = Height / 2

Rad = Math.Sqrt(HalfWid * HalfWid + HalfHgt * HalfHgt)

Theta = Math.Acos(HalfWid / Rad)

RectPts(0) = GetPt(Theta)

RectPts(1) = GetPt(Math.PI - Theta)

RectPts(2) = GetPt(Math.PI + Theta)

RectPts(3) = GetPt(-Theta)

g.DrawPolygon(PenColor, RectPts)

End Sub

Private Function GetPt(ByVal Offset As Single) As Point

Dim tAngle As Single

tAngle = Rotation / 180 * Math.PI + Offset

GetPt.X = CenterX + Rad * Math.Cos(tAngle)

GetPt.Y = CenterY + Rad * Math.Sin(tAngle)

End Function

Excuse me. Gotta be somewhere else now. Will catch up with the explanation later.