在控件内绘制比例三角形

时间:2014-11-09 08:46:32

标签: c# .net vb.net math geometry

数学不是我的专长,因为这是一个数学问题,而不是VB问题,我也用C#标签标记它。

我需要帮助在自定义用户控件的工作区域(客户端矩形)内绘制一个三角形,但是我无法设置正确的坐标(我未能尝试计算正确的坐标,使用Me.Left,Me.Right进行操作, Me.Top和Me.Bottom ...),这里是绘制矩形的相关代码:但是无论如何我不确定我是否使用了野兽方法(因为控件' s客户矩形区域。)

    Dim ptsArray As PointF() =
        {
            New PointF(0, 0),
            New PointF(0, 0),
            New PointF(0, 0),
            New PointF(0, 0)
        }

    Dim gp As New Drawing2D.GraphicsPath(Drawing2D.FillMode.Alternate)
    gp.AddLines(ptsArray)
    gp.CloseFigure()

    e.Graphics.FillPath(Brushes.Red, gp)
    e.Graphics.DrawLines(Pens.Black, ptsArray)

如果这是我的控制:

enter image description here

矩形结果应该是这样的,因为你会看到矩形尊重控件的比例/大小:

enter image description here

1 个答案:

答案 0 :(得分:1)

这是一个如何绘制三角形的示例。请注意,您还需要将笔的宽度放入方程式中。此外,您需要绘制path,而不是线条。

pt1:顶部中心,pt2:底部 - 右侧,pt3:左下角。

Using pen As New Pen(Brushes.Red, 10)

    Dim rect As Rectangle = Me.ClientRectangle
    Dim pt1 As New PointF(CSng(rect.Left + (rect.Width / 2)), (rect.Top + pen.Width))
    Dim pt2 As New PointF((rect.Right - pen.Width), (rect.Bottom - pen.Width))
    Dim pt3 As New PointF((rect.Left + pen.Width), (rect.Bottom - pen.Width))

    Using path As New Drawing2D.GraphicsPath(FillMode.Winding)
        path.AddLines({pt1, pt2, pt3, pt1})
        path.CloseFigure()
        e.Graphics.DrawPath(pen, path)
    End Using

End Using