【问题标题】:VB.NET keep line inside circleVB.NET 在圆圈内保持线
【发布时间】:2012-09-27 11:30:34
【问题描述】:

我想把我的线路保持在我的圈子内。

我的代码:

Public Class Form1

    Public Function pt(ByVal x As Integer, ByVal y As Integer, ByVal w As Integer, ByVal h As Integer)

        Dim b As Bitmap = New Bitmap(Me.Width, Me.Height)
        Dim g As Graphics = Graphics.FromImage(b)
        g.DrawEllipse(Pens.Black, New Rectangle(x, y, h, w))
        Me.BackgroundImage = b
        Dim S As New Size
        Dim loc As New Point
        S.Width = 4
        S.Height = 4
        loc.X = x + w / 2 - 1
        loc.Y = y + h / 2 - 1
        Dim ptMove As New Panel
        ptMove.Size = S
        ptMove.Location = loc
        ptMove.BackColor = Color.Aqua
        Dim ptC As New Panel
        ptC.Size = S
        ptC.Location = New Point(loc.X, loc.Y - h / 2)
        ptC.BackColor = Color.Red
        Me.Controls.Add(ptC)
        Me.Controls.Add(ptMove)
        line(1, ptMove, ptC) ' < -- Draw The Line Between Two Point (I want To Keep Ptc Inside The Circle
        Return True

    End Function
    Public Function line(ByVal w As Integer, ByVal pt1 As Panel, ByVal pt2 As Panel)
        Dim b As Bitmap
        b = Me.BackgroundImage
        Dim g As Graphics
        g = Graphics.FromImage(b)
        g.DrawLine(Pens.Black, pt1.Location, pt2.Location)
        Return True

    End Function

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim x, y, w, h As Integer
        x = 200
        y = 40
        w = 100
        h = 100
        pt(x, y, w, h)
    End Sub
End Class

结果:

有两点:

point 1
point 2

点 2:在圆的中心。

点 1:在圆的边缘。

假设我将点 1 移动到新位置(例如 0,0):

g.DrawLine(Pens.Black, new point(0,0) , pt2.Location)

结果:

线在圈外

我想把线保持在我的圈子里。

如何将线(ptC 面板)保持在圆的半径内?

【问题讨论】:

    标签: vb.net winforms


    【解决方案1】:

    您需要创建一个剪切区域或路径并在绘制线条之前应用它:

    Using g = Me.CreateGraphics()
    
        Using clip_path = New Drawing2D.GraphicsPath
            clip_path.AddEllipse(100, 100, 100, 100)
    
            g.DrawPath(Pens.Black, clip_path)
            g.SetClip(clip_path)
    
            g.DrawLine(Pens.Black, 0, 0, 150, 150)
        End Using
    
    End Using
    

    我在这里画了圆和线,你可能想删除对DrawPath的调用。

    【讨论】:

      猜你喜欢
      • 2023-01-30
      • 2011-02-17
      • 1970-01-01
      • 2014-07-13
      • 2012-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-19
      相关资源
      最近更新 更多