#include#include #include int main() { std::vector myVector {5.0, 0.0, -4.0}; double length = std::sqrt((myVector[0]*myVector[0]) + (myVector[1]*myVector[1]) + (myVector[2]*myVector[2])); std::vector normalizedVector { myVector[0]/length, myVector[1]/length, myVector[2]/length }; std::cout << "Normalized vector: (" << normalizedVector[0] << ", " << normalizedVector[1] << ", " << normalizedVector[2] << ")\n"; return 0; }
#includeIn this example, the normalize function from the glm package is used to normalize a vector. The glm package is a popular C++ library for mathematics and graphics, often used in computer graphics and game development. The function from this package simplifies the normalization procedure by automatically finding the length and dividing each component by that length.#include #include int main() { glm::vec3 myVector {5.0f, 0.0f, -4.0f}; glm::vec3 normalizedVector = glm::normalize(myVector); std::cout << "Normalized vector: (" << normalizedVector.x << ", " << normalizedVector.y << ", " << normalizedVector.z << ")\n"; return 0; }