79752341

Date: 2025-09-01 09:05:57
Score: 0.5
Natty:
Report link

Good hint by @SmartMarkCoder

Option Explicit On

Public Class Form1
    Dim numberOfSides As Integer = 0
    Const skew As Single = Math.PI * 1.5F ' Corrects the rotation

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi
        'MessageBox.Show(Me.Width & "," & Me.Height)
    End Sub

    Protected Overrides Sub OnPaint(e As PaintEventArgs)
        Dim centerX As Integer = Me.ClientSize.Width * 2 / 3
        Dim centerY As Integer = Me.ClientSize.Height * 1 / 2
        Dim centerPoint As New PointF(centerX, centerY)
        Dim radius As Integer = Me.ClientSize.Width / 4.5
        '
        MyBase.OnPaint(e)
        If numberOfSides < 3 Then Return
        Dim polygon = CreatePolygon(radius, numberOfSides, centerPoint)
        Using blackPen As New Pen(Color.Black, 2)
            e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
            e.Graphics.DrawPolygon(blackPen, polygon)
        End Using
        '
        e.Graphics.FillRectangle(Brushes.Red, centerX, centerY, 2, 2)   ' added to visualise the center point
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
        numberOfSides = CInt(ListBox1.SelectedItem)
        Me.Invalidate()
    End Sub

    Public Function CreatePolygon(radius As Single, sides As Integer, center As PointF) As PointF()
        Dim polygon = New PointF(sides - 1) {}
        Dim angle As Single = 360.0F / sides * CSng(Math.PI / 180.0F)

        For side As Integer = 0 To sides - 1
            polygon(side) = New PointF(
            CSng(center.X + radius * Math.Cos(skew + side * angle)),
            CSng(center.Y + radius * Math.Sin(skew + side * angle)))
        Next
        Return polygon
    End Function

End Class
Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • User mentioned (1): @SmartMarkCoder
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: Florin Home