Results 1 to 8 of 8

Thread: Plotting Arrows

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2003
    Posts
    114

    Plotting Arrows

    I need to be pointed in the right direction. In a program that I'm writing I need to draw an arrow. I have a line that can be any random angle. What formula can I use to draw two other lines that begin at an X2,Y2 point and are slanted at a 45 degree angle for instance. The endpoints of the lines that make up the arrow does not matter.

    For example:

    X1=0
    Y1=0
    X2=10
    Y2=10

    How can I plot an arrow off of theese points using a formula?

    Thanks for any help.
    I can do all things with VB.

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Apr 2003
    Posts
    114

    Resolved - Plotting Arrows

    Well, I figured out in a round about way to do it. It's not a formula like I was looking for but for those who are interested, here it is.

    VB Code:
    1. x1=0:y1=0:x2=300:y2=400
    2.  
    3.     xLineDelta = X2 - X1
    4.     yLineDelta = Y2 - Y1
    5.  
    6.     xLineUnitDelta = xLineDelta / Sqr((xLineDelta * xLineDelta) + (yLineDelta * yLineDelta))
    7.     yLineUnitDelta = yLineDelta / Sqr((xLineDelta * xLineDelta) + (yLineDelta * yLineDelta))
    8.  
    9.     '(xBase,yBase) is where arrow line is perpendicular to base of triangle.
    10.     HeadLength = 15 'The Length Of The Arrow Head
    11.     xBase = X2 - Round(HeadLength * xLineUnitDelta)
    12.     yBase = Y2 - Round(HeadLength * yLineUnitDelta)
    13.  
    14.     xNormalDelta = yLineDelta
    15.     yNormalDelta = -xLineDelta
    16.     xNormalUnitDelta = xNormalDelta / Sqr((xNormalDelta * xNormalDelta) + (yNormalDelta * yNormalDelta))
    17.     yNormalUnitDelta = yNormalDelta / Sqr((xNormalDelta * xNormalDelta) + (yNormalDelta * yNormalDelta))
    18.    
    19.  
    20.     '// Draw the arrow tip
    21.     Me.Line (X1, Y1)-(X2, Y2)
    22.  
    23.     'here is one side of the arrow
    24.     'x1=xBase+Round(HeadLength * xNormalUnitDelta)
    25.     'y1=yBase + Round(HeadLength * yNormalUnitDelta)
    26.     Me.Line (xBase + Round(HeadLength * xNormalUnitDelta), yBase + Round(HeadLength * yNormalUnitDelta))-(X2, Y2)
    27.  
    28.     'here is the other side of the arrow
    29.     'x1=xBase+Round(HeadLength * xNormalUnitDelta)
    30.     'y1=yBase + Round(HeadLength * yNormalUnitDelta)
    31.     Me.Line (xBase - Round(HeadLength * xNormalUnitDelta), yBase - Round(HeadLength * yNormalUnitDelta))-(X2, Y2)

    Thanks
    I can do all things with VB.

  3. #3
    Fanatic Member sql_lall's Avatar
    Join Date
    Jul 2002
    Location
    Up Above (i.e. AUS)
    Posts
    571

    Talking Trig.

    OK, when drawing lines it is always a good idea to look at the angle formed between the line and the x-axis.
    this way, if you want a line at T degrees, length L from the x-axis starting at X1,Y1, the line is:
    (X1,Y1)->(X1+R*cos(T), Y1+R*sin(T))

    Then, as you are now wanting two lines 45 degrees off the endpoint, the new lines are at 45+T and 135+T degrees from the X-Axis (or pi/4 +T and 3Pi/4 +T radians)

    You can use the same formula then to draw the new lines, with starting point X2=X1+R*cos(T) and Y2=Y1+R*sin(T)

    Finally, i'm not too sure about what happens with angles with the same tan value (like, 30 and 210 degrees) whether this works fine, or whether some if-statements must be added to see if the angle is < or >=180 degrees.
    sql_lall

  4. #4
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573
    I think this should be correct in all instances.

    Referring to the drawing, (your arrow is blue):



    alpha is 45 deg. in your case, but I have written the general case.

    theta is the angle between the (x1,y1) to (x2,y2) segment and the x axis. In the drawing it is > 180 deg. because the arrow is pointing downwards and to the left.

    The coordinates you want to figure out are (x3,y3) and (x4,y4).

    Then:

    x3 = x2 + cos(Beta2)
    y3 = y2 + sin(Beta2)
    x4 = x2 + cos(Beta1)
    y4 = y2 + sin(Beta1)

    and cos & sin of Beta2 and Beta1 as a function of alpha and theta are calculated by means of elemental trig. formulas:

    Beta2 = theta - (Pi - alpha)
    Beta1 = theta + (Pi - alpha)

    cos(Beta2) = sin(theta)sin(alpha) - cos(theta)cos(alpha)
    sin(Beta2) = -sin(theta)cos(alpha) - cos(theta)sin(alpha)
    cos(Beta1) = -sin(theta)sin(alpha) - cos(theta)cos(alpha)
    sin (Beta1) = -sin(theta)cos(alpha) + cos(theta)sin(alpha)
    Attached Images Attached Images  

  5. #5
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573
    I have written a demo project where I have made a few changes to take user scales into account, otherwise if the vertical and horizontal scales are not the same your arrow could come out quite out of this world.
    Attached Files Attached Files

  6. #6
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573
    Here's a new drawing I've used in relation to the code.

    Attached Images Attached Images  
    Last edited by krtxmrtz; Aug 8th, 2003 at 09:04 AM.

  7. #7
    Fanatic Member
    Join Date
    Jan 2003
    Posts
    1,004
    Couldn't you have an X and Y range?

    If you did then you could find the position by (this is for X):
    (X - Xmin) / (Xmax - Xmin) = decimal percent. of placement inside pic
    where Xmin <= X <= Xmax
    then
    Pic1.left + round( dec. percent * Pic1.Width) = X coordinate

    This way, your graph is scalable.

    NOTE: Please check to make sure I'm right. Also, sorry if I am reiterating anything here. I did not get a chance to look at the attachment.
    "Can't" and "shouldn't" are two totally separate things.

    All questions should be answered. All answers should be true. That is why I post.

  8. #8
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573
    Originally posted by Darkwraith
    Couldn't you have an X and Y range?

    If you did then you could find the position by (this is for X):
    (X - Xmin) / (Xmax - Xmin) = decimal percent. of placement inside pic
    where Xmin <= X <= Xmax
    then
    Pic1.left + round( dec. percent * Pic1.Width) = X coordinate

    This way, your graph is scalable.

    NOTE: Please check to make sure I'm right. Also, sorry if I am reiterating anything here. I did not get a chance to look at the attachment.
    At first glance I'd say this is more or less what I do. See the attachment and take a look at the code.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width