开发者

Drawing a septagon in vb.net

开发者 https://www.devze.com 2023-02-20 02:57 出处:网络
Does anyone have any code that can draw an equilateral septagon in vb.net? All开发者_运维技巧 sides and angles need to be equal.

Does anyone have any code that can draw an equilateral septagon in vb.net?

All开发者_运维技巧 sides and angles need to be equal.

Thanks


No, but if you think of a clock face with 60 minutes, each 8.5 minutes marks one point of your 7-sided shape.


Here is a function to draw a regular polygon of specified number of sides:

Sub poly(ByVal center As PointF, ByVal radius As Double, ByVal nSides As Integer, ByVal g As Graphics)

Dim pts(nSides) As PointF
Dim Angle As Double = Math.PI * 2 / nSides
Dim i As Integer
Dim a As Double

a = Math.PI / 2 ' first point on top
For i = 0 To UBound(pts)
  pts(i) = center + New Point(radius * Math.Cos(a), -radius * Math.Sin(a))
  a = a + Angle
  Next i

g.DrawPolygon(Pens.DarkGreen, pts)
End Sub

To call it, setup a graphics object where you want it drawn. For example, to draw it in PictureBox1, you could call it like this:

Dim g As Graphics

PictureBox1.Image = New Bitmap(PictureBox1.Width, PictureBox1.Height) ' new bitmap
g = Graphics.FromImage(PictureBox1.Image) ' assign graphics object to g
g.FillRectangle(Brushes.White, 0, 0, PictureBox1.Width, PictureBox1.Height) ' white background
' draw 7-sided polygon in the center of the picturebox
poly(New PointF(PictureBox1.Width / 2, PictureBox1.Height / 2), PictureBox1.Height / 3, 7, g)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号