Hossein
Hossein

Reputation: 25944

How to calculate the angle between two points and rotate the third point baste on that?

I have a set of points based on which I want to calculate the next point's location. To give you a concrete idea of what I'm exactly after, consider the following image:

enter image description here

For this image, I have both eyes center points(which are shown as yellow) and I need to calculate the proper location for the green ones.

enter image description here

If the head was not tilted it would be easy as all the points lie on the same straight line. However, without taking the angle, I can't approximate the new points accurately. Suppose the landmarks are as follows:

left_eye = {X=128,Y=121}
right_eye = {X=188,Y=81}

Now for a straight face, I'd do something like this:

auto distance = (right_eye_x - left_eye_x) / 4;
auto left_eye_left_corner = left_eye_x - distance;
auto left_eye_right_corner = left_eye_x + distance;
auto right_eye_left_corner = right_eye_x - distance;
auto right_eye_right_corner = right_eye_x + distance;

and for the Ys I'd use the respective eye's Y location.

However, when the head is tilted, I obviously can't use this method. So I need to calculate the angle between the eyes and then somehow incorporate this into the Y's calculation (and possibly also X's calculation). This is where I'm stuck.

If my memory serves me right, the angle could be calculated like this tan = (Y/X), and thus to get the actual angle(theta) we would use atan. (also explained here)

enter image description here

Now for the angle, I'd write :

auto deltaY = abs(right_eye_Y - left_eye_Y);
auto deltaX = abs(right_eye_X - left_eye_X);
auto angle = atan2(deltaY, deltaX);

Now for getting the new locations we apply the angle :

new_x = distance * cos(angle) 
new_y = distance * sin(angle) 

and since we want this not to be relative to the (0,0) we offset this with a point(x0,y0). I presume this to be left_eye's and right_eye's X and Y coordinates respectively.

This is where I get stuck. For one, unlike the X axis, I do not have any distance for the Y axis of these [green] points and therefore simply doing something like below fails miserably!

auto left_eye_left_corner = distance * cos(angle) - left_eye_x;
auto left_eye_left_corner_y = left_eye_y * sin(angle);
auto left_eye_right_corner = distance * cos(angle) + left_eye_x;
auto left_eye_right_corner_y = left_eye_y * sin(angle);

auto right_eye_left_corner = distance * cos(angle) - right_eye_x;
auto right_eye_left_corner_y = right_eye_y * sin(angle);
auto right_eye_right_corner = distance * cos(angle) + right_eye_x;
auto right_eye_right_corner_y = right_eye_y * sin(angle);

How should I be going about this?

Upvotes: 2

Views: 164

Answers (1)

Beta
Beta

Reputation: 99124

You're making this far too complicated.

Take the two points:

L = {X=128,Y=121}
R = {X=188,Y=81}

And do a little vector arithmetic:

D  = (R-L)/4
LL = L-D
LR = L+D
RL = R-D
RR = R+D

And you're done.

More pedantically (if you don't like vector arithmetic, or don't want to define a Point class),

Dx  = (Rx-Lx)/4
LLx = Lx-Dx
...

and so on, and the same for the y's.

Upvotes: 4

Related Questions