![C++ Game Development By Example](https://wfqqreader-1252317822.image.myqcloud.com/cover/625/36698625/b_36698625.jpg)
Vector magnitude
The magnitude of the vector is equal to the length of the vector itself,. But how do we calculate it mathematically? .
The magnitude of a vector is given by the Pythagorean theorem, which specifies that in a right triangle , the square of length of a diagonal is equal to the sum of the squares of the adjacent sides. So when we look at the right triangle as follows, c2 = x2 + y2.
![](https://epubservercos.yuewen.com/1079FF/19470379008810206/epubprivate/OEBPS/Images/33d29f10-660e-4b50-951a-d0dabbcc34d9.png?sign=1739273861-LLbm09G5XEDFateGxJIm6w3n3y9Yolrp-0-257c0195467e197dc2a23e60ffce6829)
This can be extended to three dimensions with c2 = x2 + y2 + z2.
Magnitudes of vectors are indicated by double vertical bars, so the magnitude of a
vector is denoted by
. The magnitude is always greater than or equal to zero.
So, if vector A = (X, Y, Z) then the magnitude is given by the following equation:
![](https://epubservercos.yuewen.com/1079FF/19470379008810206/epubprivate/OEBPS/Images/2d887c18-924e-41c4-9f54-ec8091ec16cd.png?sign=1739273861-7PnZAe0M4pb61tkz0Sjfk5KDyohWlmuS-0-ff7a2648ca835169c02780c9d799174f)
If = (3, -5, 7), Then:
![](https://epubservercos.yuewen.com/1079FF/19470379008810206/epubprivate/OEBPS/Images/f07ae15b-1c8c-4586-8c39-5f54ca27a01c.png?sign=1739273861-MJK8neQS8ggIeLohvvNBEYAcjYMoWWFL-0-46398f6960f95ca5469d6111d2df36ca)
= 9.110
The vector is therefore 9.11 units long