【问题标题】:How to draw circular arcs in VB.NET如何在 VB.NET 中绘制圆弧
【发布时间】:2013-11-05 10:47:15
【问题描述】:

我必须根据用户输入的值生成下图。给定它们的起点和终点(分别为 B 和 F)以及距 BF 段的高度,我该如何绘制弧线(如图中的 B-C-F,本质上是圆形)?我可以进行一些几何计算并获得半径和所有值,但是如何绘制圆弧?

我尝试过使用Graphics.DrawCurve() 方法,但它没有按预期工作。我怎样才能使这种方法适用于圆弧?也欢迎任何其他解决方法。

【问题讨论】:

  • Graphics.DrawArc 出了什么问题? msdn.microsoft.com/en-us/library/…
  • 如果您已经计算了生成曲线所需的半径,那么只需使用 Graphics.DrawEllipse() 绘制整个圆,但使用 Graphics.SetClip() 并使用点 B 和 F 传递一个矩形作为边并使用高度 C 计算其他两个点。这会将整个圆剪裁到该矩形内可见的部分。然后调用 Graphics.ResetClip() 并绘制其余的线条。重复 SetClip() 技巧以在底部绘制曲线。
  • @Idle_Mind 您能否发表评论作为答案。 @Mitch 你能稍微描述一下我如何在我的情况下使用DrawArc 吗? PS - 我是 VB.NET 的新手

标签: vb.net geometric-arc


【解决方案1】:

来自我的评论:

如果您已经计算出生成曲线所需的半径,那么 只需使用 Graphics.DrawEllipse() 绘制整个圆,但使用 Graphics.SetClip() 并使用点 B 和 F 传递一个矩形作为 边并使用高度 C 计算其他两个点。这将 将整个圆圈剪辑到该矩形内可见的部分。 然后调用 Graphics.ResetClip() 并绘制其余的线条。重复 SetClip() 技巧也在底部绘制曲线。

这是通过 B、C 和 F 的顶部曲线的概念验证。

我使用了 Donna Roberts 在Investigative Circle Activity Using Three Points 提供的公式。

截图如下:

...以及产生它的代码:

Public Class Form1

    Private B As New Point(50, 100)
    Private F As New Point(250, 100)
    Private DistanceFromBF As Integer = 50

    Private Sub Form1_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        If B.Y = F.Y Then
            Dim C As New Point(B.X + (F.X - B.X) / 2, B.Y - DistanceFromBF)
            Dim ctr As Point
            Dim rad As Double
            CircleFromPointsOnCircumference(B, C, F, ctr, rad)

            Dim rc As New Rectangle(ctr, New Size(1, 1))
            rc.Inflate(rad, rad)
            e.Graphics.DrawRectangle(Pens.Black, rc)

            Dim clip As New Rectangle(New Point(B.X, B.Y - DistanceFromBF), New Size(F.X - B.X, DistanceFromBF))
            e.Graphics.SetClip(clip)
            e.Graphics.DrawEllipse(Pens.Green, rc)

            e.Graphics.ResetClip()
            DrawPoint(B, e.Graphics, Color.Red)
            DrawPoint(C, e.Graphics, Color.Red)
            DrawPoint(F, e.Graphics, Color.Red)
            DrawPoint(ctr, e.Graphics, Color.Green)
        End If
    End Sub

    Private Sub DrawPoint(ByVal pt As Point, ByVal G As Graphics, ByVal clr As Color)
        Dim rc As New Rectangle(pt, New Size(1, 1))
        rc.Inflate(3, 3)
        Using brsh As New SolidBrush(clr)
            G.FillEllipse(brsh, rc)
        End Using
    End Sub

    Private Sub CircleFromPointsOnCircumference(ByVal ptA As Point, ByVal ptB As Point, ByVal ptC As Point, ByRef Center As Point, ByRef Radius As Double)
        Dim mR As Double = CDbl(ptA.Y - ptB.Y) / CDbl(ptA.X - ptB.X)
        Dim mT As Double = CDbl(ptC.Y - ptB.Y) / CDbl(ptC.X - ptB.X)
        Dim X As Double = (mR * mT * (ptC.Y - ptA.Y) + mR * (ptB.X + ptC.X) - mT * (ptA.X + ptB.X)) / CDbl(2) * (mR - mT)
        Dim Y As Double = CDbl(-1) / mR * (X - CDbl(ptA.X + ptB.X) / CDbl(2)) + (CDbl(ptA.Y + ptB.Y) / CDbl(2))
        Center = New Point(X, Y)
        Radius = Math.Sqrt(Math.Pow(ptA.X - Center.X, 2) + Math.Pow(ptA.Y - Center.Y, 2))
    End Sub

End Class

【讨论】:

  • 感谢您的努力,尽管DrawArc 方法对我来说要简单得多。谢谢!
【解决方案2】:

知道了!谢谢@Mitch & @Idle_Mind

使用Graphics的内置DrawArc方法

Friend Function draw_tank() As Boolean

    ' Create pen. 
    Dim blackPen As New Pen(Color.Black, 3)

    ' Create rectangle to bound ellipse. 
    Dim rect As New Rectangle(100, 100, 200, 200)
    ' Keeping the width & length same (200) we get a circle

    ' Create start and sweep angles on ellipse. 
    Dim startAngle As Single = 225.0F
    Dim sweepAngle As Single = 90.0F

    ' Draw arc to screen.
    Dim myarc As Graphics = Me.CreateGraphics
    myarc.DrawArc(blackPen, rect, startAngle, sweepAngle)

    Return True
End Function

欢迎提出建议/改进。

注意 - 这不是我的代码中的实际功能。

【讨论】:

  • 你不应该使用CreateGraphics()。相反,您的绘图应该在表单的 OnPaint 覆盖中。
  • 您能否提供一些相同的学习资源链接?为什么它可以/更好?我是 VB.NET 的新手
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-20
相关资源
最近更新 更多