我必须根据用户输入的值生成下图。给定起点和终点(分别为 B 和 F)以及距 BF 段的高度,如何绘制弧线(如图中的 BCF,本质上是圆形)?我可以做一些几何计算并得到半径和所有,但我如何绘制弧线?
我已经尝试使用该Graphics.DrawCurve()
方法,但它没有按预期工作。我怎样才能使这种方法适用于圆弧?也欢迎任何其他解决方法。
我必须根据用户输入的值生成下图。给定起点和终点(分别为 B 和 F)以及距 BF 段的高度,如何绘制弧线(如图中的 BCF,本质上是圆形)?我可以做一些几何计算并得到半径和所有,但我如何绘制弧线?
我已经尝试使用该Graphics.DrawCurve()
方法,但它没有按预期工作。我怎样才能使这种方法适用于圆弧?也欢迎任何其他解决方法。
从我的评论:
如果您已经计算了生成曲线所需的半径,那么只需使用 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
知道了!谢谢@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
欢迎提出建议/改进。
注意 - 这不是我的代码中的实际功能。