如何在VB.NET中绘制圆弧

时间:2013-11-04 23:55:22

标签: vb.net geometric-arc

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

enter image description here

我已尝试使用Graphics.DrawCurve()方法,但它无法按预期工作。如何使此方法适用于圆弧?还欢迎任何其他解决方法。

2 个答案:

答案 0 :(得分:4)

来自我的评论:

  

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

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

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

以下是截图:

Partial Circle thru 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

答案 1 :(得分:2)

知道了!谢谢@Mitch& @Idle_Mind

使用DrawArc

的内置Graphics方法
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

建议/改进欢迎。

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