![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=1739286685-8vE8MS4qCu11tKdgx6TsbCWLrUGj1vWB-0-b8e8ee33d26ae8d9fb610fee6b5086df)
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=1739286685-pUNpyiO3kOIFu3XwxXVBXt0dX9pKA4Ly-0-e31a675bd890551ca4c531f997822561)
If = (3, -5, 7), Then:
![](https://epubservercos.yuewen.com/1079FF/19470379008810206/epubprivate/OEBPS/Images/f07ae15b-1c8c-4586-8c39-5f54ca27a01c.png?sign=1739286685-cIyGYL3iXNjFYmOhhCVVDSehXIDm7nkn-0-c305029faa1bb6acadbd75fbdc7a699b)
= 9.110
The vector is therefore 9.11 units long