Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleDraw an arrowhead on a line
Keywordsarrow, arrowhead, line, draw
CategoriesGraphics
 
Use the geometry of the line to figure out how to draw the arrowhead. Suppose a line connects points (X1, Y1) and (X2, Y2). Then the vector (where dx = X2 - X1 and dy = Y2 - Y1) points in the direction of the line. The vectors and <-dy, dx> are perpendicular to the line.

The DrawArrow routine divides dx and dy by the length of the vector to get a unit vector (of length 1). It multiplies by an arm length to give the program control over ho long the arrowhead's sides are.

The routine then adds <-dx, -dy> to the perpendicular vectors and <-dy, dx> to get vectors pointing out and backwards, making the arrowhead.

 
Private Sub DrawArrow(ByVal x1 As Single, ByVal y1 As _
    Single, ByVal x2 As Single, ByVal y2 As Single, ByVal _
    arm_length As Single)
Dim dx As Single
Dim dy As Single
Dim length As Single

    Line (x1, y1)-(x2, y2)
    dx = x2 - x1
    dy = y2 - y1
    length = Sqr(dx * dx + dy * dy)
    dx = dx / length * arm_length
    dy = dy / length * arm_length
    Line (x2, y2)-Step(-dx - dy, -dy + dx)
    Line (x2, y2)-Step(-dx + dy, -dy - dx)
End Sub

Private Sub Form_Paint()
    DrawArrow 200, 200, 3000, 2000, 200
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated