2

All vector

First I want to call this following vector as:

Vector M1 = [O → M1]; Vector C1 = [O → E];

Vector M2 = [A → M2]; Vector C2 = [A → C];

Vector M3 = [B → M3]; Vector C3 = [B → G];

every M vector is main vector or base vector.

My problem is how to find angle between those vector(M to C). to decide which direction did C look compare from M.

e.g.

1.) M1 and C1 direction is right.

2.) M2 and C2 direction is left.

3.) M3 and C3 direction is back.

ps. sorry for my bad English, if you don't understand what I try to say, ask me.

Edited: angle should be in clockwise direction.

Zen3515
  • 99
  • 2
  • 10
  • Yes, I know it was hard, I 've found 2 way to do this but using much performance. Math.toDegrees(Math.atan2(V1.getZ(), V1.getX()) - Math.atan2(V2.getZ(), V2.getX())) and compare a lots more... – Zen3515 May 12 '14 at 15:09
  • 2
    I am not getting it. If a.b = |a||b|cos(theta) then if you have two vectors, find their dot product a.b (sum of products of coordinates), divide by the product of their lengths |a||b|, take Math.acos of the result followed by Math.toDegrees. Or am I getting the question wrong? – Oleg Sklyar May 12 '14 at 15:12
  • I want angle between two vector in clockwise direction. – Zen3515 May 12 '14 at 15:21

1 Answers1

2

Thanks to MvG from Direct way of computing clockwise angle between 2 vectors his solution working perfect.

double dot = C.dot(M);
double det = ((C.getX()*M.getZ()) - (C.getZ()*M.getX()));
double angle = Math.toDegrees(Math.atan2(det, dot));

String movedirection = "";
if(angle < -135 || angle >= 135){
movedirection = "Front";
};
if(angle < 135 && angle >= 45){
movedirection = "right";
};
if(angle < 45 && angle >= -45){
movedirection = "back";
};
if(angle < -45 && angle >= -135){
movedirection = "left";
};
Community
  • 1
  • 1
Zen3515
  • 99
  • 2
  • 10