ElektroStudios
ElektroStudios

Reputation: 20464

How to draw a triangle?

I've inherited the Control class to create a custom user control used to put some shapes, I would like to draw a triangle inside the control counds and I'm drawing an inner border in the triangle but I have messed my head with the pointers, I can't find the right calculation of the points (maths are not my speciality) then instead a triangle I get this ugly figure:

enter image description here

Below is the relevant part of the code that draws the lines, the problem I think is the fourth PointF

Notes

p object is the Pen instance that I use to draw the lines.

The BorderWidth property of the property grid in the image above is just to set the p.Width

Code

Case Figures.Triangle
    Dim trianglePoints As PointF() =
        {
            New PointF(p.Width / 4.0F, CSng(Me.Height) + (p.Width / 2.0F)),
            New PointF(CSng(Me.Width) / 2.0F, p.Width),
            New PointF(CSng(Me.Width) - (p.Width / 4.0F), CSng(Me.Height) + (p.Width / 2.0F)),
            New PointF(-CSng(Me.Height) + p.Width, CSng(Me.Width / 2) - p.Width)
        }

    Using gp As New Drawing2D.GraphicsPath(FillMode.Alternate)

        gp.AddLines(trianglePoints)
        gp.CloseFigure()

        With e.Graphics

            If Not Me._InnerColor = Color.Transparent Then

                Using b As New SolidBrush(Me._InnerColor)
                    .FillPath(b, gp)
                End Using

            End If

            .DrawLines(p, trianglePoints)

        End With

    End Using

Upvotes: 0

Views: 1279

Answers (1)

Hamlet Hakobyan
Hamlet Hakobyan

Reputation: 33381

The last element of the array must be same as first (to close the figure)

Dim trianglePoints As PointF() =
    {
        New PointF(p.Width / 2.0F, CSng(Me.Height) - (p.Width / 2.0F)),
        New PointF(CSng(Me.Width) / 2.0F, p.Width / 2F),
        New PointF(CSng(Me.Width) - (p.Width / 2.0F), CSng(Me.Height) - (p.Width / 2.0F)),
        New PointF(p.Width / 2.0F, CSng(Me.Height) - (p.Width / 2.0F))
    }

Upvotes: 2

Related Questions