jingwood / d2dlib

A .NET library for hardware-accelerated, high performance, immediate mode rendering via Direct2D.
MIT License
245 stars 43 forks source link

Line arrows? #45

Closed BergChristian closed 3 years ago

BergChristian commented 3 years ago

Hi again!

Testing extensively and ran into a question.

Draw arrows at the end or beginning of a line? Is that supported or do I have to draw them myself as triangles?

I am still amazed how easy this library is to use to get crazy much more performance. Thanks for creating it!

Christian

BergChristian commented 3 years ago

Sharing my solution if there is non built in to draw arrow heads at the end of a line. (here is the link to the author of it: https://stackoverflow.com/questions/43527894/drawing-arrowheads-which-follow-the-direction-of-the-line-in-pygame/43529178)

Private Sub DrawArrow(g As D2DGraphics, startp As D2DPoint, endp As D2DPoint) Dim dX As Single = endp.x - startp.x Dim dY As Single = endp.y - startp.y

    '//vector length 
    Dim vLen As Single = Math.Sqrt(dX * dX + dY * dY) ' // use Hypot If available

    '//normalized direction vector components
    Dim udX As Single = dX / vLen
    Dim udY As Single = dY / vLen

    '//perpendicular vector
    Dim perpX As Single = -udY
    Dim perpY As Single = udX

    '//points forming arrowhead
    '//with length L And half-width H
    'arrowend = (end) 

    Dim L As Single = 20
    Dim H As Single = 10

    Dim leftX As Single = endp.x - L * udX + H * perpX
    Dim leftY As Single = endp.y - L * udY + H * perpY

    Dim rightX As Single = endp.x - L * udX - H * perpX
    Dim rightY As Single = endp.y - L * udY - H * perpY

    Dim po(2) As D2DPoint
    po(0).x = endp.x
    po(0).y = endp.y

    po(1).x = leftX
    po(1).y = leftY

    po(2).x = rightX
    po(2).y = rightY

    Using brh As D2DBrush = g.Device.CreateSolidColorBrush(D2DColor.FromGDIColor(Forecolor))
        g.FillPolygon(po, brh) '(Color.FromArgb(180, RegionColor)))
    End Using

    'g.DrawPolygon(po, D2DColor.FromGDIColor(Forecolor), 1)

End Sub
jingwood commented 3 years ago

Thanks! I would consider adding an API to draw a simple filled-arrow. It can be implemented in low-level coding by using VC++, which might get better performance.

jingwood commented 3 years ago

The arrow has more properties can be customized, so finally I think it might not be a good idea to make the built-in one, maybe it's better to let the user to implement their own...

BergChristian commented 3 years ago

I think the implementation is very fast as it is anyway so not a need to have function in the library. As you say, there are many potential parameters of an arrow head.

jingwood commented 3 years ago

Can be implemented by user code, so close for now.