- Hands-On C++ Game Animation Programming
- Gabor Szauer
- 292字
- 2021-06-30 14:45:51
Normalizing vectors
A vector with a length of 1 is called a normal vector (or unit vector). Generally, unit vectors are used to represent a direction without a magnitude. The dot product of two unit vectors will always fall in the -1 to 1 range.
Aside from the 0 vector, any vector can be normalized by scaling the vector by the inverse of its length:
- Implement the normalize function in vec3.cpp. Don't forget to add the function declaration to vec3.h:
void normalize(vec3 &v) {
float lenSq = v.x * v.x + v.y * v.y + v.z * v.z;
if (lenSq < VEC3_EPSILON) { return; }
float invLen = 1.0f / sqrtf(lenSq);
v.x *= invLen;
v.y *= invLen;
v.z *= invLen;
}
- Implement the normalized function in vec3.cpp. Don't forget to add the function declaration to vec3.h:
vec3 normalized(const vec3 &v) {
float lenSq = v.x * v.x + v.y * v.y + v.z * v.z;
if (lenSq < VEC3_EPSILON) { return v; }
float invLen = 1.0f / sqrtf(lenSq);
return vec3(
v.x * invLen,
v.y * invLen,
v.z * invLen
);
}
The normalize function takes a reference to a vector and normalizes it in place. The normalized function, on the other hand, takes a constant reference and does not modify the input vector. Instead, it returns a new vector.
- Google Apps Script for Beginners
- GeoServer Cookbook
- MySQL 8從入門到精通(視頻教學版)
- Microsoft Dynamics 365 Extensions Cookbook
- Hands-On Data Structures and Algorithms with JavaScript
- NLTK基礎教程:用NLTK和Python庫構建機器學習應用
- Hadoop+Spark大數據分析實戰
- Python GUI Programming Cookbook
- Podman實戰
- Serverless computing in Azure with .NET
- Visual Studio 2015高級編程(第6版)
- HTML+CSS+JavaScript網頁設計從入門到精通 (清華社"視頻大講堂"大系·網絡開發視頻大講堂)
- Instant Zurb Foundation 4
- SFML Game Development
- Java EE 7 Development with WildFly