数学公式
-
ベクトルの大きさ(長さ)
$$ \mid \vec{a} \mid = \sqrt{a_1^2 +a_2^2} $$ -
内積の定義
$$ \vec{a} \cdot \vec{b} = \mid \vec{a} \mid \mid \vec{b} \mid \cos \theta $$ -
内積の代数的定義
$$ \vec{a} \cdot \vec{b} = a_1b_1 + a_2b_2 $$ -
ベクトルのなす角
内積の定義より、
$$ \cos \theta = \frac{\vec{a} \cdot \vec{b}}{\mid \vec{a} \mid \mid \vec{b} \mid} = \frac{a_1b_1 + a_2b_2}{\sqrt{a_1^2 +a_2^2}\sqrt{b_1^2 +b_2^2}} $$
$$ \theta = \cos^{-1}\left(\frac{a_1b_1 + a_2b_2}{\sqrt{a_1^2 +a_2^2}\sqrt{b_1^2 +b_2^2}}\right)$$
AutoLISPで求める
;; ベクトルの大きさを計算
(defun vector:Norm (v) (sqrt (apply '+ (mapcar '* v v))))
;; ベクトルの内積を計算
(defun vector:DotProduct (v1 v2) (apply '+ (mapcar '* v1 v2)))
;; アークコサインを計算
(defun math:Acos (r) (if (<= -1. r 1.) (atan (sqrt (- 1. (* r r))) r)))
;; 2つのベクトルのなす角を計算
(defun vector:GetFormedAngle (v1 v2)
(math:Acos
(/
(vector:DotProduct v1 v2)
(* (vector:Norm v1) (vector:Norm v2)))
)
)
;; < Example >
(vector:GetFormedAngle '(3 -1) '(-1 2))
;; -> 2.35619 (135°)
AutoCAD .NET (C#) で求める
Vector2d 構造体を使う。
using Autodesk.AutoCAD.Geometry;
// 2つのベクトルのなす角を計算
public static double GetFormedAngle(Vector2d v1, Vector2d v2) {
return Math.Acos(v1.DotProduct(v2) / (v1.Length * v2.Length));
}
// < Example >
var v1 = new Vector2d(3, -1);
var v2 = new Vector2d(-1, 2);
GetFormedAngle(v1, v2);
// -> 2.35619 (135°)
ObjectARX (C++) で求める
AcGeVector2d クラスを使う。
// 2つのベクトルのなす角を計算
static double getFormedAngle(AcGeVector2d v1, AcGeVector2d v2) {
return acos(v1.dotProduct(v2) / (v1.length() * v2.length()));
}
// < Example >
auto v1 = AcGeVector2d(3, -1);
auto v2 = AcGeVector2d(-1, 2);
getFormedAngle(v1, v2);
// -> 2.35619 (135°)
