#include#include // ... glm::vec2 velocity {vx, vy}; float max_speed = 10.0f; if (glm::length(velocity) > max_speed) { velocity = glm::normalize(velocity) * max_speed; }
#includeBoth GLM and Eigen are popular C++ libraries for mathematical operations, including vectors and matrices, and provide the normalize method for vectors.// ... Eigen::VectorXd features {f1, f2, f3}; double norm = features.norm(); if (norm != 0) { features.normalize(); }