Author Topic: Finding an angle between two vectors. (cheating on math homework)  (Read 3523 times)

                          |  v . w |         
        cos theta =______                 
                         ||v|| ||w||

Where V and W are vectors. Vector V<%a,%b> and Vector W<%c,%d>. The top part of the equation is called Dot Product in which (%a*%c) + (%b*%d)..... X values multiplied together added to the Y values multiplied together. The bottum part is the magnitudes of the vectors multiplied together which can be found by pythagorean theorem. Find the absolute value of the entire equation. Flip it to ArcCosine and BOOM! you got yourself an angle. Just thought I would explain the equation to anyone who didn't already know it.

So what I need is a simple function Angles(%a,%b,%c,%d)

Here is what I am thinking

function Angles(%a,%b,%c,%d)
{
 %dot1=%a*%c;
 %dot2=%b*%d;
 %product=%dot1+%dot2;
 %dotproduct=mAbs(%product);
 %asquared=mPow(%a,2);
 %bsquared=mPow(%b,2);
 %magvector1=mSqrt(%asquared*%bsquared);
 %csquared=mPow(%c,2);
 %dsquared=mPow(%d,2);
 %magvector2=mSqrt(%csquared*%dsquared);
 %bottumpart=%magvector1*%magvector2;
 %runningoutofvariablenames=%dotproduct/%bottumpart;
 %theta=mAcos(%runningoutofvariablenames);
 %theta1=%theta*180/3.14159265;
 echo(%theta1);
}

I keep getting 0 degrees on all angles. HELP D:

heh, sounds like me, making BL do my math for me
not sure why you're getting 0 though

Completely untested, but try something like this:

function cheating(%x1,%y1,%x2,%y2)
{
   %v1 = %x1 SPC %y1 SPC 0;
   %v2 = %x2 SPC %y2 SPC 0;
  
   %l1 = vectorLen(%v1);
   %l2 = vectorLen(%v2);
  
   %dot = vectorDot(%v1, %v2);
   %cos = %dot / (%l1 * %l2);
  
   %angle = mACos(%cos) * 180 / 3.14159 ;
   echo(%angle);
}


There's automatic functions to do most of these.

For your specific code, it may be the line mSqrt(%asquared*%bsquared) - for Pythagoras you add the two other squares, not multiply them.

Yeah your script works, thanks. I pretty much just wrote it into my calculator amazingly easier :D.
Except I don't understand this part
   %v1 = %x1 SPC %y1 SPC 0;
   %v2 = %x2 SPC %y2 SPC 0;
Do you mind explaining that to me?
« Last Edit: December 02, 2010, 06:23:34 PM by Jetpuff »

Yeah your script works, thanks. I pretty much just wrote it into my calculator amazingly easier :D.
Except I don't understand this part
   %v1 = %x1 SPC %y1 SPC 0;
   %v2 = %x2 SPC %y2 SPC 0;
Do you mind explaining that to me?
%v1 and %v2 are vectors, a vector is basically three numbers separated by two spaces, like this:
"5 6 0"
The "SPC" means "combine these two string values and put a space between them".

Well, about learning in school. Teachers generally don't care what answer you got, but if you understand how you got it.