"성공한 사람보다는 가치 있는 사람이 되려 노력해야 한다." – Albert Einstein

2022/01/23

sqrMagnitude

원문

https://docs.unity3d.com/ScriptReference/Vector3-sqrMagnitude.html

Vector3.sqrMagnitude

public float sqrMagnitude;

설명

해당 vector의 길이를 제곱한 값을 반환합니다. (읽기 전용)

vector v의 magnitude(크기)는 Mathf.Sqrt(Vector3.Dot(v, v))로 계산됩니다. 하지만 Sqrt(square root, 제곱근) 계산은 매우 복잡하고 실행이 일반 산술 연산들보다 더 오래 걸립니다. magnitude 속성을 바로 사용하는 대신 제곱 된 magnitude를 계산하는 것이 훨씬 더 빠릅니다. 느린 Sqrt를 호출하지 않는 것 말고는 계산 자체는 기본적으로 같습니다. 단순히 거리 비교를 위해 magnitude를 사용하고 있다면 제곱 된 magnitude와 (비교하려는) 거리의 제곱을 비교해서 같은 결과를 얻을 수 있습니다.

더 보기: magnitude

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour
{
    // other 트랜스폼이 closeDistance보다 가까운지 감지합니다
    // Vector3.magnitude를 사용하는 것보다 이게 더 빠릅니다
    public Transform other;
    public float closeDistance = 5.0f;

    void Update()
    {
        if (other)
        {
            Vector3 offset = other.position - transform.position;
            float sqrLen = offset.sqrMagnitude;

            // (제곱된 크기를 사용하기 때문에)비교할 거리를 제곱합니다
            if (sqrLen < closeDistance * closeDistance)
            {
                print("other 트랜스폼이 가까이 왔습니다!");
            }
        }
    }
}

댓글 남기기 | cat > 타닥타닥 | tag > ,

댓글 남기기

* 표시된 곳은 반드시 입력해주세요