Could anybody help to solve this? Relative angle between 2 rays(lines) which starts from same point. They could point anywhere 360 clockwise, but angle should be always relative (just inside angle).


Did you try even a cursory search for this? It's pretty standard vector math:

v1 = normalize(end1X - startX, end1Y - startY);
v2 = normalize(end2X - startX, end2Y - startY);

angle = acos(dot(v1, v2)) * 180.0/pi;

This will always give a value from 0 to 180, giving you the smallest positive angle clockwise or counter-clockwise.

In 2D, you can fix a rotation direction like so:

v1perp = (-v1.y, v1.x);

if(dot(v2, v1perp) > 0)
    angle = 360.0 - angle;

This will give a clockwise angle in the range 0...180 if v2 points to the right of v1, or 180...360 if v2 is pointing to the left of v1.

  • 1
    \$\begingroup\$ I know that Google would fill in the blanks, but you should probably at least mention the term "dot product" so that OP has a better idea of what to Google for ;-) Perhaps something like "taking the dot product of two normalized vectors gives the cosine of the angle between them" \$\endgroup\$ – A C Oct 18 '17 at 17:52

The angle between two lines is the angle between direction vectors of the lines.

Here's a formula:

enter image description here

You need to remove the Z param from the equation and then calculate your angle from the cosine.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.