Why not login to Qiita and try out its useful features?

We'll deliver articles that match you.

You can read useful information later.

2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

単位ベクトルを求める

Last updated at Posted at 2019-03-28

単位ベクトル [ unit vector ] ・・・ 長さが1のベクトル


数学公式

  • ベクトルの大きさ(長さ)
    • 平面上のベクトル(2D)
      $$ \mid \vec{a} \mid = \sqrt{a_1^2 +a_2^2} $$
    • 空間のベクトル(3D)
      $$ \mid \vec{a} \mid = \sqrt{a_1^2 +a_2^2 +a_3^2} $$
  • 単位ベクトル
    $$ \vec{e} = \frac{1}{\mid \vec{a} \mid}\vec{a} = \frac{\vec{a}}{\mid \vec{a} \mid}$$
    $$ \vec{a} = (a_1, a_2, a_3) $$
    の単位ベクトルは、
    $$ \left( \frac{a_1}{\sqrt{a_1^2 +a_2^2 +a_3^2}}, \frac{a_2}{\sqrt{a_1^2 +a_2^2 +a_3^2}}, \frac{a_3}{\sqrt{a_1^2 +a_2^2 +a_3^2}} \right) $$

AutoLISPで求める

;; ベクトル -> 単位ベクトル
(defun vector:ToUnit (v)                 
    (   (lambda (n)
			(if (equal 0.0 n 1e-10)
				nil
			    (mapcar '/ v (list n n n))
		    )
	    )
        (distance '(0. 0. 0.) v)
    )
)
;; < Example >
(vector:ToUnit '(3 4))
;; -> (0.6 0.8)
(vector:ToUnit '(2 -1 3))
;; -> (0.534522 -0.267261 0.801784)

AutoCAD .NET(C#)で求める

Vector2d, Vector3d 構造体を使う。

// < Example >
// 2D
var v2d = new Vector2d(3, 4);
new Vector2d(v2d.X / v2d.Length, v2d.Y / v2d.Length);
// -> (0.6,0.8)

// 3D
var v3d = new Vector3d(2, -1, 3);
new Vector3d(v3d.X / v3d.Length, v3d.Y / v3d.Length, v3d.Z / v3d.Length);
// -> (0.534522483824849,-0.267261241912424,0.801783725737273)
拡張メソッドを定義
using Autodesk.AutoCAD.Geometry;
// 2D
public static class Vector2dExtensions {
    public static Vector2d ToUnit(this Vector2d v) {
        return new Vector2d(v.X / v.Length, v.Y / v.Length);
    }
}
// 3D
public static class Vector3dExtensions {
    public static Vector3d ToUnit(this Vector3d v) {
        return new Vector3d(v.X / v.Length, v.Y / v.Length, v.Z / v.Length);
    }
}

ObjectARX(C++)で求める

AcGeVector2d, AcGeVector3d クラスを使う。

// < Example >
// 2D
AcGeVector2d v2d(3, 4);
AcGeVector2d uv2d(v2d.x / v2d.length(), v2d.y / v2d.length());
// uv2d == (0.600000, 0.800000)

// 3D
AcGeVector3d v3d(2, -1, 3);
AcGeVector3d uv3d(v3d.x / v3d.length(), v3d.y / v3d.length(), v3d.z / v3d.length());
// uv3d == (0.534522, -0.267261, 0.801784)

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up

Qiita Conference 2025 will be held!: 4/23(wed) - 4/25(Fri)

Qiita Conference is the largest tech conference in Qiita!

Keynote Speaker

ymrl、Masanobu Naruse, Takeshi Kano, Junichi Ito, uhyo, Hiroshi Tokumaru, MinoDriven, Minorun, Hiroyuki Sakuraba, tenntenn, drken, konifar

View event details
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?