Построить правильный n-угольник вписанный в окружность - Visual Basic .NET

Узнай цену своей работы

Формулировка задачи:

Подскажите, как построить правильный n- угольник вписанный в окружность ?

Решение задачи: «Построить правильный n-угольник вписанный в окружность»

textual
Листинг программы
Option Strict On
 
Public Class Form1
    Friend TextBox1 As TextBox
    Friend TextBox2 As TextBox
    Friend PolygonInCircle1 As PolygonInCircle
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Size = New Size(384, 384)
 
        TextBox1 = New TextBox With {.Location = New Point(10, 10), .Tag = "Radius"}
        AddHandler TextBox1.KeyDown, AddressOf TextBoxs_KeyDown
 
        TextBox2 = New TextBox With {.Location = New Point(10, 40), .Tag = "NumberAngles"}
        AddHandler TextBox2.KeyDown, AddressOf TextBoxs_KeyDown
 
        PolygonInCircle1 = New PolygonInCircle With {.Location = New Point(10, 70), _
                                                     .Size = New Size(256, 256), _
                                                     .BackColor = Color.FromArgb(&HFFC0FFC0), _
                                                     .CircleColor = Color.Red, _
                                                     .PolygonColor = Color.Blue}
 
        Me.Controls.AddRange(New Control() {TextBox1, TextBox2, PolygonInCircle1})
    End Sub
 
    Private Sub TextBoxs_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        If e.KeyData = Keys.Enter Or e.KeyData = Keys.Tab Then
            Dim tbSender As TextBox = DirectCast(sender, TextBox)
            Dim value As String = tbSender.Text
 
            If IsNumeric(value) Then
                Select Case tbSender.Tag.ToString
                    Case "Radius"
                        PolygonInCircle1.Radius = CType(value, Integer)
 
                    Case "NumberAngles"
                        PolygonInCircle1.NumberAngles = CType(value, Integer)
 
                End Select
 
            End If
        End If
    End Sub
End Class
 
Public Class PolygonInCircle
    Inherits UserControl
 
    Private _NumberAngles As Integer
    Public Property NumberAngles As Integer
        Get
            Return _NumberAngles
        End Get
        Set(ByVal value As Integer)
            _NumberAngles = value
            Me.Refresh()
        End Set
    End Property
 
    Private _Radius As Integer
    Public Property Radius As Integer
        Get
            Return _Radius
        End Get
        Set(ByVal value As Integer)
            _Radius = value
            Me.Refresh()
        End Set
    End Property
 
    Private _CircleColor As Color
    Public Property CircleColor As Color
        Get
            Return _CircleColor
        End Get
        Set(ByVal value As Color)
            _CircleColor = value
            Me.Refresh()
        End Set
    End Property
    Private _PolygonColor As Color
    Public Property PolygonColor As Color
        Get
            Return _PolygonColor
        End Get
        Set(ByVal value As Color)
            _PolygonColor = value
            Me.Refresh()
        End Set
    End Property
 
    Sub New()
        Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
        Me.SetStyle(ControlStyles.UserPaint, True)
        Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
    End Sub
 
    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        MyBase.OnPaint(e)
 
        e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
 
        If _NumberAngles < 3 Or (_Radius = 0 Or (_Radius + 5) * 2 > Math.Min(Me.Width, Me.Height)) Then
            Dim p As New Pen(Color.Red, 3)
            p.Alignment = Drawing2D.PenAlignment.Center
            e.Graphics.DrawLine(p, _
                                New Point(0, 0), _
                                New Point(Me.ClientRectangle.Width, Me.ClientRectangle.Height))
            e.Graphics.DrawLine(p, _
                                New Point(Me.ClientRectangle.Width, 0), _
                                New Point(0, Me.ClientRectangle.Height))
            p.Dispose()
            Exit Sub
 
        Else
            Dim Angles As New List(Of PointF)
 
            Dim rad As Double
            For n As Integer = 0 To 360 Step 360 \ _NumberAngles
                rad = (n - 90) * Math.PI / 180
                Angles.Add(New PointF(CSng(_Radius * Math.Cos(rad)), CSng(_Radius * Math.Sin(rad))))
            Next
 
            e.Graphics.TranslateTransform(Me.Width \ 2, Me.Height \ 2)
 
            e.Graphics.DrawPolygon(New Pen(_PolygonColor, 1), Angles.ToArray)
            e.Graphics.DrawEllipse(New Pen(_CircleColor, 1), New Rectangle(-_Radius, -_Radius, _Radius * 2, _Radius * 2))
        End If
    End Sub
End Class

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

13   голосов , оценка 4.077 из 5
Похожие ответы