6

兩向量的夾角角度

 3 years ago
source link: https://naclyen.com/2009/03/03/%e5%85%a9%e5%90%91%e9%87%8f%e7%9a%84%e5%a4%be%e8%a7%92%e8%a7%92%e5%ba%a6/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
兩向量的夾角角度 – NaCl's Blog

「真不該把數學還給老師的!」
這句話自大學以來已經悔恨不知多少次了…
雖然是高中生的一個簡單的題目,終究還是還給老師,還是筆記下來比較實在XDD

  • 向量內積 (dot product)
    A dot B = |A| |B| cos(θ)
  • θ = Acos( (A dot B) / (|A| |B|) )
 // C# with XNA
 Vector3 A = new Vector3(1, 2, 3);
 Vector3 B = new Vector3(4, 5, 6);
 float A_dot_B = Vector3.Dot(A, B);
 float ALen_mul_BLen = A.Length() * B.Length();

 // 基本上A & B皆不應該為零,就略過檢查分母為零的狀況
 double theta = Math.Acos(A_dot_B / ALen_mul_BLen);

 // 這裡計算出來的theta是弧度(單位為π),可依照需求轉成度數
 // double degree = theta * 180 / 3.14;
 double degree = MathHelper.ToDegrees((float)theta);

 // C# with XNA,將向量化的動作提前,結果是一樣的…吧~
 Vector3 A = new Vector3(1, 2, 3);
 Vector3 B = new Vector3(4, 5, 6);
 A.Normalize();
 B.Normalize();
 double theta = Math.Acos(Vector3.Dot(A, B));

獻給已經將數學還給老師的同志們XDD


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK