Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

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

$\arctan( \frac{m1 - m2}{1 + m1*m2} )$ where $m1 \gt m2$

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

$\arctan(\frac{1.05555556+0.368567038}{1 + 1.05555556*-0.368567038)})$ = 1.16553753 radians = ~66 degrees

This is correct if the line with slope -.3 is moving down to the right while the line with slope 1 is moving up to the right however the direction of the -.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|improve this question
1  
+1 It is nice to see somebody give some thought to the problem before posting. –  Ross Millikan Feb 12 '11 at 16:14

1 Answer 1

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|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

 
discard

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