Angle and points math problem

First, I apologize if this should be posted in a math forum.
But I haven't touch math for more than 3 years, now I need to wright a c++ program that needs the angle with 2 points on the screen.
This should be a simple math problem, but I just can't figure it out.

This image illustrates the question:
EDIT: problem solve, image removed.

I also hope the equation for 'r' is correct.
Last edited on
There is a way to get the angle with the positive x-axis, arctangent. From there you should be able to convert it to your liking by doing some wizard-maths involving pi.

1
2
3
4
5
6
7
8
...
#include <cmath>
#define PI 3.14159265
...

...
float angle = atan2(y2-y, x2-x);
...


EDIT:
Reference to the manual page:
http://www.cplusplus.com/reference/clibrary/cmath/atan2/
Last edited on
From you link it looks like what you want is the angle formed between a vertical line intersecting (x1, y1) and the line formed between the two given points. If this is so then you need this instead:

1
2
3
4
5
6
7
8
...
#include <cmath>
#define PI 3.14159265
...

...
float angle = atan2(x2-x, y2-y);
...


This formula will account for negative angles. Try it out.
@Browni3141

After converting to degree and reversing y and y2, that formula worked out great.
Thanks.
atan2 takes y,x not x,y. This is because it calculates atan(y/x) and adjusts to put it in the right quadrant.
Last edited on
Topic archived. No new replies allowed.