hello , I need to know how to get the direction from point A to point B

eg. point_direction(0 , 0 , 100,100); would equal 315
x1 y1 x2 y2

how do I compute this ?

Thanks !

Dani AI

Generated

There are two separate things to get right: the trig routine and the coordinate/angle convention. was pointing you toward trig, computed the angle in standard mathematical coordinates (which gives 45° for (0,0)->(100,100)), and correctly noted that many graphics APIs use a top-left origin (Y increases downward), which flips the sign and can turn 45° into 315°. Use a robust routine (atan2) and explicitly pick the convention you want.

#include <cmath>
#include <iostream>

const double PI = 3.14159265358979323846;

double direction_deg(double x1, double y1, double x2, double y2)
{
    double dx = x2 - x1;
    double dy = y1 - y2;   // note: invert Y for typical screen coords where +Y is down
    double rad = std::atan2(dy, dx);    // handles quadrants and dx==0 safely
    double deg = rad * 180.0 / PI;
    if (deg < 0.0) deg += 360.0;        // normalize to [0,360)
    return deg;
}

int main()
{
    std::cout << direction_deg(0,0,100,100) << '\n'; // prints 315 with the Y-inversion above
}

Notes and quick conversions:

  • If you want standard math angles (0° = +X, counter-clockwise), compute atan2(y2 - y1, x2 - x1) and normalize.
  • To convert a math angle to a clockwise-from-right value: angle_cw = fmod(360.0 - angle_math, 360.0).
  • To make 0° point up, rotate by 90° (add/subtract 90 and re-normalize).

Troubleshooting tips: cast to double to avoid integer division, prefer atan2 over atan to get correct quadrants and avoid divide-by-zero, and be aware that some platforms don’t define M_PI (use your own PI constant). This approach keeps results predictable across different engines and matches the 315° expectation once the screen Y direction is accounted for.

Recommended Answers

All 6 Replies

Are you looking for the bearing? You would need to use the inverse trig functions found in the <cmath> header then convert the answers from radians to degrees.

Are you looking for the bearing? You would need to use the inverse trig functions found in the <cmath> header then convert the answers from radians to degrees.

what would the code be to this ?

#include <cmath>
#include <iostream>

int main()
{
  double x1 = 0;
  double y1 = 0;
  double x2 = 100;
  double y2 = 100;
  
  std::cout << 180./3.14159 * atan((x2-x1)/(y2-y1)) << std::endl;
  
  return 0;
}

The answer is 45 not 315 going from (0,0), to (100,100), right?

Dave

Just look up the distance formula.

#include <cmath>
#include <iostream>

int main()
{
  double x1 = 0;
  double y1 = 0;
  double x2 = 100;
  double y2 = 100;
  
  std::cout << 180./3.14159 * atan((x2-x1)/(y2-y1)) << std::endl;
  
  return 0;
}

The answer is 45 not 315 going from (0,0), to (100,100), right?

Dave

well in graphics everything starts at the top left hand side :icon_smile:

Ah, yes. In mathematics the standard is +x is 0 degrees and the angle increases counter-clockwise. You'll have to adjust accordingly.

Dave

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.