Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am working on a texturing algorithm for 3d graphics and I am trying to rotate triangles that are attached at a point so that they are connected (share a single edge) I am using

$\tan^{-1}\left( \frac{m_1 - m_2}{1 + m_1m_2}\right )$ where $m_1 \gt m_2$

This is working well in some test cases but not in all... I have two lines one with slope ~$1$ and ~$(-0.3)$ which gets me

$\tan^{-1}\left(\frac{1.05555556+0.368567038}{1 + (1.05555556)(-0.368567038)}\right) = 1.16553753\space \text{radians} \approx 66^o$

This is correct if the line with slope $-0.3$ is moving down to the right while the line with slope $1$ is moving up to the right however the direction of the $-0.3$ slope is moving up and to the left. Is there a way to parameterize this function so that I can provide the direction the lines moving in? Thanks.

share|cite|improve this question
+1 It is nice to see somebody give some thought to the problem before posting. – Ross Millikan Feb 12 '11 at 16:14
up vote 1 down vote accepted

There are two angles to choose from. One will be 180 degrees minus the other. If you take a vector along each direction and take the dot product, it will be negative when you want the angle greater than 90. So in your example, if both lines are moving to the right $(1,1)\cdot(1,-0.3)=0.7\gt0$ so you want the 66 degree solution. In the second case $(1,1)\cdot(-1,0.3)=-0.7\lt0$ so you want the 180-66=114 degree solution.

share|cite|improve this answer
Should have remembered this stuff from linear algebra :) Thanks a ton! – user6983 Feb 12 '11 at 16:06
Testing this solution in my code seems to produce better results in most test cases however it's not completely correct yet. Are there any other test cases or are these the only two possible outcomes? (It is of course possible my code has a bug in it) but will this work for any lines in any direction? Thanks again. – user6983 Feb 12 '11 at 16:23
Bah disregard that, 180 degrees != pi/2 (too early still) – user6983 Feb 12 '11 at 17:00
Don't forget that if the dot product is zero, the two vectors are perpendicular. Not sure if that was the issue! – fdart17 Feb 12 '11 at 17:02
Haven't come across it yet but I will surely make a note of it. Thanks for all the help guys I've been rattling my brain for days trying to figure this out. – user6983 Feb 12 '11 at 17:07

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.