使用 VB.NET 的圆心/省略号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20114826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 15:54:11  来源:igfitidea点击:

Center of circle/ellipsis using VB.NET

vb.netgraphicsgeometryellipsisdrawellipse

提问by user3016398

I am attempting to complete a project where, on mouse click, 200 circles are drawn from the clicked location as the center point, growing larger from 1 radius to 200.

我正在尝试完成一个项目,在该项目中,在鼠标单击时,从单击位置作为中心点绘制 200 个圆,从 1 半径增大到 200。

I don't need help with the mouse part of this program.

我不需要这个程序的鼠标部分的帮助。

My current roadblock is that it would seem that although there were circle methods in previous releases of VB, VB.NET only uses the System.CreateGraphics.DrawEllipse method to create circles, and this method uses an x and y coordinate as a starting location for the upper lefthand corner of an invisible rectangle and uses two more integers/singles to decide the distance to the right and down (x + and y +) to determine the size and location of the circle.

我目前的障碍是,虽然 VB 的早期版本中存在圆方法,但 VB.NET 仅使用 System.CreateGraphics.DrawEllipse 方法来创建圆,并且该方法使用 x 和 y 坐标作为开始位置不可见矩形的左上角,并使用另外两个整数/单数来决定向右和向下的距离(x + 和 y +),以确定圆的大小和位置。

I'm hoping I am just epically missing a built in way to create a circle/ellipsis using a center point and declaring the radius from that center point in order to complete my program.

我希望我只是特别缺少一种使用中心点创建圆/省略号并声明该中心点的半径以完成我的程序的内置方法。

Is there a way to draw a circle onto a FORM using a center point as the point of reference then declare the radius?

有没有办法使用中心点作为参考点在 FORM 上绘制一个圆然后声明半径?

回答by Fredrik M?rk

Essentially, the only thing you need to do is to is to decrease the Xand Yvalues of the point with the radius, and use radius * 2 for the width and height:

本质上,您唯一需要做的就是用半径减小点的XY值,并使用半径 * 2 作为宽度和高度:

graphics.DrawEllipse(
    pen, location.X - radius, location.Y - radius, radius * 2, radius * 2)

Given that, it's pretty easy to make an extension method giving you the interface that you are looking for:

鉴于此,很容易制作一个扩展方法,为您提供您正在寻找的界面:

Imports System.Runtime.CompilerServices

Module GraphicsExtensions

    <Extension()>
    Public Sub DrawCircle(ByVal graphics As Graphics, pen As Pen, location As Point, radius As Integer)
        graphics.DrawEllipse(
            pen, location.X - radius, location.Y - radius, radius * 2, radius * 2)
    End Sub

End Module

...and use it in your form:

...并以您的形式使用它:

Dim pos As Point = Me.PointToClient(MousePosition)

Using g As Graphics = Me.CreateGraphics()
    g.DrawCircle(Pens.Black, pos, 15)
End Using

回答by Idle_Mind

I'm hoping I am just epically missing a built in way to create a circle/ellipsis using a center point and declaring the radius from that center point in order to complete my program.

Is there a way to draw a circle onto a FORM using a center point as the point of reference then declare the radius?

我希望我只是特别缺少一种使用中心点创建圆/省略号并声明该中心点的半径以完成我的程序的内置方法。

有没有办法使用中心点作为参考点在 FORM 上绘制一个圆然后声明半径?

Sure, just create a Rectangle() at that point with a size of (1,1) and repeatedly call the Inflate() method:

当然,只需在该点创建一个大小为 (1,1) 的 Rectangle() 并重复调用 Inflate() 方法:

Rectangle.Inflate()

矩形.Inflate()

Public Class Form1

    Private Center As Point
    Private MouseClicked As Boolean = False

    Private Sub Form1_MouseDown(sender As Object, e As MouseEventArgs) Handles MyBase.MouseDown
        If e.Button = Windows.Forms.MouseButtons.Left Then
            MouseClicked = True
            Center = New Point(e.X, e.Y)
            Me.Refresh()
        End If
    End Sub

    Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
        If MouseClicked Then
            Dim rc As New Rectangle(Center, New Size(1, 1))
            For i As Integer = 1 To 200
                e.Graphics.DrawEllipse(Pens.Black, rc)
                rc.Inflate(1, 1)
            Next
        End If
    End Sub

End Class

回答by Tun Zarni Kyaw

If you have the center point and radius, then you can calculate corner point easily. The following might help you.

如果您有中心点和半径,那么您可以轻松计算角点。以下内容可能对您有所帮助。

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    Dim centerX, centerY As Integer
    Dim cornerX, cornerY As Integer
    Dim radius As Integer

    centerX = 100
    centerY = 100
    Dim i As Integer
    For i = 20 To 200 Step 20
        radius = i
        cornerX = centerX - radius / 2
        cornerY = centerY - radius / 2

        e.Graphics.DrawEllipse(Pens.Black, cornerX, cornerY, radius, radius)
    Next

End Sub