简体   繁体   中英

Visual Basic random coordinates

i was trying to write a program that draws circles around the center of the form (creating a larger circle) and i noticed that it isn't really working, the circles are in the wrong coordinates, the following pictures explain what i mean

when the input is 3: 当输入为3时

when the input is 10: 当输入为10时

as you see, the circles aren't uniformed, here is the code:

Dim center As Integer = Convert.ToInt32(Me.Width / 2)
    Dim angels As Integer = 360 / deviceCount
    TextBox1.Text = deviceCount
    Dim i As Integer
    For i = 1 To deviceCount
        e.Graphics.DrawEllipse(Pens.Red, Convert.ToInt32(center + 275 * Math.Cos(i * angels)) - 25, Convert.ToInt32(center + 275 * Math.Sin(i * angels)) - 25, 50, 50)
    Next

*note: the form is 600*600 and deviceCount is the number in the textbox (the number of circles)

thanks in advance!

Edit: 在此输入图像描述

The lazy way.

Private Sub DrawCircles(ByVal Graphics As Graphics, ByVal Number As Integer, ByVal Radius As Integer)

    Dim Center = New Point(Me.ClientSize.Width \ 2, Me.ClientSize.Height \ 2)
    Dim BigRadius = Math.Min(Center.X, Center.Y) - Radius

    Dim CurrentState = Graphics.Save()
    Graphics.ResetTransform()
    Graphics.TranslateTransform(Center.X, Center.Y)
    Graphics.DrawEllipse(Pens.Blue, -BigRadius, -BigRadius, BigRadius * 2, BigRadius * 2)

    For i As Integer = 1 To Number
        Graphics.RotateTransform(360 \ Number)
        Graphics.DrawEllipse(Pens.Red, 0, -BigRadius - Radius, Radius * 2, Radius * 2)
    Next

    Graphics.Restore(CurrentState)
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM