?
Solved

Drawing Arrows

Posted on 1998-01-29
8
Medium Priority
?
588 Views
Last Modified: 2012-06-22
I would like to know if there are any routines that, given
a line (x1,y1)-(x2,y2), will automatically draw an arrow-
head on it pointing in the right direction.
0
Comment
Question by:SimonWilliams
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 5

Expert Comment

by:dirtdart
ID: 1454061
There's no built in function for drawing arrows, but the following function will do it.

Private Function Arrow(x1 As Long, y1 As Long, x2 As Long, y2 As Long, Direction As Long) As Long
Dim xdiff As Long

xdiff = x2 - x1
xdiff = xdiff / 5

Form1.Line (x1, y1)-(x2, y2)
If Direction = 1 Then
    x1 = x2 - xdiff
    y1 = y2 - xdiff
    Form1.Line (x2, y2)-(x1, y1)
    y1 = y1 + (xdiff * 2)
    Form1.Line (x2, y2)-(x1, y1)
Else
    x2 = x1 + xdiff
    y2 = y1 - xdiff
    Form1.Line (x1, y1)-(x2, y2)
    y2 = y2 + (xdiff * 2)
    Form1.Line (x1, y1)-(x2, y2)
End If

End Function

Just pass it the four endpoints of the line, and the direction you want the arrow to point.  1 is right and 2 is left.  It could be expanded to also include up and down, and probably diagonals also.
0
 

Author Comment

by:SimonWilliams
ID: 1454062
It should be possible to draw the arrow in ANY direction, at
ANY angle. There should be no need to pass a direction value
to the function as the direction is determined by the position
of the start and end points of the line. Thanks for trying though:)
0
 
LVL 4

Expert Comment

by:rantanen
ID: 1454063
Following subroutine draws an arrow head at the line end point. The sample is written to draw an arrow formed by two line sections, but you can modify it to draw with an arrow style you like. It assumes the usage of a type

Private Type LineType
    xStart As Integer
    yStart As Integer
    xEnd As Integer
    yEnd As Integer
End Type

as the argument.

Private Sub DrawLineWithArrow(MyLine As LineType)
    Dim xTemp As Single
    Dim yTemp As Single
   
    ' Rotation coefficients
    Dim sCos As Single
    Dim sSin As Single
    Dim xDiff As Integer
    Dim yDiff As Integer
   
    ' Arrow coordinates
    Dim xTip As Single
    Dim yTip As Single
    Dim xCorner1 As Single
    Dim yCorner1 As Single
    Dim xCorner2 As Single
    Dim yCorner2 As Single
   
    ' Arrow dimensions. Set them to comply with your scale.
    Const xOffset = 5 'Lenght
    Const yOffset = 2 'Width
   
    With MyLine
        ' Calculate rotation coefficients
        xDiff = .xEnd - .xStart
        yDiff = .yEnd - .yStart
        sCos = xDiff / Sqr(xDiff * xDiff + yDiff * yDiff)
        sSin = Sqr(1 - sCos * sCos)
        If yDiff < 0 Then
            sSin = -Abs(sSin)
        End If
        If xDiff < 0 Then
            sCos = -Abs(sCos)
        End If
       
        ' Calculate arrow coordinates assuming horizontally
        ' positioned arrow at the line end point
        xTip = .xEnd
        yTip = .yEnd
        ' Translate origo to the the arrow tip
        xCorner1 = -xOffset '.xEnd - xOffset - xTip etc
        yCorner1 = yOffset
        xCorner2 = -xOffset
        yCorner2 = -yOffset
        ' Rotate and translate points
        xTemp = xCorner1 * sCos - yCorner1 * sSin
        yTemp = xCorner1 * sSin + yCorner1 * sCos
        xCorner1 = xTemp + xTip ' translate to original coordinates
        yCorner1 = yTemp + yTip
       
        xTemp = xCorner2 * sCos - yCorner2 * sSin
        yTemp = xCorner2 * sSin + yCorner2 * sCos
        xCorner2 = xTemp + xTip ' translate to original coordinates
        yCorner2 = yTemp + yTip
       
        'Draw line
        Picture1.Line (.xStart, .yStart)-(.xEnd, .yEnd)
        ' Draw arrow
        Picture1.Line (xTip, yTip)-(xCorner1, yCorner1)
        Picture1.Line (xTip, yTip)-(xCorner2, yCorner2)
    End With
End Sub

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:dirtdart
ID: 1454064
That's the only thing I couldn't come up with is the formula to calculate the endpoints from the given angle.  Been too many years since Trig.
0
 

Author Comment

by:SimonWilliams
ID: 1454065
I'm afraid the solution doesn't work. The line:

sCos = xDiff / Sqr(xDiff * xDiff + yDiff * yDiff)

will produce an overflow error for all but the tiniest of lines, and even for the tiny lines, I see no arrow head.

I do appreciate the effort made in writing this answer however.
0
 
LVL 4

Accepted Solution

by:
rantanen earned 600 total points
ID: 1454066
That code works.

If you get overflow, you probably have your scalemode set to twips or pixels (you also say you don't see any arrow heads - same reason). I'm sorry I didn't mention that, I took the code directly (simplified) from an app where I have user defined scalemode.

The cure is to change all the variable declares having type single to type double. And to see the arrow heads, if your scalemode is eg. pixels, try

Const xOffset = 10 'Lenght
Const yOffset = 4 'Width

If your scalemode is twips then try

Const xOffset = 150 'Lenght
Const yOffset = 60 'Width
0
 
LVL 4

Expert Comment

by:rantanen
ID: 1454067
Ooops .....

I meant to write, change all variable declarations having type integer to type single or double.

Sorry about that.
0
 

Author Comment

by:SimonWilliams
ID: 1454068
Great! It works perfectly now. Thankyou very much :)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses
Course of the Month11 days, 16 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question