Math question: direction calculation

Posted on 2000-04-11
Medium Priority
Last Modified: 2010-04-02
If I have values for east/west, north/south and vertical velocities, how do I calculate the resulting direction taking all of them into account?

Will increase points on suitable answer.
Question by:skanade

Expert Comment

ID: 2706216
how do u want the direction expressed??

Author Comment

ID: 2706246

Expert Comment

ID: 2706249
it's 3d, so degrees from which axis??
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.


Author Comment

ID: 2706370
Frankly, I don't know. But, I use a program that shows the value already. I don't have the code. I suppose, if I give you three values and a result, will you be able to find it out? I guess so. Please give me some time to give you the values.


Accepted Solution

pitonyak earned 400 total points
ID: 2706439

Are you given information such as...

3 m/sec East
4 m/sec North

If so, the combined velocity will be given by a right triangle with the east leg a length of 3 and the north leg of length 4. If you have a right triangle you can use things such as:

Leg1^2 + Leg2^2 = Hypotenuse^2

In our case, this yields...

9 + 16 = 25 so the real velocity is 5 m/sec (the square root of 25)...

So, assume that you are given two velocities v1 and v2, you can determine the actual velocity as

v = sqrt(v1*v1 + v2*v2);

Now here is where the fun begins.... You want to know in which direction you are really going... I assume that you want an angle. If this is the case then you should note a few things...

1) Your compiler probably deals in radians and you probably think in degrees so if you say that you went turned 90 degrees to the left, your computer would probably say that you turned about 1.5707963268 rads to your left. I will state the problem in degrees since this is how I think.

This said, I will assume that you will use trigonometry to solve your problem.

I will assume

pi = 3.14159265359
East = 0 degrees = 0 radians
North = 90 degrees = pi/2 radians
West = 180 degrees = pi radians
South = 270 degrees = 3*pi/2 radians

In general, if I want to know the angle, assume that I am traveling

3 m/sec East
4 m/sec North
5 m/sec North-East

The real direction x is given using trig...

sin(x) = opposite/hyp = 4/5 = .8
cos(x) = adjacent/hyp = 3/5 = .6
tan(x) = opposite/adjacent = 4/3 = 1.33333

I can then have that

atan(1.3333) = acos(.6) = asin(.8) = 53.13 degrees = 0.927295218002 radians.

Note that if you have an answer in radians and you want to convert it to degrees, you can simply multiply by 180 and divide by pi.

Note that this will only hold (without doing fancy stuff) if you stick with right triangles (triangles with a 90 degree angle in them).

How you interpret the directions is based soley upon the right triangle that you draw and how you interpret it.

I will assume now that
3 m/s West = -3 m/s East
4 m/s South = -4 m/s North

I will draw ALL of my triangles off of the East/West axis so...

double east;
double north;
double v = sqrt(east*east + north*north); // velocity
double pi2 = asin(1); // note that asin(1) = pi/2 or 90 degrees
double angle_off_axis = asin(fabs(east) / v) * 90 / pi2; // converted to degrees
double angle;
if (east >= 0 && north >= 0)
    angle = angle_off_axis;
else if (east < 0 && north >= 0)
    angle = 180 - angle_off_axis;
else if (east < 0 && north < 0)
    angle = 180 + angle_off_axis;
    angle = 360 - angle_off_axis;



Author Comment

ID: 2706780
Here are the sample values and results.

E/W: -145 mm/s
N/S: -1334 mm/s
Vertical: -27 mm/s

Magnitude: 1342 mm/s
Direction: 186.2 degrees

E/W: -133 mm/s
N/S: -1321 mm/s
Vertical: -20 mm/s

Magnitude: 1328 mm/s
Direction: 185.7 degrees

Author Comment

ID: 2706785

I will need time to understand your comment as it is a little complicated for me.


Expert Comment

ID: 2707191
pitonyak, I'm afraid you forgot the vertical velocity.
here,(simply) are the co-ordinates you need are:

Magnitude = sqrt( E/W^2+N/S^2 + Vertical^2)

(in every dimension, 3 in our case, the magnitude is the square root of sum of squares of all the directions)

Now, for direction, you only needed the horizontal direction so:
Direction=arctan (absolute(E/W)/absolute(N/S))
all you need to determine, is in which quarter you are in, in order to compute the real direction:

   180 - Direction  |   Direction
        -------------------------- N/S
    180+Direction   |  360-Direction



Author Comment

ID: 2708585
Thanks! I need time to check this. I will reply tomorrow.

Author Comment

ID: 2718319
Sorry about that, but the previous comment already answered the main question in great detail.

Author Comment

ID: 2718321
Adjusted points from 50 to 100

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

599 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