官术网_书友最值得收藏!

The angle between vectors

If two vectors are of unit length, the angle between them is the cosine of their dot product:

If the two vectors are not normalized, the dot product needs to be divided by the product of the length of both vectors:

To find the actual angle, not just the cosine of it, we need to take the inverse of the cosine on both sides, which is the arccosine function:

Implement the angle function in vec3.cpp. Don't forget to add the function declaration to vec3.h:

float angle(const vec3 &l, const vec3 &r) {

    float sqMagL = l.x * l.x + l.y * l.y + l.z * l.z;

    float sqMagR = r.x * r.x + r.y * r.y + r.z * r.z;

    if (sqMagL<VEC3_EPSILON || sqMagR<VEC3_EPSILON) {

        return 0.0f;

    }

    float dot = l.x * r.x + l.y * r.y + l.z * r.z;

    float len = sqrtf(sqMagL) * sqrtf(sqMagR);

    return acosf(dot / len);

}

Important note:

The acosf function returns angles in radians. To convert radians to degrees, multiply by 57.2958f. To convert degrees to radians, multiply by 0.0174533f.

主站蜘蛛池模板: 平江县| 阿拉善盟| 仲巴县| 石河子市| 永仁县| 都兰县| 梓潼县| 修武县| 芜湖市| 阜新| 阳曲县| 阳原县| 广元市| 涿州市| 蛟河市| 乌兰县| 牡丹江市| 崇礼县| 靖安县| 专栏| 河池市| 唐海县| 洛宁县| 宁晋县| 灵川县| 名山县| 玛曲县| 耿马| 元氏县| 丰都县| 墨竹工卡县| 高清| 古交市| 河北区| 上栗县| 宁德市| 泰安市| 济源市| 三门县| 含山县| 桐柏县|